<?php

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

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;

class ExampleMail {

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

    private $ws;

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

    public function testCreateMail() {
        try {
            $mail = new \openkm\bean\Mail();
            // Mail path = msgId + escaped(subject)
            $msgId = "2937b81d-0b10-4dd0-a426-9acbd80be1c9";
            $subject = "some subject";
            $mailPath = "/okm:root/SDK4PHP/" . $msgId . "-" . $this->escape($subject);
            $mail->setPath($mailPath);
            // Other format for mail "some name <no_reply@openkm.com>"
            $mail->setFrom('<no_reply@openkm.com>');
            $mail->setTo(['anonymous@gmail.com', 'anonymous1@gmail.com']);
            // You should set real dates
            $mail->setSentDate(date('Y-m-d'));
            $mail->setReceivedDate(date('Y-m-d'));
            $mail->setContent('some content');
            $mail->setMimeType(\openkm\bean\Mail::MIME_TEXT);
            $mail->setSubject($subject);
            $mail->setOrigin(\openkm\bean\Mail::ORIGIN_API);
            // Get only as an approximation of real size for these sample
            $mail->setSize(strlen($mail->toString()));
            var_dump($this->ws->createMail($mail));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    private function escape($name) {
        $ret = $this->cleanup($name);
        return htmlentities($ret);
    }

    private function cleanup($name) {
        $ret = str_replace('/', '', $name);
        $ret = str_replace('*', '', $ret);
        $ret = str_replace('\\s+', ' ', $ret);
        return $ret;
    }

    public function testGetMailProperties() {
        try {
            $mail = $this->ws->getMailProperties('520f4d98-1855-4784-9bc3-01ba5b440c1d');
            var_dump($mail);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteMail() {
        try {
            $this->ws->deleteMail('520f4d98-1855-4784-9bc3-01ba5b440c1d');
            echo 'deleted';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRenameMail() {
        try {
            $mail = $this->ws->renameMail('520f4d98-1855-4784-9bc3-01ba5b440c1d', 'new name');
            var_dump($mail);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testMoveMail() {
        try {
            $this->ws->moveMail('520f4d98-1855-4784-9bc3-01ba5b440c1d', '/okm:root/SDK4PHP/tmp');
            echo 'move';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetMailChildren() {
        try {
            $mails = $this->ws->getMailChildren('/okm:root/SDK4PHP');
            foreach ($mails as $mail) {
                var_dump($mail);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testIsValidMail() {
        try {
            //Return false
            var_dump($this->ws->isValidMail('/okm:root/SDK4PHP/logo.png'));
            //Return true
            var_dump($this->ws->isValidMail('4b43c647-4900-4fdd-b547-00f3f2d48c3b'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetMailPath() {
        try {
            var_dump($this->ws->getMailPath('520f4d98-1855-4784-9bc3-01ba5b440c1d'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testCreateAttachment() {
        try {
            $fileName = dirname(__FILE__) . '/files/logo.png';
            $docName = 'logo.png';
            $document = $this->ws->createAttachment('520f4d98-1855-4784-9bc3-01ba5b440c1d', $docName, file_get_contents($fileName));
            var_dump($document);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteAttachment() {
        try {
            $this->ws->deleteAttachment('520f4d98-1855-4784-9bc3-01ba5b440c1d', '73ba1f80-4769-492b-ab15-0ecfbb15d6ac');
            echo 'deleted attachment';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetAttachments() {
        try {
            $documents = $this->ws->getAttachments('520f4d98-1855-4784-9bc3-01ba5b440c1d');
            foreach ($documents as $document) {
                var_dump($document);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testPurgeMail() {
        try {
            $this->ws->purgeMail('520f4d98-1855-4784-9bc3-01ba5b440c1d');
            echo 'purge';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testCopyMail() {
        try {
            $this->ws->copyMail('520f4d98-1855-4784-9bc3-01ba5b440c1d', '/okm:root/SDK4PHP/tmp', 'new_name');
            echo 'copyMail';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testExtendedMailCopy() {
        try {
            $this->ws->extendedMailCopy('3c19ceb2-9c6a-4b43-aabb-7f169127022a', '/okm:root/SDK4PHP/tmp', true, true, true, true, true, 'new_name_copy');
            echo 'extendedMail copy';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSendMailWithAttachments() {
        try {
            $to = ['gnu.java.sergio@gmail.com'];
            $cc = ['melcita.claudia@gmail.com '];
            $bcc = [ 'ares.mj23@gmail.com '];
            $docsId = ['50828cb6-71db-49af-aa56-9ee8df63fd79'];
            $this->ws->sendMailWithAttachments('sochoa@openkm.com', $to, $cc, $bcc, 'some suject', 'some body', $docsId, '/okm:root/import');
            echo 'sendMailWithAttachemetns';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testImportEml() {
        try {
            $fileName = dirname(__FILE__) . '/files/test.eml';            
            $mail = $this->ws->importEml('768748c5-04ab-4921-b0d4-7545d239ce5a', 'some title', file_get_contents($fileName));
            var_dump($mail);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testImportMsg() {
        try {
            $fileName = dirname(__FILE__) . '/files/test.eml';            
            $mail = $this->ws->importEml('702e456a-3145-4dd5-831d-01647a9f2608', 'some title', file_get_contents($fileName));
            var_dump($mail);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetMailTitle() {
        try {
            $this->ws->setMailTitle('3c19ceb2-9c6a-4b43-aabb-7f169127022a', 'some title');
            echo 'mail title';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSendMail() {
        try {
            $recipients = array();
            $recipients[] = 'gnu.java.sergio@gmail.com';
            $recipients[] = 'sochoa@openkm.com';
            $this->ws->sendMail('Testing sending mail from OpenKM', 'Test message body.', $recipients);
            echo 'send Mail';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleMail = new ExampleMail();
$exampleMail->testIsValidMail();
?>