<?php
namespace ModulesGarden\Servers\OpenXchange\App\UI\Client\EmailAccount\Sections;
use ModulesGarden\Servers\OpenXchange\App\Traits\FormExtendedTrait;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Fields\Text;
use ModulesGarden\Servers\OpenXchange\App\Validators\PasswordsValidator;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Fields\Hidden;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Fields\Password;
use ModulesGarden\Servers\OpenXchange\App\Validators\RepeatPasswordValidator;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Sections\InputGroup;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\Others\Fields\OptGroupSelect;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\Others\Fields\ExtendedInputField;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Fields\InputGroupElements;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\Others\Sections\FreeFieldsSection;
use function \ModulesGarden\Servers\OpenXchange\Core\Helper\di;
use ModulesGarden\Servers\OpenXchange\App\Helpers\Helper;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\Customs\Fields\PasswordGenerateExtended;
use ModulesGarden\Servers\OpenXchange\App\Models\ProductConfiguration;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\EmailAccount\Constants;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 12.11.19
 * Time: 13:39
 * Class GeneralSection
 */
class GeneralSection extends FreeFieldsSection
{
    protected $id   = 'generalSection';
    protected $name = 'generalSection';

    use FormExtendedTrait;

    public function initContent()
    {
        $hiddenField = new Hidden('autofillGen');
        $this->addField($hiddenField);

        //check if this is updated product from free (freemium model)
        $hostingId = $this->request->getValue('id');

        if(empty($hostingId))
        {
            $hostingId = $_SESSION['serviceid'];
        }

        $params       = Helper::moduleParams($hostingId);
        $freeAccounts = Helper::freeAccountsLimit($params);

        //if ($freeAccounts > 0)
        //{
            $emialTypeField = (new Select('emailType'))->notEmpty();
            $this->addField($emialTypeField);
            
            //gen select if some addons available
            $availableAddons = $this->getAvailableAddons($params);

            if (!empty($availableAddons))
            {
                $emialAddonField = (new Select('emailAddonType'))->enableMultiple();
                $this->addField($emialAddonField);
            }
            
        //}
            
        $this->generateDoubleSection([(new Text('firstname'))->notEmpty(), (new Text('lastname'))->notEmpty()]);
        $this->generateDoubleSection([/*(new Text('display_name'))->notEmpty(),*/(new ExtendedInputField('quota'))->setFieldType(ExtendedInputField::TYPE_NUMBER)]);
        $this->generateDoubleSection([(new OptGroupSelect('timezone'))->notEmpty(), (new Select('language'))->notEmpty()]);

        $email = new InputGroup('usernameGroup');

        $email->addTextField('username', false, true);
        $email->addInputAddon('emailSign', false, '@');
        $email->addInputComponent((new InputGroupElements\Text('domain'))->notEmpty()->addHtmlAttribute('readonly', 'true'));
        $this->addSection($email);

        $passwd = new PasswordGenerateExtended('password');
        //$passwd = new Password('password');
        $lang = di('lang');
        $description = $lang->T('passDescription');
        $passwd->setDescription($description);
        $passwd->addValidator(new PasswordsValidator());
        $passwd->notEmpty();

        $repPasswd = (new Text('repeat_password'))->addValidator(new RepeatPasswordValidator());
        $repPasswd->notEmpty();

        $this->generateDoubleSection([$passwd, $repPasswd]);
    }
    
    private function getAvailableAddons($params)
    {
        $configOptions     = $params['configoptions'];
        $allowedAddonsData = [];
        
        if ($configOptions && !empty($configOptions))
        {
            if (isset($configOptions['acc_limit_guard']) && $configOptions['acc_limit_guard'] > 0)
            {
                $allowedAddonsData['guard'] = array_search('guard', Constants::getTranslatedAddons());
            }
            
            if (isset($configOptions['acc_limit_ai']) && $configOptions['acc_limit_ai'] > 0)
            {
                $allowedAddonsData['ai'] = array_search('ai', Constants::getTranslatedAddons());
            }
        }

        if (empty($allowedAddonsData))
        {
            $config = ProductConfiguration::config($params['packageid']);

            if ($config['email_accounts_guard'] && $config['email_accounts_guard'] != 0)
            {
                $allowedAddonsData['guard'] = array_search('guard', Constants::getTranslatedAddons());
            }

            if ($config['email_accounts_ai'] && $config['email_accounts_ai'] != 0)
            {
                $allowedAddonsData['ai'] = array_search('ai', Constants::getTranslatedAddons());
            }
        }

        return $allowedAddonsData;
    }
}
