<?php

namespace ModulesGarden\Servers\OpenXchange\App\UI\Client\Home\Pages;

use ModulesGarden\Servers\OpenXchange\App\Enums\ControllerEnums;
use ModulesGarden\Servers\OpenXchange\App\Helpers\BuildUrlExtended;
use ModulesGarden\Servers\OpenXchange\App\Libs\Product\ProductManager;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\Home\Fields\FeatureField;
use ModulesGarden\Servers\OpenXchange\Core\UI\Builder\BaseContainer;
use ModulesGarden\Servers\OpenXchange\Core\UI\Interfaces\ClientArea;
use ModulesGarden\OpenXchangeAddon\Core\Models\ModuleSettings\Model;
use ModulesGarden\Servers\OpenXchange\App\Models\ProductConfiguration;
use function \ModulesGarden\Servers\OpenXchange\Core\Helper\di;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 10.09.19
 * Time: 10:09
 * Class Dashboard
 */
class Dashboard extends BaseContainer implements ClientArea
{
    
    /**
     * @var array
     */
    protected $featureContainer = [];

    public function initContent()
    {
        $this->initFeatures();
    }

    /**
     *
     */
    protected function initFeatures()
    {
        $feature = new FeatureField('emailAccount');
        $feature->setUrl(BuildUrlExtended::getProvisioningUrl('emailAccount'));
        $this->addFeature($feature);

        $feature = new FeatureField('emailAlias');
        $feature->setUrl(BuildUrlExtended::getProvisioningUrl('emailAlias'));
        $this->addFeature($feature);

        $feature = new FeatureField('emailForwarder');
        $feature->setUrl(BuildUrlExtended::getProvisioningUrl('emailForwarder'));
        $this->addFeature($feature);

        $hostingId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
        $hosting   = \WHMCS\Service\Service::find($hostingId);
        $config    = ProductConfiguration::config($hosting->packageid);

        if (isset($config['show_catch_all_button']) && $config['show_catch_all_button'] == 'on')
        {
            $feature = new FeatureField('emailCatchAll');
            $feature->setUrl(BuildUrlExtended::getProvisioningUrl('emailCatchAll'));
            $this->addFeature($feature);
        }

        $feature = new FeatureField('mxRecords');
        $feature->setUrl(BuildUrlExtended::getProvisioningUrl('mxRecords'));
        $this->addFeature($feature);

        $configData  = Model::where('setting', '=', 'portalURL')->get()->toArray();
        $oxPortalUrl = $configData[0]['value'];

        if ($oxPortalUrl && $oxPortalUrl != '')
        {
            $feature = new FeatureField('loginButton');
            $feature->setUrl($oxPortalUrl);
            
            $configNameData  = Model::where('setting', '=', 'portalName')->get()->toArray();
            $oxPortalName    = $configNameData[0]['value'];
            
            if ($oxPortalName && $oxPortalName != '')
            {
                $lang       = di('lang');
                $buttonText = $lang->T('loginButton').$oxPortalName;

                $feature->setTitle($buttonText);
            }

            $this->addFeature($feature);
        }
    }

    /**
     * @param FeatureField $feature
     * @return $this
     */
    protected function addFeature(FeatureField $feature)
    {
        $this->featureContainer[$feature->getId()] = $feature;
        return $this;
    }

    /**
     * @return array
     */
    public function getFeatures()
    {
        return $this->featureContainer;
    }

}
