(ns advent.screens.rooms.inside-castle
  (:require [advent.screens.rooms :as rooms]
            [advent.actions :as actions]
            [advent.screens.items :as items]
            [advent.utils :as utils]
            [clojure.zip :as zip]
            [play-clj.core :refer :all]
            [play-clj.ui :refer :all]
            [play-clj.utils :refer :all]
            [play-clj.g2d :refer :all]))

(defn play-warlocks-castle [entities]
  (let [scenarios [#(actions/do-dialogue entities
                                         :game-player "As you approach the lair of the cave beast, a foul odor fills your nostrils."
                                         :game-player "To your horror, you realize that the stench is from the corpses of less wise wizards."
                                         :game-player "The cave is eerie and dark. What do you do?")
                   #(actions/do-dialogue entities
                                         :game-player "As you move forward in your quest, you come upon an abandoned library."
                                         :game-player "Still, something doesn't feel right about the place."
                                         :game-player "It's quiet."
                                         :game-player "A little too quiet.")
                   #(actions/do-dialogue entities
                                         :game-player "The treacherous stair lies before you.")]]
    (doseq [scenario (take 2 (shuffle scenarios))]
      (scenario))))

(defn do-game-player-dialogue [entities]
  (actions/do-dialogue entities :ego "You there!" :game-player "... Yes?")
  (actions/present-choices entities
                           {:choices ["Do you know anything about the sword in the stone up there?"
                                      {:run #(actions/respond entities %
                                                              :game-player "It is said that only he who is worthy in wisdom can pull the sword!"
                                                              :game-player "I, of course have such wisdom, but I'm still unable to pull it myself.")
                                       :choices ["Can you teach me your ways?"
                                                 {:run #(actions/respond entities %
                                                                         :game-player "Ha! My intellect has only been achieved with years of precise training!"
                                                                         :game-player "You're out of luck, friend. Unless you are willing to spend hours and hours in careful study, you will always be a dunce."
                                                                         :ego "...")
                                                  :choices actions/previous-choices}
                                                 "How come?"
                                                 {:run #(actions/respond entities %
                                                                         :game-player "You must be mighty in strength to pull the sword."
                                                                         :game-player "And I spend all of my time either playing my game, or at the library.")
                                                  :choices actions/previous-choices}
                                                 "Something else."
                                                 {:choices actions/something-else}]}
                                      (when (= nil (get-in @entities [:state :current-riddle]))
                                        "Nice trophy.")
                                      {:run #(actions/respond entities %
                                                              :game-player "Thanks. I was nominated the Riddlemaster of Remington for the 7th straight year!"
                                                              :game-player "I guess I'm pretty wise."
                                                              :game-player "And with my sharp intellect, I'm guessing you want my trophy?")
                                       :choices ["Yes."
                                                 {:run #(do (actions/update-state entities (fn [state] (assoc state :current-riddle :wool)))
                                                            (actions/respond entities %
                                                                             :game-player "Well, if you want my trophy, you'll have to earn it."
                                                                             :game-player "There are a few riddles that even I, the wisest in all of Remington, cannot solve."
                                                                             :game-player "How about you help me?"
                                                                             :game-player "Bring me the answer:"
                                                                             :game-player "White as snow, but not as cold,"
                                                                             :game-player "Keeps you warm, or so I'm told."
                                                                             :ego "Okay."))}
                                                 "No."
                                                 {:run #(actions/respond entities %
                                                                         :game-player "That's exactly what I'd expect a dummy like you to say.")
                                                  :choices actions/something-else}]}

                                      (when (= :wool (get-in @entities [:state :current-riddle]))
                                        "What was that riddle again?")
                                      {:run #(actions/respond entities %
                                                              :game-player "White as snow, but not as cold,"
                                                              :game-player "Keeps you warm, or so I'm told.")
                                       :choices actions/previous-choices}

                                      (when (= :balloon (get-in @entities [:state :current-riddle]))
                                        "What was that riddle again?")
                                      {:run #(actions/respond entities %
                                                              :game-player "Filled with air, light as a feather,"
                                                              :game-player "If you want to keep it, best have a tether.")
                                       :choices actions/previous-choices}

                                      (when (= :frog-legs (get-in @entities [:state :current-riddle]))
                                        "What was that riddle again?")
                                      {:run #(actions/respond entities %
                                                              :game-player "Hippity-hop, I jump real far,"
                                                              :game-player "Now I'm dead, or at least have a scar.")
                                       :choices actions/previous-choices}
                                      "Nevermind."
                                      {:run #(actions/respond entities % :game-player "See you around.")}]}))

(defn make [screen]
  (let [game-player-talk-sheet (texture! (texture "inside-castle/game-player-talk.png") :split 40 44)
        game-player-talk (animation 0.15 (for [i [0 2 0 2 0 2 0 3 0 2 0 1 0 0 0 0 2 0 2 0 3 0 1 0 1 0 0 1 0 2 0 3 0]]
                                          (aget game-player-talk-sheet 0 i)))] 
    (rooms/make :music :town-1
                :interactions
                {:right-door {:box [286 140 306 160]
                              :cursor :right
                              :script (actions/get-script entities
                                                          (actions/walk-to entities :ego [284 145])
                                                          (actions/transition-background entities :outside-castle [82 180])
                                                          (actions/walk-to entities :ego [129 148]))}
                 :up-door {:box [50 150 70 170]
                           :script (actions/get-script entities
                                                       (actions/walk-to entities :ego [65 155])
                                                       (actions/transition-background entities :inside-cafeteria [319 55])
                                                       (actions/walk-to entities :ego [300 55]))
                           :cursor :up}
                 :antique-door {:box [154 90 189 150]
                                :cursor :up
                                :script (actions/get-script entities
                                                            (actions/walk-to entities :ego [182 90] :face :left)
                                                            (actions/play-animation entities :ego :reach)
                                                            (actions/transition-background entities :inside-antique [222 15]))}
                 :sword {:box [0 130 39 165]
                         :script (actions/get-script entities
                                                     (actions/talk entities :ego "That's the coolest sword I've ever seen!!")
                                                     (actions/walk-to entities :ego [37 134] :face :left)
                                                     (actions/talk entities :ego "Maybe I can pull it out.")
                                                     (actions/play-animation entities :ego :reach)
                                                     (actions/do-dialogue entities
                                                                          :ego "I can't pull it out!"
                                                                          :ego "It looks like there's an enscription here.")
                                                     (actions/play-animation entities :ego :squat)
                                                     (actions/do-dialogue entities
                                                                          :ego "The Sword of Blergh with magic sting,"
                                                                          :ego "shall yield to no earthly king."
                                                                          :ego "Worthy in wisdom, courage, and might,"
                                                                          :ego "only then with sword he'll fight."))}
                 :sign {:box [125 140 165 155]
                        :script (actions/get-script entities
                                                    (actions/talk entities :ego "Ye Ol' Antique Shoppe."))}}
                :layers [(assoc (texture "inside-castle/background.png") :x 0 :y 0 :baseline 0)
                         (assoc (texture "inside-castle/pedestal-overlay.png") :x 0 :y 0 :baseline 135)]
                :entities {:game-player (assoc (texture "inside-castle/gameplayer.png") :x 266 :y 49 :baseline 191
                                               :script (actions/get-script entities (do-game-player-dialogue entities))
                                               :scripts {:wool (actions/get-script entities
                                                                                   (if (= :wool (get-in @entities [:state :current-riddle]))
                                                                                     (do (actions/update-state entities #(assoc % :current-riddle :balloon))
                                                                                         (actions/remove-item entities :wool)
                                                                                         (actions/do-dialogue entities
                                                                                                              :game-player "That's right! Now for your second riddle:"
                                                                                                              :game-player "Filled with air, light as a feather,"
                                                                                                              :game-player "If you want to keep it, best have a tether."))
                                                                                     (actions/talk entities :ego "He doesn't need it.")))
                                                         :balloon (actions/get-script entities
                                                                                      (if (= :balloon (get-in @entities [:state :current-riddle]))
                                                                                        (do (actions/update-state entities #(assoc % :current-riddle :frog-legs))
                                                                                            (actions/remove-item entities :balloon)
                                                                                            (actions/do-dialogue entities
                                                                                                                 :game-player "That's right! Now for your third riddle:"
                                                                                                                 :game-player "Hippity-hop, I jump really far,"
                                                                                                                 :game-player "Now I'm dead, or at least a have a scar."))
                                                                                        (actions/talk entities :ego "He doesn't need it.")))
                                                         :frog-legs (actions/get-script entities
                                                                                        (if (= :frog-legs (get-in @entities [:state :current-riddle]))
                                                                                          (do (actions/update-state entities #(assoc % :current-riddle :done))
                                                                                              
                                                                                              (actions/remove-item entities :frog-legs)
                                                                                              (actions/do-dialogue entities
                                                                                                                   :game-player "Wow! That's right!"
                                                                                                                   :game-player "I guess I'm not the wisest person in Remington."
                                                                                                                   :game-player "You have earned my trophy.")
                                                                                              (actions/give entities :trophy))
                                                                                          (actions/talk entities :ego "He doesn't need it.")))}
                                               :anim nil
                                               :talk game-player-talk)}
                :collision "inside-castle/collision.png"
                :scale-fn (utils/scaler-fn-from-image "inside-castle/scale.png" 0.25 1.00)
                :apply-state (fn [entities]
                               (as-> entities entities
                                     (if (actions/has-item? entities :ladder) 
                                       (update-in entities [:room :entities] #(dissoc % :ladder))
                                       entities)))
                :start-pos [245 90])))
