<?php

/**
 * OpenKM, Open Knowledge Management System S.L.  (http://www.openkm.com)
 * Copyright (c) 2006-2023
 *
 * No bytes were intentionally harmed during the development of this application.
 *
 * This program is commercial software; you can use it under the terms of the
 * EULA - OpenKM SDK End User License Agreement as published by OpenKM Knowledge Management System S.L.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * EULA - OpenKM SDK End User License Agreement for more details:
 * http://docs.openkm.com/kcenter/view/licenses/eula-openkm-sdk-end-user-license-agreement.html
 */

namespace openkm\impl;

use Httpful\Exception\ConnectionErrorException;
use Httpful\Request;
use Httpful\Mime;
use openkm\definition\BaseTask;
use openkm\exception\AccessDeniedException;
use openkm\exception\DatabaseException;
use openkm\exception\PathNotFoundException;
use openkm\exception\RepositoryException;
use openkm\exception\UnknowException;
use openkm\util\Status;
use openkm\util\TypeException;
use openkm\util\UriHelper;

/**
 * TaskImpl
 *
 * @author sochoa
 */
class TaskImpl extends ClientImpl implements BaseTask {

    public function getAssignedTasks($projectId, $typeId, $statusId, $orderColumn, $orderAsc, $offset,
                                     $limit, $from, $to, $subject) {

    }

    public function getActiveTasks($projectId, $typeId, $statusId, $orderColumn, $orderAsc, $offset,
                                   $limit, $from, $to, $subject) {

    }

    public function getFinishedTasks($projectId, $typeId, $statusId, $orderColumn, $orderAsc, $offset,
                                     $limit, $from, $to, $subject) {

    }

    public function getNotifiedTasks($projectId, $typeId, $statusId, $orderColumn, $orderAsc, $offset,
                                     $limit, $from, $to, $subject) {

    }

    public function getTaskStatuses() {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::TASK_GET_STATUSES);
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                $taskStatuses = [];
                foreach ($response->body as $taskStatus) {
                    $taskStatuses[] = $this->phpTaskStatus($taskStatus);
                }
                return $taskStatuses;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException($restException->getMessage());
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function getTaskProjects($filterActive) {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::TASK_GET_PROJECTS_ACTIVE);
            if ($filterActive == true) {
                $uri = str_replace('{0}', 'true', $uri);
            } else {
                $uri = str_replace('{0}', 'false', $uri);
            }
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                $taskProjects = [];
                foreach ($response->body as $taskProjectJson) {
                    $taskProjects[] = $this->phpTaskProject($taskProjectJson);
                }
                return $taskProjects;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException($restException->getMessage());
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function getTaskTypes($filterActive) {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::TASK_GET_TYPES_ACTIVE);
            if ($filterActive == true) {
                $uri = str_replace('{0}', 'true', $uri);
            } else {
                $uri = str_replace('{0}', 'false', $uri);
            }
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                $taskTypes = [];
                foreach ($response->body as $taskTypeJson) {
                    $taskTypes[] = $this->phpTaskType($taskTypeJson);
                }
                return $taskTypes;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException($restException->getMessage());
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function getAssignedTasksCount($statusId, $projectId, $typeId) {

    }

    public function getActiveTasksCount($statusId, $projectId, $typeId) {

    }

    public function getFinishedTasksCount($statusId, $projectId, $typeId) {

    }

    public function getNotifiedTasksCount($statusId, $projectId, $typeId) {

    }

    public function createTask($subject, $start, $end, $description, $statusId, $projectId,
                               $typeId, $user, $notificationUsers, $externalUsers, $relatedDocuments,
                               $relatedFolders, $relatedRecords, $relatedMails, $repeatExpression,
                               $repeatUntil, $formatDate, $repeatTimes, $reminderStartUnit, $reminderStartValue,
                               $reminderEndUnit, $reminderEndValue){
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::TASK_POST_CREATE);
            $params = [];
            $params['subject'] = $subject;
            $params['start'] = $start;
            $params['end'] = $end;
            $params['description'] = $description;
            $params['statusId'] = $statusId;
            $params['projectId'] = $projectId;
            $params['typeId'] = $typeId;
            $params['repeatExpression'] = $repeatExpression;
            $params['repeatUntil'] = $repeatUntil;
            $params['formatDate'] = $formatDate;
            $params['repeatTimes'] = $repeatTimes;
            $params['reminderStartUnit'] = $reminderStartUnit;
            $params['reminderStartValue'] = $reminderStartValue;
            $params['reminderEndUnit'] = $reminderEndUnit;
            $params['reminderEndValue'] = $reminderEndValue;
            $params['userId'] = $user;
            $params['notificationUsersId'] = $notificationUsers;
            $params['externalUser'] = $externalUsers;
            $params['docsId'] = $relatedDocuments;
            $params['fldsId'] = $relatedFolders;
            $params['recsId'] = $relatedRecords;
            $params['mailsId'] = $relatedMails;

            $client = Request::post($uri);
            $client->body($params, Mime::FORM);
            $client->addHeader('Authorization', $this->authorizationToken);
            $client->expects(Mime::JSON);
            $response = $client->send();
            if ($response->code == Status::OK) {
                return $this->phpTask($response->body);
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException($restException->getMessage());
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function updateTask($taskId, $subject, $start, $end, $description, $statusId,
                               $projectId, $typeId, $user, $notificationUsers, $externalUsers,
                               $relatedDocuments, $relatedFolders, $relatedRecords, $relatedMails,
                               $owner, $repeatExpression, $repeatUntil, $formatDate, $repeatTimes, $progress,
                               $reminderStartUnit, $reminderStartValue, $reminderEndUnit, $reminderEndValue){

    }

    public function getTask($taskId) {

    }

    public function deleteTask($taskId) {

    }

    public function createTaskProject($name, $active, $description) {

    }

    public function updateTaskProject($projectId, $active, $name, $description){

    }

    public function deleteTaskProject($projectId) {

    }

    public function getTaskProject($projectId) {

    }

    public function createTaskType($name, $active, $description) {

    }

    public function updateTaskType($typeId, $active, $name, $description) {

    }

    public function deleteTaskType($typeId) {

    }

    public function getTaskType($typeId) {

    }

    public function createTaskStatus($name, $finish) {

    }

    public function updateTaskStatus($statusId, $name, $finish) {

    }

    public function deleteTaskStatus($statusId) {

    }

    public function getTaskStatus($statusId) {

    }

    public function getTaskNotes($taskId) {

    }

    public function createTaskNote($taskId, $text) {

    }

    public function updateTaskNote($noteId, $text) {

    }

    public function deleteTaskNote($noteId) {
        
    }
}

?>
