<?php
namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Actions;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\AbstractAction;
use SoapFault;
use SoapParam;
use SoapVar;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Helpers\XmlParser;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 28.08.19
 * Time: 13:46
 * Class Account
 */
class Account extends AbstractAction
{

    public function read()
    {

    }

    /**
     * @description create account in Zimbra API
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account
     * @return bool|void
     */
    public function create(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account)
    {
        /**
         *
         */
        $params = [
            new SoapParam($account->getName(), "name"),
            new SoapParam($account->getPassword(), "password"),
        ];

        /**
         * add account params
         */
        foreach ($account->getAttrs() as $key => $value)
        {
            $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
        }

        /**
         * run request
         */
        $result = $this->connection->request("CreateAccountRequest", $params);

        /**
         * load response to model
         */
        if($accountData = $result->getResponseBody()['CREATEACCOUNTRESPONSE']['ACCOUNT'])
        {
            $account->fill($accountData);
            return $account;
        }

        $this->setError($result->getLastError());
        return false;
    }

    /**
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias $alias
     * @return \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias
     */
    public function createAlias(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias $alias)
    {
        $params = [
            new SoapParam($alias->getAccountId(), "id"),
            new SoapParam($alias->getAlias(), "alias"),
        ];

        $result = $this->connection->request("AddAccountAliasRequest", $params);

        if(!$result->getLastError())
        {
            return $alias;
        }

        return false;
    }

    /**
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account
     * @return bool|\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account
     */
    public function update(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account)
    {
        $params = [
            new SoapParam($account->getId(), "id"),
        ];

        /**
         * add account params
         */
        foreach ($account->getAttrs() as $key => $value)
        {
            $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
        }

        $result = $this->connection->request("ModifyAccountRequest", $params);

        if($accountData = $result->getResponseBody()['MODIFYACCOUNTRESPONSE']['ACCOUNT'])
        {
            $account->fill($accountData);
            return $account;
        }

        $this->setError($result->getLastError());
        return false;
    }

    public function setPassword(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account)
    {
        $params = [
            new SoapParam($account->getId(), "id"),
            new SoapParam($account->getPassword(), "newPassword"),
        ];

        $result = $this->connection->request("SetPasswordRequest", $params);
        if($result->getLastError())
        {
            $this->setError($result->getLastError());
            return false;
        }

        return true;

    }

    /**
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account
     * @return bool
     */
    public function delete(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account)
    {
        $params = [
            new SoapParam($account->getId(), "id"),
        ];

        $result = $this->connection->request("DeleteAccountRequest", $params);
        if($result->getLastError())
        {
            $this->setError($result->getLastError());
            return false;
        }

        return true;
    }

    /**
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias $alias
     * @return bool
     */
    public function deleteAlias(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias $alias)
    {
        $params = [
            new SoapParam($alias->getAccountId(), "id"),
            new SoapParam($alias->getAlias(), "alias"),
        ];

        $result = $this->connection
            ->request("RemoveAccountAliasRequest", $params);

        if(!$result->getLastError())
        {
            return true;
        }
        return false;

    }

    /**
     * @param $name
     * @return \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Response
     */
    public function getAccountId($name)
    {
        $params = [
            new SoapVar('<ns1:account by="name">' . $name . '</ns1:account>', XSD_ANYXML),
        ];

        return $this->connection->cleanResponse()->request("GetAccountInfoRequest", $params);

    }

    /**
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account
     * @return \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Response
     */
    public function getAccountInfo(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account)
    {
        if($value = $account->getId())
        {
            $type = 'id';
        }elseif($value =$account->getName())
        {
            $type = 'name';
        }

        $result = null;

        $params = [
            new SoapVar('<ns1:account by="' . $type . '">' . $value . '</ns1:account>', XSD_ANYXML),
        ];

        return $this->connection->request("GetAccountInfoRequest", $params);
    }

    /**
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account
     * @return \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Response
     */
    public function delegateAuth(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account)
    {
        if($value = $account->getId())
        {
            $type = 'id';
        }elseif($value =$account->getName())
        {
            $type = 'name';
        }

        $result = null;

        $params = [
            new SoapVar('<ns1:account by="' . $type . '">' . $value . '</ns1:account>', XSD_ANYXML),
        ];

        $result = $this->connection->cleanResponse()->request("DelegateAuthRequest", $params);

        if($result->getLastError())
        {
            $this->setError($result->getLastError());
            return false;
        }

        return $result;
    }

    /**
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account
     * @return \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Response
     */
    public function getAccountOptions(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account $account)
    {
        if($value = $account->getId())
        {
            $type = 'id';
        }elseif($value =$account->getName())
        {
            $type = 'name';
        }

        $params = [
            new SoapVar('<ns1:account by="' . $type . '">' . $value . '</ns1:account>', XSD_ANYXML),
        ];

        return  $this->connection
            ->cleanResponse()
            ->request("GetAccountRequest", $params);
    }

    /**
     * @param \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Domain $domain
     * @return \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Response
     */
    public function getAllByDomain(\ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Domain $domain)
    {
        if($value = $domain->getId())
        {
            $type = 'id';
        }elseif($value =$domain->getName())
        {
            $type = 'name';
        }

        $params = [
            new SoapVar('<ns1:domain by="' . $type . '">' . $value . '</ns1:domain>', XSD_ANYXML),
        ];
        $result = $this->connection->request("GetAllAccountsRequest", $params);
        return $result;
    }

    public function getAllAccounts($idOrNameDomain, $type = "auto")
    {
        if ($type == "auto")
        {
            //$realType = getDomainType($idOrNameDomain);
        }
        else
        {
            $realType = $type;
        }

        $result = null;

        $params = [
            new SoapVar('<ns1:domain by="' . $realType . '">' . $idOrNameDomain . '</ns1:domain>', XSD_ANYXML),
        ];

        return  $this->connection->request("GetAllAccountsRequest", $params);

    }

}