<?php

namespace ModulesGarden\HostingQuotaNotifications\App\UI\TicketTemplates\Forms\EditSubPages;

use ModulesGarden\HostingQuotaNotifications\App\Helper\SmartyHelper\Notification;
use ModulesGarden\HostingQuotaNotifications\App\Helper\SmartyHelper\SmartyHelper;
use ModulesGarden\HostingQuotaNotifications\App\UI\Validators\NotEmptyWithTrim;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\BaseForm;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AdminArea;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\Fields\Select;
use \ModulesGarden\HostingQuotaNotifications\App\UI\TicketTemplates\Fields\TextArea;
use \ModulesGarden\HostingQuotaNotifications\App\UI\TicketTemplates\Fields\AccordingAriaField;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\Fields\Hidden;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\FormConstants;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\TemplatesHelper;
use \ModulesGarden\HostingQuotaNotifications\Core\Helper;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\Sections;
//use ModulesGarden\HostingQuotaNotifications\App\Models\Templates\MessageGroups;
use \ModulesGarden\HostingQuotaNotifications\App\UI\TicketTemplates\Fields\HalfPageSectionCustom;
use \ModulesGarden\HostingQuotaNotifications\App\UI\TicketTemplates\Providers\TicketTemplatesLangsProvider;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Request;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\Fields\Text;

/**
 * Description of AddLangForm
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class AddLangForm extends BaseForm implements AdminArea
{
    protected $id    = 'addLangForm';
    protected $name  = 'addLangForm';
    protected $title = 'addLangForm';

    public function initContent()
    {

        $this->setFormType(FormConstants::CREATE);
        $this->dataProvider = new TicketTemplatesLangsProvider();
        $this->initFields();
    }

    protected function initFields()
    {
        $leftSide = new HalfPageSectionCustom();
        $leftSide->setWidth('col-lg-5');
        $leftSide->setId('leftSide');


        $rightSide = new HalfPageSectionCustom();
        $rightSide->setWidth('col-lg-7');
        $rightSide->setId('rightSide');

        $leftSide->addField(new Hidden('id'));

        $leftSide->addField((new Hidden('template_id'))->setValue(Request::getValue('templateId')));

        $leftSide->addField((new Text('subject'))->addValidator(new NotEmptyWithTrim()));


        $id = Request::getValue('actionElementId');
        $langs = TemplatesHelper::getUnusedLanguages($id);

        $leftSide->addField((new Select('lang'))->setAvalibleValues($langs));

        $leftSide->addField((new TextArea('message'))->addValidator(new NotEmptyWithTrim()));

        $this->getVariables($tpl_id, $rightSide);

        $this->addSection($leftSide);
        $this->addSection($rightSide);
    }

    protected function getVariables($tpl_id, $section)
    {

        $vars = TemplatesHelper::getGroupedGroupVariablesNames('product');
        $vars = array_merge($vars, ['notification' => Notification::getMergeFieldsNames()]);

        foreach ($vars as $key => $variables)
        {
            $ticket = new AccordingAriaField();
            $ticket
                    ->setVueAria($key)
                    ->initIds($key)
                    ->setName($key)
                    ->serVariables($variables);
            $section->addField($ticket);
        }
    }
}
