<?php

namespace AuthStack\UserDirectory\Model;

use function AuthStack\UserDirectory\routes;
use AuthStack\UserDirectory\Service\RESTService;
use Buckhill\Support\Traits\DotNotationAccessorTrait;

class Group extends AbstractModel
{
    use DotNotationAccessorTrait;

    protected $service;

    /**
     * @return int
     */
    public function getID(): int
    {
        return (int)$this->dotNotationFromArray($this->model, 'id');
    }

    /**
     * @return Group
     */
    public function create(): Group
    {
        return $this->__model($this->getService()->create($this->getModel()));
    }

    /**
     * @param int $id
     * @return Group
     */
    public function read(int $id)
    {
        return $this->__model($this->getService()->read($id));
    }

    /**
     * @param array $model
     * @return bool
     */
    public function update(array $model)
    {
        $result = $this->getService()->update($this->getID(), $model);

        $this->model = array_merge($this->model, $model);

        return $result;
    }

    /**
     * @return bool
     */
    public function delete(): bool
    {
        return $this->getService()->delete($this->getID());
    }

    /**
     * @param array $options
     * @return array
     */
    public function listing(array $options = []): array
    {
        return $this->getService()->listing($options);
    }

    /**
     * @param int $parent_id
     * @return bool
     */
    public function setParentGroup(int $parent_id): bool
    {
        return $this->getService()->setParent($this->getID(), $parent_id);
    }

    /**
     * @param array $options
     * @return array
     */
    public function getMembers(array $options = []): array
    {
        return $this->getService()->getMembers($this->getID(), $options);
    }

    /**
     * @param int $parent_id
     * @return Group
     */
    public function setParentID(int $parent_id): self
    {
        $this->model['parent_id'] = $parent_id;

        return $this;
    }

    public function getParentID()
    {
        return $this->dotNotationFromArray($this->model, 'parent_id');
    }

    /**
     * @param int $org_id
     * @return Group
     */
    public function setOrganizationID(int $org_id): self
    {
        $this->model['organization_id'] = $org_id;

        return $this;
    }

    public function getOrganizationID()
    {
        return $this->dotNotationFromArray($this->model, 'organization_id');
    }

    /**
     * @param int $depth
     * @return Group
     */
    public function setDepth(int $depth = 0): self
    {
        $this->model['depth'] = $depth;

        return $this;
    }

    public function getDepth()
    {
        return $this->dotNotationFromArray($this->model, 'depth');
    }

    /**
     * @param string $title
     * @return Group
     */
    public function setTitle(string $title): self
    {
        $this->model['title'] = $title;

        return $this;
    }

    public function getTitle()
    {
        return $this->dotNotationFromArray($this->model, 'title');
    }

    /**
     * @param string $description
     * @return Group
     */
    public function setDescription(string $description): self
    {
        $this->model['description'] = $description;

        return $this;
    }

    public function getDescription()
    {
        return $this->dotNotationFromArray($this->model, 'description');
    }

    /**
     * @param array $additional
     * @return Group
     */
    public function setAdditional(array $additional = []): self
    {
        $this->model['additional'] = $additional;

        return $this;
    }

    /**
     * @param string $path
     * @param null $default
     * @return array|mixed|null
     */
    public function getAdditional(string $path, $default = null)
    {
        return $this->dotNotationFromArray($this->model, sprintf("additional.%s", $path), $default);
    }

    /**
     * @param \AuthStack\UserDirectory\Service\Group $service
     * @return $this
     */
    public function setService(RESTService $service)
    {
        $this->service = $service;

        return $this;
    }

    /**
     * @return \AuthStack\UserDirectory\Service\Group
     */
    public function getService(): RESTService
    {
        if(!$this->service instanceof \AuthStack\UserDirectory\Service\Group)
        {
            $baseUrl = $this->baseUrl;
            $authType = $this->auth_type;
            $token = $this->auth_token;

            $routes = routes($baseUrl, $authType, $token);

            $this->service = new \AuthStack\UserDirectory\Service\Group($baseUrl, $routes['options'], $routes['routes']['group']);

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

        return $this->service;
    }

    /**
     * @param Group $model
     * @return Group
     */
    protected function __model(Group $model): Group
    {
        $this->model = $model->getModel();

        return $model->setService($this->getService());
    }
}