(ns advent.screens.rooms
  (:require [advent.zone :as zone]))

(defn make [& {:keys [collision interactions entities] :as params}]
  (let [interactions-as-list (for [[id spec] interactions]
                               (merge spec {:mouse-in? (fn [_ x y]
                                                         ((apply zone/box (:box spec)) x y))
                                            :get-script (fn [cursor [x y]]
                                                          (if (= :main cursor)
                                                            (:script spec)
                                                            (get-in spec [:scripts cursor])))}))
        entities (into {} (for [[id entity] entities]
                            [id (merge entity
                                       {:mouse-in? (fn [entities x y]
                                                     (let [{entity-x :x entity-y :y region :object} (get-in entities [:room :entities id]) 
                                                           width (.getRegionWidth region)
                                                           height (.getRegionHeight region)]
                                                       ((zone/box entity-x entity-y (+ entity-x width) (+ entity-y height)) x y)))}
                                       (when (:script entity)
                                         {:get-script (fn [cursor [x y]]
                                                        (if (= :main cursor)
                                                          (:script entity)
                                                          (get-in entity [:scripts cursor])))}))]))]
    (merge params {:collision (advent.pathfind/map-from-resource collision)
                   :interactions interactions-as-list
                   :entities entities})))


