(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}]}
                                      "What are you playing?"
                                      {:run #(actions/respond entities %
                                                              :game-player "It's called Warlock's Castle. "
                                                              :game-player "You play as a warlock, using skills and magic to reach the top of the tower of doom."
                                                              :game-player "If you want to reach the top of the tower, you must have the sharpest of minds."
                                                              :game-player "It's not for little kids like you."
                                                              :game-player "Even wise wizards like Mr. Fangald can't beat me!")
                                       :choices ["Can I play with you?"
                                                 {:run #(do (actions/respond entities %
                                                                             :game-player "You think YOU have what it takes to climb the stairs of treachery?"
                                                                             :game-player "Or solve the riddle in the library of mystery?"
                                                                             :game-player "Or break the curse that has entrapped the cave beast for 1000 years?"
                                                                             :game-player "Very well. Here are your cards.")
                                                            (actions/give entities items/cards)
                                                            (actions/talk entities :game-player "Are you ready to play?"))
                                                  :choices ["You bet!" {:run (fn [_] (play-warlocks-castle entities))}
                                                            "Hang on a sec." {:choices ["Hey look! A real warlock!" {:choices actions/previous-choices}
                                                                                        "Ok, ready." {:run (fn [_] (play-warlocks-castle entities))}]}]}
                                                 "That sounds pretty dumb."
                                                 {:run #(actions/respond entities %
                                                                         :game-player "That's exactly what I'd expect a dummy like you to say."
                                                                         :choices actions/previous-choices)}
                                                 "Something else."
                                                 {:choices actions/something-else}]}
                                      "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 :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]))}}
                :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))
                                               :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))))
