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

use ThurData\Servers\KerioEmail\App\Enums\Kerio;
use function ThurData\Servers\KerioEmail\Core\Helper\di;
use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
use ThurData\Servers\KerioEmail\Core\Helper\DnsHelper;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
use ThurData\Servers\KerioEmail\Api\KerioWhmcs;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 10.09.19
 * Time: 13:06
 * Class SettingDataProvider
 */
class InfoSettingDataProvider extends BaseDataProvider
{

    public function read()
    {
        $maildomain = $this->actionElementId;
        $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
        try {
            $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
            $dkimSet = $api->getDkimDnsRecord($maildomain);
        } catch (KerioApiException $error) {
            logModuleCall(
                'kerioEmail',
                __FUNCTION__,
                $error,
                'Debug Error',
                $error->getMessage()
            );
            return ['error' => $error->getMessage()];
        }
        $api->logout();
        // format dkim response
        $dkimArray = explode(PHP_EOL,$dkimSet['detail']);
        $dkimValue = explode(":",$dkimArray[1]);
        $this->domainKey = ltrim($dkimValue[1]);
        $dkimName = explode(":",$dkimArray[0]);
        $this->dkimName = ltrim($dkimName[1]);

        // get settings from product configuration
        $productManager = new ProductManager();
        $productManager->loadById($this->getWhmcsParamByKey('pid'));
        $this->spfConfig = $productManager->get('spf_string');
        $this->dmarcConfig = $productManager->get('dmarc_string');
        $clientDomains = localAPI('GetClientsDomains', array('clientid' => $this->getWhmcsParamByKey('userid')));

        $dns = new DnsHelper();

        /**
         * format model to array
         */
        in_array($maildomain,$clientDomains) ? $selfdomain = 1 : $selfdomain = 0;
        $dnsRecords = $dns->getRecords($maildomain);
                
            logModuleCall(
                'kerioEmail',
                __FUNCTION__,
                implode('',$dnsRecords['spf']),
                'DEbug',
                $this->spfConfig
            );

        $dnsRecords['selfdns'] ? $selfDns = 1 : $selfDns = 0;
        if(empty($dnsRecords['mx'])){
            $mx = 'unset';
        } else {
            $mx = implode('',$dnsRecords['mx']);
        }
        if(empty($dnsRecords['spf'])) {
            $spf = 'unset';
        } else {
            $spf = implode('',$dnsRecords['spf']);
//            $spf = 'Blubb';
        }
        if(empty($dnsRecords['dmarc'])) {
            $dmarc = 'unset';
        } else {
            $dmarc = implode('',$dnsRecords['dmarc']);
        }
        if(empty($dnsRecords['dkim'])) {
            $dkim = 'unset';
        } else {
            $dkim = implode('',$dnsRecords['dkim']);
        }
        $this->data = [
            'id' => $maildomain,
            'domain' => $maildomain,
            'mxactive' => $mx,
            'spf' => $spf,
            'dmarc' => $dmarc,
            'dkim' => $dkim,
            'selfdns' => $selfDns,
            'selfdomain' => $selfdomain,
            'mxthurdata' => $this->getWhmcsParamByKey('serverhostname'),
            'spfthurdata' => $this->spfConfig,
            'dmarcthurdata' => $this->dmarcConfig,
            'dkimthurdata' => $this->domainKey
        ];
        $this->availableValues['dnsrecords'] = $dnsRecords;
    }

    public function create()
    {

    }

    public function updateStatus()
    {

    }

    public function update()
    {

    }
}