#|

Legacy Code

Scheduled For Deletion

YGOProDeck v1 JSON Importer

|#

(in-package #:cl-user)

(defpackage #:cl-deck-builder2.models.ygoprodeck.json
  (:use :cl
        :cl-deck-builder2.db
        :cl-deck-builder2.models.ygoprodeck)
  (:local-nicknames
   (#:v #:org.shirakumo.verbose))
  (:export :json-import-cardinfo))

(in-package #:cl-deck-builder2.models.ygoprodeck.json)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *json* nil
  "The currently loaded \"cardinfo.php\" data pulled from YGOProDeck
API. You may have to munge it into \"cardinfo.json\" with jq:

    $ jq . < cardinfo.php > cardinfo.json
")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GET-NEW-CARDINFO got renamed to YGOPRODECK2:FETCH-CARDINFO.
;; Actually, basically everything got moved into YGOPRODECK2. This is
;; legacy code.

  
(defun json-import-cardinfo (path &optional (force nil))
  (load-cardinfo path force)

  (let ((lst ()))
    ;; CARD is an ALIST
    (dolist (card *json*)
      (let (;; Banlist_Info: Unused
            ;; (banlist-info (assoc-utils:aget card :banlist--info))

            ;; Card Images: Variant Artwork
            ;; Skip the first one with REST
            (card-images (rest (assoc-utils:aget card :card--images)))

            ;; make a copy: we munge it
            (card-prices (copy-alist (assoc-utils:aget card :card--prices)))
            ;; make a copy: we munge it
            (card-sets (copy-alist (assoc-utils:aget card :card--sets)))
            ;; Link Markers: Unused
            ;; (linkmarkers (assoc-utils:aget card :linkmarkers))

            ;; make a copy: we munge it
            (card-misc-info (copy-alist (assoc-utils:aget card :misc--info)))

            ;; '((:banlist--info)
            ;;   (:card--images)
            ;;   (:card--prices)
            ;;   (:card--sets)
            ;;   (:linkmarkers)
            ;;   (:misc--info))

            (passcode (assoc-utils:aget card :id))
            (base-fields (assoc-utils:alist-plist
                          (json-cardinfo-base-fields card))))

        ;; First import all the base data
        (push (apply #'make-instance 'ygo-info base-fields) lst)

        ;; Card images, variant artwork, I'm not sure how to handle
        ;; it. Here's a dirty hack! If there are any variant images
        ;; with passcodes, just create a duplicate entry with the same
        ;; data from the parent. I'm sure a separate table is a better
        ;; way to handle this information, but I don't know how to do
        ;; that right now!
        (dolist (card-image card-images)
          (let ((id (assoc-utils:aget card-image :id)))
            (setf (getf base-fields :passcode) id)
            (push (apply #'make-instance 'ygo-info base-fields) lst)))

        ;; TODO Ban List Info
        ;; Append :passcode to all banlist-info for this card
        ;; (dolist (banlist banlist-info)
        ;;   (setf banlist (acons :passcode passcode banlist))
        ;;   (apply #'mito:create-dao 'banlist-info
        ;;          (assoc-utils:alist-plist banlist)))

        ;; Append :passcode to all card-prices for this card
        (dolist (card-price card-prices)
          (setf card-price (acons :passcode passcode card-price)
                ;; Data Transformation
                (car (assoc :cardmarket--price card-price)) :cardmarket
                (car (assoc :tcgplayer--price card-price)) :tcgplayer
                (car (assoc :ebay--price card-price)) :ebay
                (car (assoc :amazon--price card-price)) :amazon
                (car (assoc :coolstuffinc--price card-price)) :coolstuffinc)
          (push (apply #'make-instance 'ygo-price (assoc-utils:alist-plist card-price))
                lst))

        ;; Append :passcode to all card-sets for this card
        (dolist (card-set card-sets)
          (setf card-set (acons :passcode passcode card-set)
                ;; Data Transformation
                (car (assoc :set--name card-set)) :name
                (car (assoc :set--code card-set)) :code
                (car (assoc :set--rarity card-set)) :rarity
                (car (assoc :set--rarity--code card-set)) :rarity-code
                (car (assoc :set--price card-set)) :price)
          (push (apply #'make-instance 'ygo-set (assoc-utils:alist-plist card-set))
                lst))

        ;; misc_info + misc_info.formats
        ;; (dolist (misc-info card-misc-info)
        ;;   (setf card-set (acons :passcode passcode card-set)
        ;;         ;; Data Transformation
        ;;         (car (assoc :set--name card-set)) :name
        ;;         (car (assoc :set--code card-set)) :code
        ;;         (car (assoc :set--rarity card-set)) :rarity
        ;;         (car (assoc :set--rarity--code card-set)) :rarity-code
        ;;         (car (assoc :set--price card-set)) :price)
        ;;   (push (apply #'make-instance 'ygo-set (assoc-utils:alist-plist card-set))
        ;;         lst))

        ))
    (do-grouped-insert lst)))

;; TODO linkmarkers
;; Append :passcode to all linkmarkers for this card
;; (dolist (linkmarker linkmarkers)
;;   (setf linkmarker (acons :passcode passcode linkmarker))
;; Data Transformation
;;   (apply #'mito:create-dao 'linkmarker
;;          (assoc-utils:alist-plist linkmarker)))
