<?php

namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Actions;


use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\AbstractAction;
use SoapFault;
use SoapParam;
use SoapVar;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 28.08.19
 * Time: 13:47
 * Class DistributionList
 */
class DistributionList extends AbstractAction
{

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response|null
     */
    function getAllDistributionLists(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain)
    {

        $result = null;

        $params = array(
            new SoapVar('<ns1:domain by="name">'.$domain->getName().'</ns1:domain>', XSD_ANYXML),
        );

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

        return $result;
    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list
     * @return bool|\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList
     */
    public function read(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapVar('<ns1:dl by="id">'.$list->getId().'</ns1:dl>', XSD_ANYXML),
        );

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

        if($data = $result->getResponseBody()['GETDISTRIBUTIONLISTRESPONSE']['DL'])
        {
            $list->fill($data);
            return $list;
        }

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

    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list
     * @return bool|\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList
     */
    public function create(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapParam($list->getName(), "name"),
        );


        $attrs = $list->getAttrs();

        if($list->isDynamic())
        {
            $params[] = new SoapParam(1, "dynamic");
            unset($attrs['kerioDistributionListSendShareMessageToNewMembers']);
        }

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

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

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

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

    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list
     * @return bool|\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList
     */
    public function update(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = [
            new SoapParam($list->getId(), 'id')
        ];

        $attrs = $list->getAttrs();

        if($list->isDynamic())
        {
            $params[] = new SoapParam(1, "dynamic");
            unset($attrs['kerioDistributionListSendShareMessageToNewMembers']);
        }

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

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

            if($data = $result->getResponseBody()['MODIFYDISTRIBUTIONLISTRESPONSE']['DL'])
        {
            $list->fill($data);
            return $list;
        }

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

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list
     * @return bool
     */
    public function delete(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapParam($list->getId(), "id"),
        );

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

        if($result->getResponseBody()['DELETEDISTRIBUTIONLISTRESPONSE'])
        {
            return true;
        }

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

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list
     * @return bool
     */
    public function addMembers(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapParam($list->getId(), "id"),
        );

        foreach($list->getMembers() as $val)
        {
            $params[] = new SoapVar('<ns1:dlm>'.$val.'</ns1:dlm>', XSD_ANYXML);
        }

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

        if($result->getResponseBody()['ADDDISTRIBUTIONLISTMEMBERRESPONSE'])
        {
            return true;
        }

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

    public function deleteMembers(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapParam($list->getId(), "id"),
        );

        foreach($list->getMembers() as $val)
        {
            $params[] = new SoapVar('<ns1:dlm>'.$val.'</ns1:dlm>', XSD_ANYXML);
        }

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

        return $result;
    }


    public function addOwners(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapVar('<ns1:dl by="id">'.$list->getId().'</ns1:dl>', XSD_ANYXML),
            new SoapVar('<ns1:action op="addOwners">', XSD_ANYXML),
        );

        foreach($list->getOwners() as $owner)
        {
            $params[] = new SoapVar('<ns1:owner type="usr" by="name">'.$owner.'</ns1:owner>', XSD_ANYXML);
        }

        $params[] = new SoapVar('</ns1:action>', XSD_ANYXML);

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

        if($result->getResponseBody()['DISTRIBUTIONLISTACTIONRESPONSE'])
        {
            return true;
        }

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

    }

    public function deleteOwners(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapVar('<ns1:dl by="id">'.$id.'</ns1:dl>', XSD_ANYXML),
            new SoapVar('<ns1:action op="removeOwners">', XSD_ANYXML),
        );

        foreach($list->getOwners() as $owner) {
            $params[] = new SoapVar('<ns1:owner type="usr" by="name">'.$owner.'</ns1:owner>', XSD_ANYXML);
        }
        $params[] = new SoapVar('</ns1:action>', XSD_ANYXML);

        $result = $result = $this->connection
            ->cleanResponse()
            ->request("DistributionListActionRequest",$params);
        //todo
    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list
     * @return bool
     */
    public function addAlias(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapParam($list->getId(), 'id'),
            new SoapParam($list->getAlias(), 'alias'),
        );

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

        if($result->getResponseBody()['ADDDISTRIBUTIONLISTALIASRESPONSE'])
        {
            return true;
        }

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

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list
     * @return bool
     */
    public function deleteAlias(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList $list)
    {
        $params = array(
            new SoapParam($list->getId(), 'id'),
            new SoapParam($list->getAlias(), 'alias'),
        );


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

        if($result->getResponseBody()['REMOVEDISTRIBUTIONLISTALIASRESPONSE'])
        {
            return true;
        }

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



}