<?php

namespace ThurData\Servers\KerioEmail\App\UI\Client\Home\Pages;

use ThurData\Servers\KerioEmail\App\Enums\ControllerEnums;
use ThurData\Servers\KerioEmail\App\Helpers\BuildUrlExtended;
use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
use ThurData\Servers\KerioEmail\App\UI\Client\Home\Fields\FeatureField;
use ThurData\Servers\KerioEmail\Core\UI\Builder\BaseContainer;
use ThurData\Servers\KerioEmail\Core\UI\Interfaces\ClientArea;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * 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()
    {
        /**
         * product manager
         */
        $productManager = new ProductManager();
        $productManager->loadByHostingId($this->getRequestValue('id'));

        /**
         *
         * check if email account page is enabled
         */
        if($productManager->isControllerAccessible(ControllerEnums::EMAIL_ACCOUNT_PAGE))
        {
            $feature = new FeatureField('emailAccount');
            $feature->setUrl(BuildUrlExtended::getProvisioningUrl('emailAccount'));
            $this->addFeature($feature);
        }

        /**
         *
         * check if email alias page is enabled
         */
        if($productManager->isControllerAccessible(ControllerEnums::EMAIL_ALIAS_PAGE))
        {
            $feature = new FeatureField('emailAlias');
            $feature->setUrl(BuildUrlExtended::getProvisioningUrl('emailAlias'));
            $this->addFeature($feature);
        }

        /**
         *
         * check if ressource page is enabled
         */
        if($productManager->isControllerAccessible(ControllerEnums::RESSOURCE_PAGE))
        {
            $feature = new FeatureField('ressource');
            $feature->setUrl(BuildUrlExtended::getProvisioningUrl('ressource'));
            $this->addFeature($feature);
        }

        /**
         *
         * check if distribution list page is enabled
         */
        if($productManager->isControllerAccessible(ControllerEnums::DISTRIBUTION_MAIL_PAGE))
        {
            $feature = new FeatureField('distributionList');
            $feature->setUrl(BuildUrlExtended::getProvisioningUrl('distributionList'));
            $this->addFeature($feature);
        }

        /**
         *
         * check if domain alias page is enabled
         */
        if($productManager->isControllerAccessible(ControllerEnums::DOMAIN_ALIAS_PAGE)) {
            $feature = new FeatureField('domainAlias');
            $feature->setUrl(BuildUrlExtended::getProvisioningUrl('domainAlias'));
            $this->addFeature($feature);
        }

        /**
         *
         * check if settings page is enabled
         */
        if($productManager->isControllerAccessible(ControllerEnums::SETTING_PAGE)) {
            $feature = new FeatureField('setting');
            $feature->setUrl(BuildUrlExtended::getProvisioningUrl('setting'));
            $this->addFeature($feature);
        }

        /**
         *
         * check if webmail is enabled
         */
        if($productManager->isControllerAccessible(ControllerEnums::WEBMAIL_PAGE)) {
            $feature = new FeatureField('goWebmail');
            $feature->setTargetBlank(true);
            $feature->setUrl(BuildUrlExtended::getProvisioningUrl('webmail'));
            $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;
    }

}
