<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Admin\ProductConfiguration\Pages\Sections;


use ModulesGarden\Servers\ZimbraEmail\App\Enums\Size;
use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;
use ModulesGarden\Servers\ZimbraEmail\App\Traits\LangHandler;
use ModulesGarden\Servers\ZimbraEmail\App\Traits\ServerParams;
use ModulesGarden\Servers\ZimbraEmail\App\Traits\ZimbraApiHandler;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Admin\Custom\Fields\ExtendedInputField;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Admin\Custom\Sections\BoxSectionExtended;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Admin\Custom\Sections\HalfPageCustomCosSection;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms\Sections\BoxSection;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms\Sections\HalfPageSection;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms\Fields\Text;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\ClassOfService;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 29.08.19
 * Time: 08:35
 * Class ClassOfServiceFeatures
 */
class ClassOfServiceFeatures extends BoxSectionExtended implements AdminArea
{
    use LangHandler;

    protected $id       = 'classOfServiceFeatures';
    protected $name     = 'classOfServiceFeatures';
    protected $title    = 'classOfServiceFeatures';

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

    /**
     *
     */
    public function loadCos()
    {
        $lang = $this->getLang();

        $left  = new HalfPageCustomCosSection('left');
        $right = new HalfPageCustomCosSection('right');

        /**
         * api manager
         */
        $manager = new ZimbraManager();
        /**
         * repository
         */
        $repository = $manager->getApiByProduct($this->getRequestValue('id'))->soap->repository();
        /**
         * cos
         */
        $cos = $repository->cos->all();
        $this->cos = $cos;

        $pointer = 1;
        foreach($cos as $key => $cosModel)
        {
            /* @var $cosModel ClassOfService */
            $id = 'cos['.$cosModel->getId().']';
            $quete = $cosModel->getMbMailQuote() ;

            $field = new ExtendedInputField($cosModel->getId());
            $field->setFieldType(ExtendedInputField::TYPE_NUMBER);
            $field->addHtmlAttribute('min', Size::UNLIMITED);
            $field->setName($id);
            $field->setRawTitle("{$cosModel->getName()} ({$quete} MB)");
            $field->setRawDescription(sprintf($lang->absoluteT('Enter to limit an accounts number of %s with quota %s MB or set -1 to unlimited'),ucfirst($cosModel->getName()), $quete) );
            $field->setDefaultValue(Size::DEFAULT_NULL_VALUE);

            if($pointer %2 == 0)
            {
                $right->addField($field);
            }else{
                $left->addField($field);
            }
            $pointer++;
        }
        $this
            ->addSection($left)
            ->addSection($right);
    }

}