<?php

namespace ModulesGarden\ProxmoxAddon\App\Libs\Api\OpenSRS\Fastlookup;

use ModulesGarden\ProxmoxAddon\App\Libs\Api\OpenSRS\Exception;
use ModulesGarden\ProxmoxAddon\App\Libs\Api\OpenSRS\FastLookup;

class FastDomainLookup extends FastLookup
{
    public $resultFullRaw;
    public $resultRaw;
    public $resultFullFormatted;
    public $resultFormatted;
    public $tlds;
    protected $_domain = '';
    protected $_tldSelect = [];
    protected $_tldAll = [];
    protected $_formatHolder = '';

    public function __construct($formatString, $dataObject)
    {
        try
        {
            $this->_formatHolder = $formatString;

            $this->_validateObject($dataObject);
            $this->setDataObject($formatString, $dataObject);

            $this->send($this->tlds);
        }
        catch (\ModulesGarden\ProxmoxAddon\App\Libs\Api\OpenSRS\Exception $exe)
        {

        }
    }

    // Validate the object
    private function _validateObject($dataObject)
    {
        $domain = '';

        // search domain must be definded
        if (!isset($dataObject->data->domain))
        {
            Exception::notDefined('domain');
        }

        // Grab domain name
        $domain = $dataObject->data->domain;

        if (!isset($dataObject->data->selected))
        {
            Exception::notDefined('selected');
        }

        if (!isset($dataObject->data->alldomains))
        {
            Exception::notDefined('alldomains');
        }

        $selected   = explode(';', $dataObject->data->selected);
        $this->tlds = explode(';', $dataObject->data->alldomains);

        if (count(array_filter($selected)) >= 1)
        {
            $this->tlds = $selected;
        }
    }
}
