<?php

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

ini_set('display_errors', true);
error_reporting(E_ALL);

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;
use openkm\util\PathUtils;

class ExampleDocument {

    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 testCreateDocument() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $fileName = dirname(__FILE__) . '/files/test.pdf';
            $document = $this->ws->document->createDocument('f93ac8df-e83b-487f-9a14-0af9e910a818', 'test.pdf', file_get_contents($fileName));
            var_dump($document);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteDocument() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->document->deleteDocument('5b37c508-b2be-4429-ba5e-acc96089c6a9');
            echo 'deleted';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetDocumentProperties() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $document = $this->ws->document->getDocumentProperties('7e12a9ff-95b2-4945-bf5f-1bb709d89c14');
            var_dump($document);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetContent($method) {
        $this->ws->login(self::USER, self::PASSWORD);
        $content = $this->ws->document->getContent('b02877cc-bc71-4be9-9bfb-2e50b1732fd3');
        switch ($method) {
            case 1:
                $file = fopen(dirname(__FILE__) . '/files/logo_download.png', 'w+');
                fwrite($file, $content);
                fclose($file);
                echo 'download correct';
                break;
            case 2:
                ob_clean();
                $document = $this->ws->document->getDocumentProperties('b02877cc-bc71-4be9-9bfb-2e50b1732fd3');
                $name = PathUtils::getName($document->getPath());
                header("Cache-Control:  maxage=1");
                header("Pragma: public");
                header("Content-Type: " . $document->getMimeType());
                header('Content-Disposition: attachment; filename="' . $name . '"');
                header("Content-Length: " . strlen($content));
                echo $content;
                break;
        }
    }

    public function testGetContentByVersion($method) {
        $this->ws->login(self::USER, self::PASSWORD);
        $content = $this->ws->document->getContentByVersion('b02877cc-bc71-4be9-9bfb-2e50b1732fd3', 1.2);
        switch ($method) {
            case 1:
                $file = fopen(dirname(__FILE__) . '/files/logo_download_version.png', 'w+');
                fwrite($file, $content);
                fclose($file);
                echo 'download correct';
                break;
            case 2:
                $document = $this->ws->document->getDocumentProperties('b02877cc-bc71-4be9-9bfb-2e50b1732fd3');
                header('Expires', 'Sat, 6 May 1971 12:00:00 GMT');
                header('Cache-Control', 'max-age=0, must-revalidate');
                header('Cache-Control', 'post-check=0, pre-check=0');
                header('Pragma', 'no-cache');
                header('Content-Type: ' . $document->getMimeType());
                header('Content-Disposition: attachment; filename="' . substr($document->getPath(), strrpos($document->getPath(), '/') + 1) . '"');
                echo $content;
                break;
        }
    }

    public function testGetExtractedText() {
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $text = $this->ws->document->getExtractedText('7e12a9ff-95b2-4945-bf5f-1bb709d89c14');
            var_dump($text);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetThumbnail($method) {
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $content = $this->ws->document->getThumbnail('7e12a9ff-95b2-4945-bf5f-1bb709d89c14');
            switch ($method) {
                case 1:
                    $file = fopen(dirname(__FILE__) . '/files/thumbnail.png', 'w+');
                    fwrite($file, $content);
                    fclose($file);
                    echo 'download correct';
                    break;
                case 2:
                    header('Expires', 'Sat, 6 May 1971 12:00:00 GMT');
                    header('Cache-Control', 'max-age=0, must-revalidate');
                    header('Cache-Control', 'post-check=0, pre-check=0');
                    header('Pragma', 'no-cache');
                    header('Content-Type: ' . \openkm\util\MimeTypeConfig::MIME_PNG);
                    header('Content-Disposition: attachment; filename="thumbnail.png"');
                    echo $content;
                    break;
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetDocumentChildren() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $documents = $this->ws->document->getDocumentChildren('f93ac8df-e83b-487f-9a14-0af9e910a818');
            foreach ($documents as $document) {
                var_dump($document);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRenameDocument() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $document = $this->ws->document->renameDocument('b02877cc-bc71-4be9-9bfb-2e50b1732fd3', 'logo_rename.png');
            var_dump($document);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testMoveDocument() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->document->moveDocument('b02877cc-bc71-4be9-9bfb-2e50b1732fd3', '55102c84-e4f5-4cbc-a3d1-cf1896367752');
            echo 'moveDocument';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testCopyDocument() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $document = $this->ws->document->copyDocument('b02877cc-bc71-4be9-9bfb-2e50b1732fd3', '55102c84-e4f5-4cbc-a3d1-cf1896367752', 'new_logo.png');
            var_dump($document);
            echo 'copyDocument';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testIsValidDocument() {
        try {
            echo '/okm:root/sdk/logo.png -> ';
            var_dump($this->ws->document->isValidDocument('d701c503-87fc-4a9e-829e-478dc375eb83'));
            echo '<br/>';
            echo '/okm:root -> ';
            var_dump($this->ws->document->isValidDocument('74aa0a29-97f8-4e2e-9c60-880eb627f05e'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }
}

$openkm = new OpenKM(); //autoload
$exampleDocument = new ExampleDocument();

// $exampleDocument->testCreateDocument();
// $exampleDocument->testDeleteDocument();/
// $exampleDocument->testGetDocumentProperties();
// $exampleDocument->testGetContent(2);
// $exampleDocument->testGetContentByVersion(1);
// $exampleDocument->testGetExtractedText();
// $exampleDocument->testGetThumbnail(1);
// $exampleDocument->testGetDocumentChildren();
// $exampleDocument->testRenameDocument();
// $exampleDocument->testMoveDocument();
// $exampleDocument->testCopyDocument();
$exampleDocument->testIsValidDocument();
?>