#|

src/web/tinymce.lisp

TinyMCE Hosting

TODO Handle POST Route

|#

(in-package #:cl-user)

(defpackage #:cl-deck-builder2.web.tinymce
  (:use #:cl)
  (:import-from #:caveman2
                #:<app>
                #:defroute
                #:clear-routing-rules)
  (:import-from #:cl-deck-builder2.web
                #:render-with-env)
  (:local-nicknames (#:v #:org.shirakumo.verbose))
  (:export #:*web*))

(in-package #:cl-deck-builder2.web.tinymce)

(defclass <web> (<app>) ())
(defvar *web* (make-instance '<web>))
(clear-routing-rules *web*)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass mce-document ()
  ((name :initarg :name)
   (path :initarg :path)
   (body :initarg :body)))

(defvar *current-document* (make-instance 'mce-document :name "test" :path #P"/tmp/test"))

(defun update-current-document (name body)
  (reinitialize-instance *current-document* :name name
                                            :body body))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defroute ("/" :method :GET) (&key _parsed)
  "TinyMCE Index Page"

  ;; (declare (ignore _parsed))
  (v:info :tinymce "GET / ~a" _parsed)

  (render-with-env #P"tinymce/index.html"
                   `(:active ,"/tinymce"
                     :file ,*current-document*)))

(defroute ("/" :method :POST) (&key |mode| |filename| |default| _parsed)
  "TinyMCE POST Route: Handle User Input"

  (v:info :tinymce "POST / ~a" _parsed)

  (when (string= |mode| "create")
    (update-current-document |filename| |default|))

  (princ-to-string |default|))
