#|

src/web/qr.lisp

QR Generator

New style web page with DEFCLASS brings some challenges: APPLY
#'FN e.g. MAKE-INSTANCE usually expects a PLIST e.g. :SLOT VALUE,
however, rendering with HTMX/HTML will require (SYMBOL-VALUE
:SLOT) e.g. "8-BIT-BYTE" which makes matching them up
tricky. Perhaps this is a solved problem.

|#

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

(defparameter *qr-settings* (make-instance 'qr-settings)
  "The saved settings for the QR module.")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defroute ("/qr" :method :GET) ()
  "QR Generator Main Route. Display the index page. Login Required."
  (v:info :qr "GET /qr")

  (with-logged-in-user
    (setf (getf (response-headers *response*) :cache-control) "no-cache, must-revalidate")
    (render-with-env #P"qr.html"
                     (append
                      (list :active "/qr")
                      (qr-to-plist *qr-settings* t)))))

(defroute ("/qr" :method :POST) (&key _parsed)
  "QR Generator Main Route. Update the *QR-SETTINGS* and display the result. Login Required."
  (v:info :qr "POST /qr => ~a" _parsed)
  
  (with-logged-in-user
    (alexandria:if-let ((settings (reinitialize-instance *qr-settings* :content _parsed)))
      (alexandria:if-let ((html (qr-generate settings)))
        html
        (_ "Something went wrong. Try again?"))
      (setf *qr-settings* (make-instance 'qr-settings)))))
