<?php

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


use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\AbstractModel;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 10.09.19
 * Time: 08:43
 * Class DistributionList
 */
class DistributionList extends AbstractModel
{
    const ATTR_DISPLAY_NAME             = 'displayName';
    const ATTR_DESCRIPTION              = 'description';
    const ATTR_SUBSCRIPTION_REQUEST     = 'zimbraDistributionListSubscriptionPolicy';
    const ATTR_UNSUBSCRIPTION_REQUEST   = 'zimbraDistributionListUnsubscriptionPolicy';
    const ATTR_NOTIFY_SHARES            = 'zimbraDistributionListSendShareMessageToNewMembers';
    const ATTR_HIDE_IN_GAL              = 'zimbraHideInGal';
    const ATTR_MAIL_STATUS              = 'zimbraMailStatus';
    const REPLY_TO_ENABLED              = 'zimbraPrefReplyToEnabled';
    const REPLY_TO_DISPLAY              = 'zimbraPrefReplyToDisplay';
    const REPLY_TO_ADDRESS              = 'zimbraPrefReplyToAddress';

    protected $name;
    protected $id;
    protected $attrs;
    protected $dynamic = false;
    protected $members = [];
    protected $owners  = [];
    protected $alias   = null;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param $id
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return bool
     */
    public function isDynamic()
    {
        return $this->dynamic;
    }

    /**
     * @param bool $dynamic
     */
    public function setDynamic($dynamic)
    {
        $this->dynamic = $dynamic;
    }



    /**
     * @return mixed
     */
    public function getAttrs()
    {
        return $this->attrs;
    }

    /**
     * @param mixed $attrs
     */
    public function setAttrs($attrs)
    {
        $this->attrs = $attrs;
    }

    /**
     * @param $key
     * @param null $value
     */
    public function setAttr($key, $value = null)
    {
        $this->attrs[$key] = $value;
    }

    /**
     * @param $member
     * @return $this
     */
    public function addMember($member)
    {
        $this->members[$member] = $member;
        return $this;
    }

    /**
     * @param array $members
     * @return $this
     */
    public function setMembers($members = [])
    {
        $this->members = $members;
        return $this;
    }

    /**
     * @return array
     */
    public function getMembers()
    {
        return $this->members;
    }

    /**
     * @param array $owners
     * @return $this
     */
    public function setOwners($owners = [])
    {
        $this->owners = $owners;
        return $this;
    }

    /**
     * @param $owner
     * @return $this
     */
    public function addOwner($owner)
    {
        $this->owners[$owner] = $owner;
        return $this;
    }

    /**
     * @return array
     */
    public function getOwners()
    {
        return $this->owners;
    }

    /**
     * @param $owner
     * @return $this
     */
    public function setAlias($alias)
    {
        $this->alias = $alias;
        return $this;
    }

    /**
     * @return array
     */
    public function getAlias()
    {
        return $this->alias;
    }

    /**
     * @return mixed
     */
    public function getResourceMembers()
    {
        if($this->resources['dlm']['DATA'])
        {
            $tmp[$this->resources['dlm']['DATA']] = $this->resources['dlm']['DATA'];
            return $tmp;
        }

        foreach($this->resources['dlm'] as $owner)
        {
            $tmp[$owner['DATA']] = $owner['DATA'];
        }

        return $tmp;
    }

    /**
     * @return mixed
     */
    public function getResourceOwners()
    {
        $owners = $this->resources['owners'] ? $this->resources['owners'] : $this->owners;

        /**
         *
         */
        if($owners['OWNER']['NAME'])
        {
            $tmp[$owners['OWNER']['NAME']] = $owners['OWNER']['NAME'];
            return $tmp;
        }

        /**
         *
         */
        foreach($owners['OWNER'] as $owner)
        {

            $tmp[$owner['NAME']] = $owner['NAME'];
        }

        return $tmp;
    }

    /**
     * @return mixed
     */
    public function getResourceAliases()
    {
        foreach($this->getDataResourceACollection('zimbraMailAlias') as $alias)
        {
            if(isset($alias['DATA']) && $this->getName() !== $alias['DATA'])
            {
                $tmp[$alias['DATA']] = $alias['DATA'];

            }elseif($this->getName() !== $alias){
                $tmp[$alias] =$alias;
            }

        }

        return $tmp;
    }

}