<?php
namespace ThurData\Servers\KerioEmail\App\Libs\Kerio;
use ThurData\Servers\KerioEmail\App\Enums\Kerio;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioApiException;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Interfaces\ClientInterface;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Client as SoapClient;
use ThurData\Servers\KerioEmail\App\Traits\ParamsHandler;
use ThurData\Servers\KerioEmail\App\Traits\StorageTrait;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 27.08.19
 * Time: 15:05
 * Class KerioClientConnection
 * @property SoapClient $soap
 */
class Api
{

    use StorageTrait;
    use ParamsHandler;


    /**
     * Api constructor.
     * @param array $params
     */
    public function __construct($params = [])
    {
       $this->setParams($params);
    }

    /**
     * @param $name
     * @return ClientInterface
     * @throws \Exception
     */
    public function __get($name)
    {
        /**
         * check if class is in storage
         */
        if(!$this->getStorage()->exists(strtolower($name)))
        {
            /**
             * prepare class namespace
             */
            $class = '\\ThurData\\Servers\\KerioEmail\\App\\Libs\\Kerio\\Components\\Api\\'.ucfirst($name).'\\Client';

            /**
             * check if class exists or throw error
             */
            if(!class_exists($class))
            {
                throw new KerioApiException([
                    'message' => 'Class `'.$class.'`` does not exists',
                    'debugCode' => KerioApiException::UNSUPORTED_CLIENT]);
            }

            /**
             *
             * create new client class
             * prepare server parameters
             */
            $hostname = $this->params['serverhostname'] ? $this->params['serverhostname'] : $this->params['serverip'];
            $port     = $this->params['serversecure'] === 'on' ? Kerio::SECURE_PORT : Kerio::PORT;
            $port     = $this->params['serverport'] ? $this->params['serverport'] : $port;
            $username = $this->params['serverusername'];
            $pw       = html_entity_decode($this->params['serverpassword']);
            $auth     = $this->params['authtype'] ? $this->params['authtype'] : 'admin';

            $client = new $class($hostname, $port, $username, $pw, $auth);

            /**
             *
             * throw exception if class is not implemented
             */
            if(!($client instanceof ClientInterface))
            {
                throw new KerioApiException([
                    'message' => "Provided client type `{$class}` is not supported",
                    'debugCode' => KerioApiException::UNSUPORTED_CLIENT]);
            }

            /**
             *
             * add class to storage
             */
            $this->getStorage()->set(strtolower($name), $client);

        }else{
            /**
             *
             * get client from storage
             */
            $client = $this->getStorage()->get(strtolower($name));
        }

        /**
         *
         */
        return $client;
    }



}