(ns advent.screens.rooms.inside-antique
  (: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 do-antique-dialogue [entities]
  (actions/do-dialogue entities
                       :shopkeep "Welcome to my antique shop!"
                       :shopkeep "Can I help you?")
  (actions/present-choices entities
                           {:choices ["What do you have for sale here?"
                                      {:run #(actions/respond entities %
                                                              :shopkeep "For sale?"
                                                              :shopkeep "This is a very UNIQUE antique shop."
                                                              :shopkeep "Nothing's for sale, sonny."
                                                              :shopkeep "It's more like a museum."
                                                              :shopkeep "You can take look around at some of my items, but nothing is for sale.")
                                       :choices ["How do you make money then?"
                                                 {:run #(actions/respond entities %
                                                                         :shopkeep "Mostly insurance claims."
                                                                         :shopkeep "When you run such an impressive shop as I do, items sometimes just go missing.")
                                                  :choices actions/previous-choices}
                                                 "Is there anything here you will sell?"
                                                 {:run #(actions/respond entities %
                                                                         :shopkeep "No."
                                                                         :shopkeep "But I do have some free magical fire mints that Gandarf brewed up."
                                                                         :shopkeep "Careful! They're spicy.")
                                                  :choices actions/previous-choices}
                                                 "So in order to continue on my quest, I'll need to solve some puzzle here?"
                                                 {:run #(actions/respond entities %
                                                                         :shopkeep "I have no idea what you're talking about, sonny.")
                                                  :choices actions/previous-choices}
                                                 "Something else"
                                                 {:choices actions/something-else}]}
                                      (when (get-in @entities [:state :wants-toy])
                                        "Listen, I really need that teddy bear you have.")
                                      {:run #(actions/respond entities %
                                                              :shopkeep "Why?"
                                                              :shopkeep "It belonged to my long lost son."
                                                              :shopkeep "Why should I give it to you?")
                                       :choices ["I just need it ok?"
                                                 {:run #(actions/respond entities %
                                                                         :shopkeep "I'm sorry, it's not for sale.")
                                                  :choices actions/something-else} 
                                                 "Your long lost son said I could have it."
                                                 {:run #(do (actions/respond entities %
                                                                             :shopkeep "REALLY? You've met him?"
                                                                             :ego "... erm. Yes!"
                                                                             :shopkeep "If you really met him, you'll have to prove it."
                                                                             :shopkeep "What is my son's name?"))
                                                  :choices ["... Bud?"
                                                            {:run #(actions/respond entities % :shopkeep "No. *sigh* That's not it.")}
                                                            "... Steve?"
                                                            {:run #(actions/respond entities % :shopkeep "No. *sigh* That's not it.")}
                                                            "... Bob?"
                                                            {:run #(actions/respond entities % :shopkeep "No. *sigh* That's not it.")}
                                                            (when ((get-in @entities [:state :clues]) :name)
                                                              "Herb.")
                                                            {:run #(do (actions/respond entities %
                                                                                        :shopkeep "Yes, that's it!"
                                                                                        :shopkeep "You must have really met my son!"
                                                                                        :shopkeep "Of course you can keep the teddy bear.")
                                                                       (actions/give entities :teddy))}]}]}
                                      "How's life in the antique shop biz?"
                                      {:run #(actions/respond entities %
                                                              :shopkeep "Pretty lonely."
                                                              :shopkeep "My long lost son used to help me run this shop, but he's been gone for five years now."
                                                              :shopkeep "I've been couped up in here by myself ever since."
                                                              :shopkeep "Now all I have is this grandfather clock to keep me company.")
                                       :choices actions/previous-choices}
                                      "Nevermind." {:run #(actions/respond entities % :shopkeep "Feel free to look around.")}]}))


(defn make [screen]
  (let [shopkeep-sheet (texture! (texture "inside-antique/shopkeep-talk.png") :split 18 21)
        shopkeep-stand (animation 0.1 (for [i (flatten [(repeat 30 0) 1 (repeat 50 0) 1 0 1 0 1])]
                                         (aget shopkeep-sheet 0 i)))
        shopkeep-talk (animation 0.15 (for [i [0 2 0 2 0 3 1 0]]
                                        (aget shopkeep-sheet 0 i)))
        portrait (rooms/make-entity :portrait (assoc (texture "inside-antique/portrait.png")
                                                :x 109
                                                :y 120 
                                                :baseline 120
                                                :script (actions/get-script entities
                                                                            (actions/walk-to entities :ego [136 80] :face :left)
                                                                            (actions/play-animation entities :ego :reach)
                                                                            (actions/talk entities :ego "It's a portrait. There's something on the back but I can't read it.")
                                                                            (actions/remove-entity entities :portrait)
                                                                            (actions/give entities :portrait))))]
    (rooms/make :music :inside-antique
                :interactions
                {:down {:box [60 0 290 25]
                        :cursor :down
                        :script (actions/get-script entities
                                                    (when (actions/has-item? entities :portrait)
                                                      (actions/walk-to entities :ego [222 3])
                                                      (actions/talk entities :shopkeep "Excuse me sonny. Please return my belongings before you leave.")
                                                      (actions/walk-to entities :ego [136 80] :face :left)
                                                      (actions/play-animation entities :ego :reach)
                                                      (actions/add-entity entities :portrait portrait)
                                                      (actions/remove-item entities :portrait))
                                                    (actions/walk-to entities :ego [222 3])
                                                    (actions/transition-background entities :inside-castle [182 90]))}
                 :window {:box [212 130 256 180]
                          :script (actions/get-script entities
                                                      (actions/talk entities :ego "It's a nice, big window."))
                          :scripts {:portrait (actions/get-script entities
                                                                  (actions/walk-to entities :ego [151 60] :face :right)
                                                                  (actions/play-animation entities :ego :reach)
                                                                  (actions/talk entities :ego "The portrait says 'Herb' on the back.")
                                                                  (actions/update-state entities (fn [state] (update-in state [:clues] #(conj % :name)) )))}}}
                :layers [(assoc (texture "inside-antique/background.png") :x 0 :y 0 :baseline 0)]
                :entities {:shopkeep (actions/start-animation screen (assoc (animation->texture screen shopkeep-stand) :x 137 :y 128 :baseline 112 
                                                                            :stand shopkeep-stand
                                                                            :talk shopkeep-talk
                                                                            :script (actions/get-script entities  (do-antique-dialogue entities))
                                                                            )
                                                              :stand)
                           :portrait portrait
                           :bowl (assoc (texture "inside-antique/bowl.png")
                                   :x 155
                                   :y 125
                                   :baseline 125
                                   :script (actions/get-script entities
                                                               (if (= 3 (get-in @entities [:state :mints-eaten]))
                                                                 (do (actions/walk-to entities :ego [145 80] :face :right)
                                                                     (actions/do-dialogue entities
                                                                                          :ego "She's all out."
                                                                                          :ego "Maybe Gandarf can brew her up another batch."))
                                                                 (do (actions/walk-to entities :ego [145 80] :face :right)
                                                                     (actions/talk entities :ego "I'll just try one of these mints.")
                                                                     (actions/play-animation entities :ego :reach)
                                                                     (actions/update-state entities (fn [s] (assoc s :mints-eaten (inc (s :mints-eaten)))))
                                                                     (actions/play-animation entities :ego [:fire (get-in @entities [:state :mints-eaten])])
                                                                     (actions/talk entities :ego "WOWZA! Those are hot.")
                                                                     (when (= 3 (get-in @entities [:state :mints-eaten]))
                                                                       (actions/talk entities :shopkeep "You brat! You ate the last mint.")
                                                                       (actions/talk entities :shopkeep "Since you ate the last one, you have to go tell Gandarf to bring me some more."))))))}
                :collision "inside-antique/collision.png"
                :scale-fn (utils/scaler-fn-with-baseline 110 0.10 1.50)
                :start-pos [222 3])))
