<?php

namespace Hura8\System\Controller;

use Hura8\System\Model\DomainModel;

class DomainController
{
    protected  $objDomainModel;

    public function __construct()
    {
        $this->objDomainModel = new DomainModel();
    }

    protected $layout_options = [
        "pc"        => "Chỉ cho PC",
        "mobile"    => "Chỉ cho Mobile",
        //"amp"       => "Chỉ cho AMP",
        "all"       => "Cả PC & Mobile",
    ];


    public function buildDomainConfig() {
        $domain_per_languages = $this->getList('');

        $config_domain_list      = []; //all domains and attributes, so we can know the info of currently visited domain
        $config_domain_languages = []; //domains per language, so we can redirect to main domain of a specific language

        foreach ($domain_per_languages as $lang => $list_domains) {
            foreach ($list_domains as $_item) {
                $config_domain_languages[$lang][] = [
                    "domain"    => $_item['domain'],
                    "is_main"   => $_item['isMain'],
                    "layout"    => ($_item['layout']) ? $_item['layout'] : 'pc',
                ];

                $config_domain_list[$_item['domain']] = [
                    "lang"      => $_item['lang'],
                    "is_main"   => $_item['isMain'],
                    "layout"    => ($_item['layout']) ? $_item['layout'] : 'pc',
                ];
            }
        }

        return [
            "language"  => $config_domain_languages,
            "list"      => $config_domain_list,
        ];
    }


    public function getLayoutOption(){
        return $this->layout_options;
    }


    public function getList($language = '') {

        $item_list = $this->objDomainModel->getList([
            "language" => $language,
            "numPerPage" => 100,
        ]);

        $result = array();
        foreach ( $item_list as $rs ) {
            if(!$rs['lang']) $rs['lang'] = DEFAULT_LANGUAGE;
            $result[$rs['lang']][] = $rs;
        }

        return $result;
    }


    public function addNewDomain($domain, $language = DEFAULT_LANGUAGE){
        $this->objDomainModel->addNewDomain($this->cleanDomain($domain), $language);

        $this->rebuildConfigFile();
    }


    public function deleteDomain($domain){
        $this->objDomainModel->deleteDomain($this->cleanDomain($domain));

        $this->rebuildConfigFile();
    }


    public function setDomainMain($domain, $language){
        $this->objDomainModel->setDomainMain($this->cleanDomain($domain), $language);

        $this->rebuildConfigFile();
    }


    public function setDomainLayout($domain, $layout = 'pc'){
        $layout_option = $this->getLayoutOption();
        if(!isset($layout_option[$layout])) {
            return false;
        }

        $this->objDomainModel->setDomainLayout($this->cleanDomain($domain), $layout);
        $this->rebuildConfigFile();

        return true;
    }


    protected function cleanDomain($domain) {
        $domain_element = parse_url($domain);

        $scheme   = isset($domain_element['scheme']) ? $domain_element['scheme'] . '://' : '';
        $host     = $domain_element['host'] ?? '';
        $port     = isset($domain_element['port']) ? ':' . $domain_element['port'] : '';

        return strtolower(trim($scheme . $host . $port));
    }


    protected function rebuildConfigFile() {
        $objSettingController    = new SettingController();
        $objSettingController->create_config_file_n_upload();
    }
}
