<?php
namespace ThurData\Servers\KerioEmail\App\UI\Client\Setting\Sections;

use ThurData\Servers\KerioEmail\App\Traits\FormExtendedTrait;
use ThurData\Servers\KerioEmail\App\UI\Admin\Custom\Sections\FreeFieldsSection;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\Select;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\Text;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\BaseField;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Sections\InputGroup;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\InputGroupElements;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\Hidden;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\Textarea;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\Info;
use ThurData\Servers\KerioEmail\Core\Helper\DnsHelper;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Sections\RawSection;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 12.11.19
 * Time: 13:39
 * Class GeneralSection
 */
class GeneralSection extends FreeFieldsSection
{
    protected $id   = 'generalSection';
    protected $name = 'generalSection';

    use FormExtendedTrait;

    public function initContent()
    {
        $maildomain = $this->getRequestValue('actionElementId');
        $dns = new DnsHelper();
        $info = new RawSection();
        if($dns->selfDns($maildomain)) {
            $infodns = new Info('infodns');
            $infodns->setId('infodns');
            $infodns->setName('infodns');
            $infodns->setTitle($maildomain);
            $infodns->setDescription('infodnsmessage');
            $info->addField($infodns);
        } else {
            $infoextdns = new Info('infoextdns');
            $infoextdns->setId('infoextdns');
            $infoextdns->setName('infoextdns');
            $infoextdns->setTitle($maildomain);
            $infoextdns->setDescription('infoextdnsmessage');
            $info->addField($infoextdns);
        }
        $this->addSection($info);

        $mxthurdata = new Text('mxthurdata');
        $mxthurdata->addHtmlAttribute('readonly','true');
        $mxthurdata->setDescription('description');
        
        $mxactive = new Text('mxactive');
        $mxactive->addHtmlAttribute('readonly','true');
        $mxactive->setDescription('description');

        $this->generateDoubleSection([$mxthurdata, $mxactive]);

        $spfthurdata = new Text('spfthurdata');
        $spfthurdata->addHtmlAttribute('readonly','true');
        $spfthurdata->setDescription('description');

        $spf = new Text('spf');
        $spf->addHtmlAttribute('readonly','true');
        $spf->setDescription('description');

        $this->generateDoubleSection([$spfthurdata, $spf]);

        $dmarcthurdata = new Text('dmarcthurdata');
        $dmarcthurdata->addHtmlAttribute('readonly','true');
        $dmarcthurdata->setDescription('description');

        $dmarc = new Text('dmarc');
        $dmarc->addHtmlAttribute('readonly','true');
        $dmarc->setDescription('description');

        $this->generateDoubleSection([$dmarcthurdata, $dmarc]);

        $dkimname = new Text('dkimname');
        $dkimname->addHtmlAttribute('readonly','true')->addHtmlAttribute('border', 0);
        $dkimname->setDescription('description');

        $this->generateDoubleSection([$dkimname]);

        $dkimthurdata = new Text('dkimthurdata');
        $dkimthurdata->addHtmlAttribute('readonly','true');
        $dkimthurdata->setDescription('description');

        $dkim = new Text('dkim');
        $dkim->addHtmlAttribute('readonly','true');

        $this->generateDoubleSection([$dkimthurdata, $dkim]);

        $this->generateDoubleSection([new Hidden('infomaildomain'), new Hidden('id')]);
        $this->generateDoubleSection([new Hidden('selfdns'), new Hidden('dnsok')]);
    }
}