<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;


use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\AbstractApiClient;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\AbstractRepository;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\DistributionList;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Domain;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 10.09.19
 * Time: 08:40
 * Class DistributionList
 */
class DistributionLists extends AbstractRepository
{

    /**
     * @param $domainName
     * @return mixed
     */
    public function getAllDistributionListsByDomain($domainName)
    {
        $domain = new Domain();
        $domain->setName($domainName);

        $result = $this->getClient()->distributionList->getAllDistributionLists($domain);

        $accounts = $result->getResponseBody()['GETALLDISTRIBUTIONLISTSRESPONSE']['DL'];

        /**
         * API return one or araay with accounts
         */
        if(isset($accounts['NAME']))
        {
            $tmpAccount =  new DistributionList($accounts);
            $tmp[$tmpAccount->getId()] = $tmpAccount;
        }else{
            foreach($accounts as $account)
            {
                $tmpAccount =  new DistributionList($account);
                $tmp[$tmpAccount->getId()] = $tmpAccount;
            }
        }

        return $tmp;
    }

    /**
     * @param $id
     * @return bool|DistributionList
     */
    public function getDistributionListBydId($id)
    {
        $list = new DistributionList();
        $list->setId($id);

        return $this->getClient()->distributionList->read($list);
    }

}