<?php

include '../src/openkm/OpenKM.php';

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;
use openkm\bean\Note;

class ExampleNote {

    const HOST = "http://localhost:8080/openkm/";
    const USER = "okmAdmin";
    const PASSWORD = "admin";

    private $ws;

    public function __construct() {
        $this->ws = OKMWebServicesFactory::build(self::HOST);
    }

    public function testAddNote() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $note = $this->ws->note->addNote("d701c503-87fc-4a9e-829e-478dc375eb83", "the note text");
            var_dump($note);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetNote() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $notes = $this->ws->note->listNotes("7e12a9ff-95b2-4945-bf5f-1bb709d89c14");
            if (count($notes) > 0) {
                var_dump($this->ws->note->getNote($notes[0]->getPath()));
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteNote() {
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $notes = $this->ws->note->listNotes("7e12a9ff-95b2-4945-bf5f-1bb709d89c14");
            if (count($notes) > 0) {
                $this->ws->note->deleteNote($notes[0]->getPath());
                echo "deleted";
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetNote() {
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $notes = $this->ws->note->listNotes("7e12a9ff-95b2-4945-bf5f-1bb709d89c14");
            if (count($notes) > 0) {
                $this->ws->note->setNote($notes[0]->getPath(), "text modified");
                echo "updated";
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testListNotes() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $notes = $this->ws->note->listNotes("7e12a9ff-95b2-4945-bf5f-1bb709d89c14");
            foreach ($notes as $note) {
                var_dump($note);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetNotesHistory() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $notesHistory = $this->ws->note->getNotesHistory("7e12a9ff-95b2-4945-bf5f-1bb709d89c14");
            foreach ($notesHistory as $noteHistory) {
                var_dump($noteHistory);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleNote = new ExampleNote();
$exampleNote->testAddNote();
//$exampleNote->testGetNote();
//$exampleNote->testDeleteNote();
//$exampleNote->testSetNote();
//$exampleNote->testListNotes();
//$exampleNote->testGetNotesHistory();
?>