(ns advent.screens.rooms.cat-tree
  (: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 get-down [entities]
  (actions/talk entities :ego "I'll get down.")
  (actions/walk-straight-to entities :ego [151 50] :update-baseline? false :face :left)
  (actions/play-animation entities :ego :reach)
  (actions/remove-entity entities :blank)
  (actions/remove-entity entities :ladder)
  (actions/give entities :ladder))

(defn make [screen]
  (let [cat-stand-sheet (texture! (texture "cat-tree/cat-stand.png") :split 22 10)
        cat-stand (animation 0.15 (for [i (flatten [(repeat 10 0) 1 1 (repeat 10 0) 2 3 4 3 0  0 2 3 4 3 (repeat 10 0) 1 1 (repeat 10 0) 5 5 6 6 7 (repeat 10 [7 8]) 6 5 0])]
                                    (aget cat-stand-sheet 0 i)))
        ladder-entity (assoc (texture "inside-cafeteria/ladder.png") :x 120 :y 60 :baseline 162)]
    (rooms/make :music :town-2
                :interactions
                {:down-dir {:box [150 0 270 20]
                            :script (actions/get-script entities
                                                        (actions/walk-to entities :ego [203 1])
                                                        (actions/transition-background entities :outside-house [137 204])
                                                        (actions/walk-to entities :ego [195 140]))
                            :cursor :down}
                 :ladder-area {:box [46 40 265 190]
                               :scripts {:ladder (actions/get-unsaved-script entities
                                                                             (actions/talk entities :ego "I'll just set this up.")
                                                                             (actions/walk-to entities :ego [151 50] :face :left)
                                                                             (actions/play-animation entities :ego :reach)
                                                                             (actions/remove-item entities :ladder)
                                                                             (actions/add-entity entities :ladder ladder-entity)
                                                                             (actions/walk-straight-to entities :ego [140 85] :face :right :update-baseline? false)
                                                                             (actions/add-entity entities :blank (rooms/make-entity :blank
                                                                                                                                    {:object nil :x 0 :y 0 :width 320 :height 240 :baseline 240
                                                                                                                                     :script (actions/get-script entities
                                                                                                                                                                 (when (get-in @entities [:room :entities :ladder])
                                                                                                                                                                   (get-down entities)))})) 
                                                                             )}}
                 :grass {:box [26 105 60 160]
                         :script (actions/get-script entities
                                                     (if (not (actions/has-item? entities :grass))
                                                       (do
                                                         (actions/walk-to entities :ego [60 100] :face :left)
                                                         (actions/play-animation entities :ego :reach)
                                                         (actions/give entities :grass))
                                                       (actions/talk entities :ego "I don't really need any more grass.")))}}
                :layers [(assoc (texture "cat-tree/background.png") :x 0 :y 0 :baseline 0)
                         (assoc (texture "cat-tree/tree-and-rock.png") :x 0 :y 0 :baseline 161)
                         (assoc (texture "cat-tree/sillhoute.png") :x 0 :y 0 :baseline 240)]
                :entities {:cat (actions/start-animation screen
                                                          (assoc (animation->texture screen cat-stand)
                                                            :x 184 :y 173 :baseline 1000
                                                            :script (actions/get-script entities
                                                                                        (actions/talk entities :ego "Here kitty, kitty, kitty.")
                                                                                        (actions/talk entities :ego "Kitty's not so interested in me."))
                                                            :scripts {:cat-toy (actions/get-script entities
                                                                                                   (actions/play-animation entities :ego :cat-toy)
                                                                                                   (if (get-in @entities [:room :entities :ladder])
                                                                                                     (do (actions/talk entities :ego "I think he's going for it!")
                                                                                                         (actions/play-animation entities :ego :cat-toy)
                                                                                                         (actions/remove-entity entities :cat)
                                                                                                         (get-down entities)
                                                                                                         (actions/talk entities :ego "TODO: get award from little old lady in this room")
                                                                                                         (actions/give entities :certificate))
                                                                                                     (actions/talk entities :ego "I guess I'm too far away.")))})
                                                          cat-stand)}
                :collision "cat-tree/collision.png"
                :scale-fn (utils/scaler-fn-with-baseline 110 0.10 1.20)
                :apply-state (fn [entities]
                               (as-> entities entities
                                     (if (actions/has-item? entities :certificate) 
                                       (update-in entities [:room :entities] #(dissoc % :cat))
                                       entities)))
                :start-pos [203 1])))
