(ns advent.screens.rooms.inside-house
  (:require [advent.screens.items :as items]
            [advent.screens.rooms :as rooms]
            [advent.screens.safe :as safe]
            [advent.actions :as actions]
            [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 open-safe [entities]
  (screen! safe/safe-screen :show-screen
           :success (actions/get-script entities
                                        (actions/talk entities :ego "Yes! That worked.")
                                        (actions/play-animation entities :ego :squat)
                                        (actions/give entities :recipe)
                                        (actions/talk entities :ego "I found a recipe for a strength potion!")
                                        (actions/talk entities :ego "Looks like there's something else in here too...")
                                        (actions/play-animation entities :ego :squat)
                                        (actions/give entities :frog-legs)
                                        (actions/talk entities :ego "Eww. Frog legs."))
           :failure (actions/get-script entities
                                        (actions/talk entities :ego "I don't think that worked...")))
  (actions/update-state entities #(assoc % :active? false)))

(defn do-wizard-dialogue [entities]
  (actions/do-dialogue entities :wizard "What can I do for you boy?")
  (actions/present-choices entities
                           {:choices ["What's with the safe?"
                                      {:run #(actions/respond entities %
                                                              :wizard "That's my MagiSafe 5000."
                                                              :wizard "It's used to keep whipper-snappers like you out of my precious belongings."
                                                              :wizard "It's a magical safe only opened when the correct musical password is entered."
                                                              :ego "What's the password?"
                                                              :wizard "That's for me to know.")
                                       :choices actions/previous-choices}
                                      "Will you help me become a knight?"
                                      {:run #(actions/respond entities %
                                                              :wizard "To become a knight you will need to be worthy in courage, wisdom, and might."
                                                              :wizard "Only then can you become worthy to pull the Sword of Blergh, and become a knight.")
                                       :choices ["How can I prove that I'm worthy in wisdom?"
                                                 {:run #(actions/respond entities % 
                                                                         :wizard "Every year we have the annual Town of Remington Junior Smarty Pants Derby."
                                                                         :wizard "The whole town gets together to vote on who is the wisest in town, by a game of riddles."
                                                                         :wizard "Last year's winner was a young man named Stan, in town."
                                                                         :wizard "Maybe you can ask him.")
                                                  :choices actions/previous-choices}
                                                 "Can you make some kind of potion to make me strong?"
                                                 {:run #(actions/respond entities %
                                                                         :wizard "Of course I could."
                                                                         :wizard "I keep all sorts of potion recipes in my MagiSafe 5000."
                                                                         :wizard "I can turn a weakling into an olympic lifter."
                                                                         :wizard "But that would be cheating wouldn't it?"
                                                                         :wizard "Unfortunately, you're on your own.")
                                                  :choices actions/previous-choices}
                                                 "Aren't I already courageous enough?"
                                                 {:run #(actions/respond entities %
                                                                         :wizard "When was the last time you rescued a damsel in distress?"
                                                                         :ego "Erm..."
                                                                         :wizard "Fought a firebreathing dragon?"
                                                                         :ego "Umm..."
                                                                         :wizard "Saved an old lady from a burning building?"
                                                                         :wizard "Stave off zombies from the eternal graveyard?"
                                                                         :wizard "Rescued a stranded cat?"
                                                                         :ego "I haven't done any of those things!"
                                                                         :wizard "Seems to me you have to go do some heroic deeds!")
                                                  :choices actions/previous-choices}
                                                 "Something else."
                                                 {:choices actions/something-else}]}
                                      (when (= 3 (get-in @entities [:state :mints-eaten]))
                                        "The antique shopkeeper needs more fire mints.")
                                      {:run #(do (actions/respond entities %
                                                                  :wizard "Already?"
                                                                  :wizard "Ok, I'll deliver them myself. Don't touch anything while I'm gone.")
                                                 (actions/update-state entities (fn [s] (assoc s :mints-eaten 0)))
                                                 (actions/remove-entity entities :wizard))}
                                      "Nevermind."
                                      {:run #(actions/do-dialogue entities :ego %)}]}))

(defn make [screen]
  (let [wizard-sheet (texture! (texture "wizard/talk.png") :split 20 46)
        wizard-stand (animation 0.2 (for [i (flatten [(repeat 10 0) 1])]
                                      (aget wizard-sheet 0 i)))
        wizard-talk (animation 0.2 (for [i [0 2 0 2 1 2 0 3 0 2 0 1 0 2]]
                                     (aget wizard-sheet 0 i)))
        safelock-sheet (texture! (texture "inside-house/safe-lock.png") :split 9 2)
        safe-lock (animation 0.1 (for [i (flatten [(repeat 20 0) 1 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 1 (range 3 20) 20 20 20 20 21 21 21 21 21 20 20 21 21 21 21 21 20 20 20 ])]
                                   (aget safelock-sheet 0 i)))]
    (rooms/make :music :inside-fangald
                :interactions {:down-dir {:box [151 0 320 20]
                                          :script (actions/get-script entities
                                                                      (actions/walk-to entities :ego [237 1])
                                                                      (actions/transition-background entities :outside-house [262 88]))
                                          :cursor :down}
                               :safe {:box [34 70 70 115]
                                      :script (actions/get-script entities
                                                                  (actions/walk-to entities :ego [59 65])
                                                                  (if (get-in @entities [:room :entities :wizard])
                                                                    (actions/talk entities :wizard "Don't touch my MagiSafe!!")
                                                                    (open-safe entities)))}
                               }
                :layers [(assoc (texture "inside-house/background.png") :x 0 :y 0 :baseline 0)
                         (assoc (texture "inside-house/desk.png") :x 0 :y 0 :baseline 200)
                         (assoc (texture "inside-house/sillhoute.png") :x 0 :y 0 :baseline 240)]
                :entities {:wizard (actions/start-animation screen (assoc (animation->texture screen wizard-stand) :x 228 :y 80 :baseline 160 :scale-x 1.75 :scale-y 1.75
                                                                          :left {:talk (utils/flip wizard-talk)
                                                                                 :stand (utils/flip wizard-stand)}
                                                                          :right {:talk wizard-talk
                                                                                  :stand wizard-stand}
                                                                          :facing :left
                                                                          :script (actions/get-script entities (do-wizard-dialogue entities)))
                                                            :stand)
                           :safe-lock (actions/start-animation screen (assoc (animation->texture screen safe-lock) :x 51 :y 95 :baseline 145 
                                                                             :stand safe-lock)
                                                            :stand)
                           :flask (assoc (texture "inside-house/flask.png")
                                    :x 265 :y 80 :baseline 240
                                    :script (actions/get-script entities
                                                                (actions/remove-entity entities :flask)
                                                                (actions/give entities :flask-1)
                                                                (actions/do-dialogue entities :ego "Hey you think I could have this flask?"
                                                                                     :wizard "Sure.")))}
                :collision "inside-house/collision.png"
                :scale-fn (utils/scaler-fn-with-baseline 110 0.10 1.75)
                :apply-state (fn [entities]
                             (as-> entities entities
                                   (if (actions/has-one-of? entities [:flask-1 :flask-1-with-cream-of-mushroom :flask-1-strength :flask-1-with-mushrooms :flask-1-with-milk]) 
                                     (update-in entities [:room :entities] #(dissoc % :flask))
                                     entities)))
                :start-pos [237 0])))
