(ns advent.screens.rooms.inside-cafeteria
  (:require [advent.screens.rooms :as rooms]
            [advent.screens.items :as items]
            [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 do-warrior-dialogue [entities]
  (actions/talk entities :ego "Hey guys!")
  (actions/talk entities :warriors "Hark! Good day, young esquire.")
  (actions/present-choices entities
                           {:choices ["Why dost thou speaketh in this manner?"
                                      {:run #(actions/respond entities %
                                                              :warriors "`Tis but a tradition!"
                                                              :warriors "All knights speaketh in such a manner."
                                                              :warriors "As my father instructedeth unto me as a mere lad: "
                                                              :warriors "'Son, thou must speaketh in this manner.'"
                                                              :warriors "And I asketh unto him:"
                                                              :warriors "'But father, why musteth I speaketh in this manner?'"
                                                              :warriors "'Yea, my son, for thou art a knight. And thou musteth speaketh in this manner.'"
                                                              :warriors "And I toldeth unto him:"
                                                              :warriors "'Thou art my father, and I am thy son. Therefore I will speaketh in this manner.'"
                                                              :ego "... I think I've got the idea.")
                                       :choices actions/previous-choices}
                                      "Can I be a knight like you guys?"
                                      {:run #(actions/respond entities %
                                                              :warriors "We thinketh not, young esquire."
                                                              :warriors "You lacketh the strength and vigor required for such a task.")
                                       :choices ["But I'm on a quest to become a knight!"
                                                 {:run #(actions/respond entities %
                                                                         :warriors "Young esquire, thou art valiant in heart. "
                                                                         :warriors "Departeth henceforth and go hitherto, unto the gym.")}
                                                 "I challenge you to a arm wrestling match to prove my strength."
                                                 {:run (fn [msg]
                                                         (actions/respond entities msg :warriors "Prepare thyself, thy task is not for the feint of heart")
                                                         (if (actions/has-item? @entities :flask-1-strength)
                                                           (do
                                                             (actions/do-dialogue entities :ego "One sec.")
                                                             (actions/play-animation entities :ego :grow)
                                                             (actions/do-dialogue entities
                                                                                  :warriors "[Todo animation here]"
                                                                                  :warriors "Congratulations young master. Thou art worthy in might."
                                                                                  :warriors "Take my medal of strength.")
                                                             (actions/give entities :medal))
                                                           (do
                                                             (actions/do-dialogue entities
                                                                                  :warriors "[Todo animation here]"
                                                                                  :warriors "You lost, young master. Go hitherto, unto thy gym."))))}
                                                 "Something else."
                                                 {:choices actions/something-else}]
                                       }
                                      "Goodbye."
                                      {:run #(actions/respond entities % :warriors "Fare thee well, and godspeed.")}]}))

(defn do-ladder-guard-dialogue [entities]
  (actions/walk-to entities :ego [170 45] :face :right)
  (actions/do-dialogue entities
                       :ego "Hello."
                       :ladder-guard "'Ello, young sire.")
  (actions/present-choices entities {:choices ["Nice ladder."
                                               {:run #(actions/respond entities %
                                                                       :ladder-guard "Tis nice, indeed!"
                                                                       :ladder-guard "Belongeth to the Duke of Remington, it doth.")
                                                :choices ["Can I have it?"
                                                          {:run #(actions/respond entities % :ladder-guard "No.")
                                                           :choices ["Please?"
                                                                     {:run #(actions/respond entities %
                                                                                             :ladder-guard "No.")
                                                                      :choices ["Pretty please?"
                                                                                {:run #(actions/respond entities %
                                                                                                        :ladder-guard "No.")
                                                                                 :choices ["Pretty pretty please?"
                                                                                           {:run #(actions/respond entities %
                                                                                                                   :ladder-guard "No.")
                                                                                            :choices ["Pretty pretty pretty please?"
                                                                                                      {:run #(do (actions/respond entities %
                                                                                                                                  :ladder-guard "Well, okay."
                                                                                                                                  :ladder-guard "Do not darest telleth a soul, or the Duke will haveth my head!")
                                                                                                                 (actions/walk-to entities :ego [245 75] :face :right)
                                                                                                                 (actions/play-animation entities :ego :reach)
                                                                                                                 (actions/remove-entity entities :ladder)
                                                                                                                 (actions/give entities :ladder))}
                                                                                                      "Nevermind."
                                                                                                      {:run #(actions/respond entities %)}]}
                                                                                           "Nevermind."
                                                                                           {:run #(actions/respond entities %)}]}
                                                                                "Nevermind."
                                                                                {:run #(actions/respond entities %)}]}
                                                                     "Nevermind."
                                                                     {:run #(actions/respond entities %)}]}
                                                          "Something else."
                                                          {:choices actions/something-else}]}
                                               "Why aren't you sitting and eating?"
                                               {:run #(actions/respond entities %
                                                                       :ladder-guard "I'm oneth duty, young sire."
                                                                       :ladder-guard "Dost thou seeth this ladder?"
                                                                       :ladder-guard "Tis my duty to guardeth it."
                                                                       :ladder-guard "Why, if I didst sit and dineth with my friends, a hoodlum wouldst steal it.")
                                                :choices actions/previous-choices}
                                               "What's your name?"
                                               {:run #(actions/respond entities % :ladder-guard "Tim.")
                                                :choices actions/previous-choices}
                                               "Goodbye."
                                               {:run #(actions/respond entities % "Goodbye.")}]})
  )

(defn make [screen]
  (let [warriors-stand-sheet (texture! (texture "inside-cafeteria/warriors-stand.png") :split 66 126)
        warriors-stand (animation 0.2 (for [i [0 0 0 0 0 0 1 1 0 0 1 1 1 0 0 2 2 2 2 3 3 3 0 0 0 2 2 2 ]]
                                        (aget warriors-stand-sheet 0 i)))
        ladder-guard-sheet (texture! (texture "inside-cafeteria/ladder-guard.png") :split 37 87)
        ladder-guard-stand (animation 0.1 [(aget ladder-guard-sheet 0 0)])
        ladder-guard-talk (animation 0.2 (for [i [0 0 0 0 1 0 0 1]]
                                           (aget ladder-guard-sheet 0 i)))]
    (rooms/make :music :town-1
                :interactions
                {:right-dir {:box [300 0 320 120]
                             :script (actions/get-script
                                      entities
                                      (actions/walk-to entities :ego [319 50])
                                      (actions/transition-background entities :inside-castle [65 150])
                                      (actions/walk-to entities :ego [126 80]))
                             :cursor :right} 
                 }
                :layers [(assoc (texture "inside-cafeteria/background.png") :x 0 :y 0 :baseline 0)]
                :entities {:warriors (actions/start-animation screen (assoc (animation->texture screen warriors-stand) :x 5 :y 9 :baseline 180 
                                                                            
                                                                            :left {:stand (utils/flip warriors-stand)
                                                                                   :talk (utils/flip warriors-stand)}
                                                                            :right {:stand warriors-stand
                                                                                    :talk warriors-stand}
                                                                            :facing :right
                                                                            :script (actions/get-script entities
                                                                                                        (do-warrior-dialogue entities)
                                                                                                        ))
                                                              :stand)
                           :ladder-guard (actions/start-animation screen (assoc (animation->texture screen ladder-guard-stand) :x 202 :y 85 :baseline 155
                                                                                :stand ladder-guard-stand
                                                                                :talk ladder-guard-talk
                                                                                :script (actions/get-script entities (do-ladder-guard-dialogue entities)))
                                                                  :stand)
                           :ladder (assoc (texture "inside-cafeteria/ladder.png") :x 250 :y 85 :baseline 155
                                          :script (actions/get-script entities
                                                                      (actions/talk entities :ladder-guard "Do not toucheth, young sire.")))}
                :collision "inside-cafeteria/collision.png"
                :scale-fn (utils/scaler-fn-with-baseline 110 0.10 1.50)
                :apply-state (fn [entities]
                               (as-> entities entities
                                     (if (actions/has-item? entities :ladder) 
                                       (update-in entities [:room :entities] #(dissoc % :ladder))
                                       entities)))
                :start-pos [300 55])))
