<?php

/**
 * OpenKM, Open Knowledge Management System S.L.  (http://www.openkm.com)
 * Copyright (c) 2006-2018
 *
 * 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 openkm\bean\Note;
use openkm\definition\BaseNote;
use openkm\exception\AccessDeniedException;
use openkm\exception\DatabaseException;
use openkm\exception\LockException;
use openkm\exception\PathNotFoundException;
use openkm\exception\RepositoryException;
use openkm\exception\UnknowException;
use openkm\util\Status;
use openkm\util\TypeException;
use openkm\util\UriHelper;
use openkm\util\FormatUtils;

/**
 * NoteImpl
 *
 * @author sochoa
 */
class NoteImpl extends ClientImpl implements BaseNote {

    /**
     * Add note to a node
     * @param string $uuid The uuid of the document, folder, mail or record
     * @param string $text The text
     * @return Note Return an object Note.
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws UnknowException
     */
    public function addNote($uuid, $text) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::NOTE_POST_ADD, ['uuid' => $uuid]));
            $client = Request::post($uri);

            $params = [];
            $params['text'] = $text;
            $response = $this->getClientWithṔarams($client, $params);
            if ($response->code == Status::OK) {
                return $this->phpNote($response->body);
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($error == 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 if ($restException->getSimpleName() == TypeException::LOCK_EXCEPTION) {
                        throw new LockException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Retrieves the note.
     * 
     * @param string $noteId The noteId is an UUID. The object Node have a variable named path, in that case the path contains an UUID.
     * @return Note Return un object Note.
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws UnknowException
     */
    public function getNote($noteId) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::NOTE_GET, ['noteId' => $noteId]));
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                return $this->phpNote($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->getSimpleName());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getSimpleName());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getSimpleName());
                    } else if ($restException->getSimpleName() == TypeException::LOCK_EXCEPTION) {
                        throw new LockException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Delete a note
     * 
     * @param string $noteId The noteId is an UUID. The object Node have a variable named path, in that case the path contains an UUID.
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws UnknowException
     */
    public function deleteNote($noteId) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::NOTE_DELETE, ['noteId' => $noteId]));
            $client = Request::delete($uri);
            $response = $this->getClient($client);
            if ($response->code != Status::OK) {
                $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 if ($restException->getSimpleName() == TypeException::LOCK_EXCEPTION) {
                        throw new LockException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Change the note text.
     * 
     * @param string $noteId The noteId is an UUID. The object Node have a variable named path, in that case the path contains an UUID.
     * @param string $text The text
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws UnknowException
     */
    public function setNote($noteId, $text) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::NOTE_PUT_SET, ['noteId' => $noteId]));
            $client = Request::put($uri);
            $params = [];
            $params['text'] = $text;
            $response = $this->getClientWithṔarams($client, $params);
            if ($response->code != Status::OK) {
                $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 if ($restException->getSimpleName() == TypeException::LOCK_EXCEPTION) {
                        throw new LockException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Retrieve a list of all notes of a node.
     * 
     * @param string $uuid The uuid of the document, folder, mail or record
     * @return array Return an array of all notes of a node.
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException     
     * @throws UnknowException
     */
    public function listNotes($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::NOTE_GET_LIST, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                $notes = [];
                foreach ($response->body as $noteJSON) {
                    $notes[] = $this->phpNote($noteJSON);
                }
                return $notes;
            } 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("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function getNotesHistory($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::NOTE_GET_HISTORY_LIST, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {                
                $notesHistory = [];
                foreach ($response->body as $noteHistoryJSON) {
                    $notesHistory[] = $this->phpNoteHistory($noteHistoryJSON);
                }
                return $notesHistory;
            } 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("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

}

?>
