<?php

namespace AuthStack\UserDirectory;

use AuthStack\UserDirectory\Model\Account;
use AuthStack\UserDirectory\Model\Organization;
use AuthStack\UserDirectory\Model\Group;
use AuthStack\UserDirectory\Model\Policy;

class AUD
{
    protected $baseUrl;
    protected $token;
    protected $tz_offset;
    protected $tz_dst;

    protected $account;
    protected $organization;
    protected $group;
    protected $policy;

    /**
     * AUD constructor.
     * @param string|null $baseUrl
     * @param string|null $token
     * @param string|null $tz_offset
     * @param string|null $tz_dst
     */
    public function __construct(string $baseUrl = null, string $token = null, string $tz_offset = null, string $tz_dst = null)
    {
        $this->baseUrl = $baseUrl;
        $this->token = $token;
        $this->tz_offset = $tz_offset;
        $this->tz_dst = $tz_dst;
    }

    /**
     * @param string $url
     * @return AUD
     */
    public function setBaseUrl(string $url): self
    {
        $this->baseUrl = $url;

        return $this;
    }

    /**
     * @param string $token
     * @return AUD
     */
    public function setAuthToken(string $token): self
    {
        $this->token = $token;

        return $this;
    }
    /**
     * @param string $offset
     * @return AUD
     */
    public function setTimeZoneOffset(string $offset): self
    {
        $this->tz_offset = $offset;

        return $this;
    }

    /**
     * @param string $dst
     * @return AUD
     */
    public function setTimeZoneDST(string $dst): self
    {
        $this->tz_dst = $dst;

        return $this;
    }
    /**
     * @param array $model
     * @return Account
     */
    public function account(array $model = []): Account
    {
        if(!$this->account instanceof Account)
        {
            $this->account = (new Account($model))
                ->setBaseUrl($this->baseUrl)
                ->setBasicAuthorization($this->token);

            if(!is_null($this->tz_offset) && !is_null($this->tz_dst))
            {
                $this->account
                    ->setTimeZoneOffset($this->tz_offset)
                    ->setTimeZoneDST($this->tz_dst)
                 ;
            }
        }

        return $this->account;
    }

    /**
     * @param array $model
     * @return Organization
     */
    public function organization(array $model = []): Organization
    {
        if(!$this->organization instanceof Organization)
        {
            $this->organization = (new Organization($model))
                ->setBaseUrl($this->baseUrl)
                ->setBasicAuthorization($this->token);

            if(!is_null($this->tz_offset) && !is_null($this->tz_dst))
            {
                $this->organization
                    ->setTimeZoneOffset($this->tz_offset)
                    ->setTimeZoneDST($this->tz_dst)
                ;
            }
        }

        return $this->organization;
    }

    /**
     * @param array $model
     * @return Group
     */
    public function group(array $model = []): Group
    {
        if(!$this->group instanceof Group)
        {
            $this->group = (new Group($model))
                ->setBaseUrl($this->baseUrl)
                ->setBasicAuthorization($this->token);

            if(!is_null($this->tz_offset) && !is_null($this->tz_dst))
            {
                $this->group
                    ->setTimeZoneOffset($this->tz_offset)
                    ->setTimeZoneDST($this->tz_dst)
                ;
            }
        }

        return $this->group;
    }

    /**
     * @param array $model
     * @return Policy
     */
    public function policy(array $model = []): Policy
    {
        if(!$this->policy instanceof Policy)
        {
            $this->policy = (new Policy($model))
                ->setBaseUrl($this->baseUrl)
                ->setBasicAuthorization($this->token);

            if(!is_null($this->tz_offset) && !is_null($this->tz_dst))
            {
                $this->policy
                    ->setTimeZoneOffset($this->tz_offset)
                    ->setTimeZoneDST($this->tz_dst)
                ;
            }
        }

        return $this->policy;
    }
}