<?php

namespace ModulesGarden\HostingQuotaNotifications\Core\Helper;

use ModulesGarden\HostingQuotaNotifications\Core\Http\AbstractController;
use ModulesGarden\HostingQuotaNotifications\Core\ServiceLocator;
use ModulesGarden\HostingQuotaNotifications\Core\Configuration\Addon;

/**
 * Description of BuildUrl
 *
 * @author Rafał Ossowski <rafal.os@modulesgarden.com>
 */
class BuildUrl
{

    public static function getUrl($controller = null, $action = null, array $params = [], $isFullUrl = true)
    {
        if (AbstractController::isAdmin())
        {
            $url = 'addonmodules.php?module=' . AbstractController::getModule();
        }
        else
        {
            $url = 'index.php?m=' . AbstractController::getModule();
        }

        if ($controller)
        {
            $url .= '&mg-page=' . $controller;
            if ($action)
            {
                $url .= '&mg-action=' . $action;
            }

            if ($params)
            {
                $url .= '&' . http_build_query($params);
            }
        }

        if ($isFullUrl)
        {
            $baseUrl = self::baseUrl();
            $url     = $baseUrl . $url;
        }

        return $url;
    }
    
    public static function getBaseUrl()
    {
        return self::baseUrl();
    }

    public static function getNewUrl($protocol = 'http', $host = 'modulesgarden.com', $params = [])
    {
        $url = "{$protocol}://{$host}";
        if ($params)
        {
            $url .= '?' . http_build_query($params);
        }
        return $url;
    }

    public static function getAssetsURL()
    {

        $addon    = ServiceLocator::call(Addon::class);
        $name     = $addon->getConfig('systemName');
        $template = $addon->getConfig('template', 'default');

        if (AbstractController::isAdmin())
        {
            return '../modules/addons/' . $name . '/templates/admin/assets';
        }
        else
        {
            return 'modules/addons/' . $name . '/templates/client/' . $template . '/assets';
        }
    }

    private static function baseUrl()
    {
        $protocol = 'https';
        if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')
        {
            $protocol = 'http';
        }
        $host   = $_SERVER['HTTP_HOST'];
        $surfix = $_SERVER['PHP_SELF'];
        $surfix = explode('/', $surfix);
        array_pop($surfix);
        $surfix = implode('/', $surfix);
        return "{$protocol}://{$host}{$surfix}/";
    }
}
