<?php

namespace AuthStack\UserDirectory\Service;

use AuthStack\UserDirectory\Model\Group as GroupModel;
use AuthStack\UserDirectory\Exceptions\GroupException;

class Group extends RESTService
{
    /**
     * @param array $model
     * @return mixed
     */
    public function create(array $model)
    {
        return $this->remote(function() use (&$model): GroupModel
        {
            $result = $this->json('create', $model);

            return new GroupModel($result);

        }, GroupException::class);
    }

    /**
     * @param int $id
     * @return GroupModel
     */
    public function read(int $id): GroupModel
    {
        return $this->remote(function() use ($id)
        {
            $result = $this->json('read', [], [$id]);

            return new GroupModel($result);

        }, GroupException::class);
    }

    /**
     * @param int $id
     * @param array $model
     * @return bool
     */
    public function update(int $id, array $model): bool
    {
        return $this->remote(function() use ($id, &$model)
        {
            $result = $this->json('update', $model, [$id]);

            return $result['result'];

        }, GroupException::class);
    }

    /**
     * @param int $id
     * @return bool
     */
    public function delete(int $id): bool
    {
        return $this->remote(function() use ($id)
        {
            $result = $this->json('delete', [], [$id]);

            return $result['result'];

        }, GroupException::class);
    }

    /**
     * @param int $id
     * @param int $parent_id
     * @return bool
     */
    public function setParent(int $id, int $parent_id): bool
    {
        return $this->remote(function() use ($id, $parent_id)
        {
            $result = $this->json('change_parent', ['parent_id' => $parent_id], [$id]);

            return $result['result'];

        }, GroupException::class);
    }

    /**
     * @param array $options
     * @return array
     */
    public function listing(array $options = []): array
    {
        return $this->remote(function() use (&$options)
        {
            return $this->json('listing', null, null, $options);
        });
    }

    /**
     * @todo - not implemented! Implement @ server first
     * @param int $id
     * @param array $get_params
     * @return mixed
     */
    public function getMembers(int $id, array $get_params = []): array
    {
        return $this->remote(function() use ($id, $get_params)
        {
            return $this->json('change_parent', [], [$id], $get_params);

        }, GroupException::class);
    }
}