(ns advent.screens.rooms.behind-house
  (: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 make [screen]
  (rooms/make :music :town-2
              :interactions
              {:left-dir {:box [0 131 20 224]
                          :script (actions/get-script
                                   entities
                                   (actions/walk-to entities :ego [122 140])
                                   (actions/transition-background entities :outside-house [244 150])
                                   (actions/walk-to entities :ego [195 140]))
                          :cursor :left}
               #_:crack #_{:box [68 100 73 114]
                           :script (actions/get-script
                                    entities
                                    )}
               :mushrooms {:box [247 59 269 76]
                           :script (actions/get-script
                                    entities
                                    (if (actions/has-item? entities :mushrooms)
                                      (actions/talk entities :ego "I've already got a junk ton of mushrooms.")
                                      (do
                                        (actions/walk-to entities :ego [242 75])
                                        (actions/play-animation entities :ego :squat)
                                        (actions/give entities :mushrooms)
                                        (actions/talk entities :ego "Perfectly ripe mushrooms!"))))}
               :window {:box [103 44 130 140]
                        :script (actions/get-script
                                 entities
                                 (actions/walk-to entities :ego [128 100])
                                 (actions/talk entities :ego "I can see Fangald moving around in there but it's hard to see at this angle."))}}
              :layers [(assoc (texture "behindhouse/background.png") :x 0 :y 0 :baseline 0)
                       (assoc (texture "behindhouse/house.png") :x 0 :y 0 :baseline 122)
                       (assoc (texture "behindhouse/brush.png") :x 0 :y 0 :baseline 240)]
              :entities {:stick (assoc (texture "behindhouse/stick.png")
                                  :x 26 :y 80 :baseline 160
                                  :script (actions/get-script entities
                                                              (actions/walk-to entities :ego [50 80])
                                                              (actions/play-animation entities :ego :reach)
                                                              (actions/remove-entity entities :stick)
                                                              (actions/give entities :stick)
                                                              (actions/talk entities :ego "This stick might be useful.")
                                                              ))
                         :peeling (assoc (texture "behindhouse/house-cover.png")
                                    :x 60 :y 92 :baseline 148
                                    :script (actions/get-script entities
                                                                (if (get-in @entities [:state :opened-crack?])
                                                                  (do (actions/walk-to entities :ego [70 80])
                                                                      (actions/play-animation entities :ego :start-squat :stop? false)
                                                                      (actions/talk entities :ego "I can see Gandarf, the wizard inside." :animate? false :stop? false)
                                                                      (actions/talk entities :ego "It looks like he's opening his Magi-safe." :animate? false :stop? false)
                                                                      (actions/play-sound entities "safe-sound.ogg")
                                                                      (actions/talk entities :ego "So that's the code to his safe..." :animate? false :stop? false)
                                                                      (actions/play-animation entities :ego :end-squat)
                                                                      (actions/talk entities :ego "A lot of good it'll do me to know his password while he's still there."))
                                                                  (do (actions/walk-to entities :ego [80 80])
                                                                      (actions/talk entities :ego "It looks like the wall is crumbling here.")
                                                                      (actions/play-animation entities :ego :reach)
                                                                      (actions/update-entity entities :peeling #(assoc % :opacity 0))
                                                                      (actions/update-state entities (fn [state] (assoc state :opened-crack? true)))))))}
              :collision "behindhouse/collision.png"
              :scale-fn (utils/scaler-fn-with-baseline 110 0.10 1.00)
              :apply-state (fn [entities]
                             (as-> entities entities
                                   (if (get-in entities [:state :opened-crack?])
                                     (assoc-in entities [:room :entities :peeling :opacity] 0)
                                     entities)
                                   (if (actions/has-one-of? entities [:stick :cat-toy]) 
                                     (update-in entities [:room :entities] #(dissoc % :stick))
                                     entities)))
              :start-pos [172 122]))
