#|

src/web/cards.lisp

Card Browser

TODO Link To Inventory Item
TODO This needs to be rewritten using YGOProDeck 2 still.

|#

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

(defroute ("/cards" :method :GET) (&key _parsed)
  "Card Browser Main Route. We use RENDER-RESULTS from `/inventory` URL.

TODO Cards should probably have their own RENDER-RESULTS. RENDER-CARD?"
  (v:info :cards "GET /cards => ~a" _parsed)

  (with-logged-in-user
    (render-results :active "/cards"
                    :class 'ygo-info
                    :params _parsed
                    :tpl #P"cards/index.html")))

(defroute ("/cards/search" :method :POST) (&key _parsed)
  "Card Browser Search POST Route. We also user RENDER-RESULTS here."
  (v:info :cards "POST /cards/search => ~a" _parsed)

  (with-logged-in-user
    (render-results :active "/cards"
                    :class 'ygo-info
                    :params _parsed
                    :tpl #P"cards/search-results.html")))

(defroute ("/cards/:id/view" :method :GET) (&key id)
  "View Route for Card ID. We use YGO-SELECT-INFO-BY-ID to get the PASSCODE to get the YGO-SET-BY-PASSCODE."
  (v:info :cards "GET /cards/~d/view" id)

  (with-logged-in-user
    (handler-case
        (ratify:with-parsed-forms
            ((:integer id))
          (with-connection (db)
            (with-transaction
              (let* ((variants (select-variant-condition))
                     (ygo-info (ygo-select-info-by-id id))
                     (passcode (ygo-passcode-of ygo-info))
                     (found (ygo-card-sets passcode)))
                (if found
                    (render-with-env #P"cards/view.html"
                                     `(:ygo-card-sets ,found
                                       :variants ,variants)))))))
      (ratify:combined-error (e)
        (flash-error (format nil "/cards/~d/view.error => ~a~%" id e))))))

(defroute ("/cards/by-passcode/:id" :method :GET) (&key id)
  "View YGO-INFO by PASSCODE ID."
  (alexandria:if-let ((info (find-dao 'ygo-info :passcode id)))
    (redirect (format nil "/cards/~d/view" (mito:object-id info)))
    (redirect "/cards")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This is bad, but could be better for other things
(defroute ("/cards/:id/image" :method :GET) (&key id)
  "Get the card image for card ID.

TODO Rewrite with templates
TODO This is bad, the way that it's written right now it will take one ID, ideally, this should wrap a list of IDs to save on database bandwidth. Each request triggers a FIND-DAO."
  (v:debug :cards "GET /cards/~d/image" id)
  
  (with-logged-in-user
    (handler-case
        (ratify:with-parsed-forms
            ((:integer id))
          (let ((found (find-dao 'ygo-info :id id)))
            (if found
                (format nil
                        "<img class=\"w3-image w3-card-4 zoom\" src=\"/public/ygoprodeck/~a.jpg\" alt=\"Card Image for Yu-Gi-Oh! ~a - ~a\">"
                        (ygo-passcode-of found)
                        (ygo-passcode-of found)
                        (name-of found))))))))

(defroute ("/cards/by-passcode/:id/price" :method :GET) (&key id)
  "Get the YGO-PRICE info for this PASSCODE ID. The pricing info comes from YGOProDeck."
  (with-logged-in-user
    (handler-case
        (ratify:with-parsed-forms
            ((:integer id))
          (let ((found (find-dao 'ygo-price :passcode-id id)))
            (render-with-env #P"cards/ygo-price.html"
                             (list :price found)))))))
