<?php

namespace ModulesGarden\ProxmoxAddon\App\Libs\Api;

use ModulesGarden\ProxmoxAddon\App\Libs\Api\OpenSRS\Request;
use ModulesGarden\ProxmoxAddon\Core\Api\AbstractApi;
use ModulesGarden\ProxmoxAddon\Core\Helper;
use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
use Mso\IdnaConvert\IdnaConvert;

/**
 * Description of OpenSRS
 *
 * @author Rafał Ossowski <rafal.os@modulesgarden.com>
 */
class OpenSRS extends AbstractApi
{
    const METHOD_API_CHECK_ONE_DOMAIN   = 'CheckOneDomain';
    const METHOD_API_CHECK_MORE_DOMAINS = 'CheckMoreDomains';
    protected $defineFields = [
        'OSRS_USERNAME'        => '', // get with db
        'OSRS_KEY'             => '', // get with db
        'CRYPT_TYPE'           => 'ssl',
        'OSRS_HOST'            => '', // get with db
        'OSRS_SSL_PORT'        => '55443',
        'OSRS_PROTOCOL'        => 'XCP',
        'OSRS_VERSION'         => 'XML:0.1',
        'OSRS_DEBUG'           => 0, // static field
        'SRS_SPIN_COUNT'       => 0, // get with db
        'OSRS_FASTLOOKUP_PORT' => '51000',
        'MAIL_HOST'            => 'https://admin.hostedemail.com',
        'MAIL_USERNAME'        => '', // null
        'MAIL_PASSWORD'        => '', // null
        'MAIL_ENV'             => 'LIVE',
        'MAIL_CLIENT'          => 'OpenSRS PHP Toolkit',
        'APP_MAIL_HOST'        => 'ssl://admin.hostedemail.com',
        'APP_MAIL_USERNAME'    => '', // null
        'APP_MAIL_PASSWORD'    => '', // null
        'APP_MAIL_DOMAIN'      => '', // null
        'APP_MAIL_PORT'        => '4449',
        'APP_MAIL_PORTWAIT'    => '10'
    ];
    protected $settings;
    protected $request;

    /**
     * @var IdnaConvert
     */
    protected $idnaConvert;

    public function __construct(IdnaConvert $idnaConvert)
    {
        $this->idnaConvert = $idnaConvert;
    }

    public function setSettings($settings)
    {
        $this->settings = $settings;
        $this->loadDefine();
        $this->request = Helper\di(Request::class);

        return $this;
    }

    protected function loadDefine()
    {
        $register                             = Helper\sl('register');
        $this->defineFields['OSRS_USERNAME']  = $this->settings['username'];
        $this->defineFields['OSRS_KEY']       = $this->settings['privateKey'];
        $this->defineFields['OSRS_HOST']      = $this->settings['hostname'];
        $this->defineFields['SRS_SPIN_COUNT'] = $this->settings['spinnerCount'];

        foreach ($this->defineFields as $defineName => $value)
        {
            if ($register->isRegister($defineName) === false)
            {
                $register->register($defineName, $value);
            }
        }
    }

    public function checkOneDomain($record)
    {
        return $this->callToMethod(self::METHOD_API_CHECK_ONE_DOMAIN, $record);
    }

    protected function callToMethod($name, $params = null)
    {
        $namespace = "\\" . ModuleConstants::getRootNamespace() . "\App\Libs\Api\OpenSRS\Method\\" . $name;
        return (new $namespace($this->request, $this->settings, $this->idnaConvert))->execute($params);
    }

    public function checkMoreDomain($records)
    {
        return $this->callToMethod(self::METHOD_API_CHECK_MORE_DOMAINS, $records);
    }
}
