(ns advent.screens.inventory
  (:require [play-clj.core :refer :all]
            [play-clj.ui :refer :all]
            [play-clj.utils :refer :all]
            [play-clj.g2d :refer :all]
            [clojure.pprint]
            [advent.pathfind]
            [advent.zone :as zone]
            [advent.utils :as utils])
  (:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter]
           [com.badlogic.gdx.graphics.g2d TextureRegion]
           [com.badlogic.gdx.scenes.scene2d.utils Align]
           [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input
            InputMultiplexer InputProcessor Net Preferences Screen]))



(defscreen inventory-screen
  :on-show
  (fn [screen entities]
    (update! screen :renderer (stage) :camera (orthographic))
    (let [highlighted-text (assoc (label "Hello" (style :label (utils/get-font "ego/font.fnt") (color :white))) :x 0 :y 850 :width 1280)]
      (label! highlighted-text :set-alignment Align/center)
      {:overlay  (assoc (texture "inventory-overlay.png" ) :x 0 :y 0 :scale-x 4 :scale-y 4 :origin-x 0 :origin-y 0)
       :fade (assoc (texture "black.png")
               :scale-x 80
               :scale-y 80 
               :opacity 0.7)
       :all-items (texture! (texture "cursor.png") :split 16 16)
       :items []
       :shown? false
       :start-showing? false
       :highlighted-item nil
       :highlighted-text highlighted-text}))

  :on-render
  (fn [screen [entities]]
    (let [entities (if (:start-showing? entities)
                     (-> entities
                         (assoc :start-showing? false)
                         (assoc :shown? true))
                     entities)]
      
      (when (:shown? entities)
        (render! screen [(:fade entities) (:overlay entities)])
        (render! screen (:items entities))
        (if-let [item (:highlighted-item entities)]
          (label! (:highlighted-text entities) :set-text (name item))
          (label! (:highlighted-text entities) :set-text ""))
        (render! screen [(:highlighted-text entities)]))
      entities))

  :show-screen (fn [{items :items} [entities]]
                 (assoc entities :start-showing? true
                        :items (for [[item index] (map vector items (range))
                                     :let [x (+ (* 79 4) (* index (* 24 4)))
                                           y (* 4 180)
                                           item-width 16
                                           offset-x (+ x (/ item-width 2))
                                           offset-y (+ y (/ item-width 2))
                                           padding (/ item-width 2)
                                           padding (* 4 padding)]]
                                 (assoc (texture (aget (:all-items entities) 0 (.indexOf utils/+all-cursors+ item)))
                                   :x x :y y
                                   :scale-x 4
                                   :scale-y 4
                                   :item item
                                   :box (zone/box (- offset-x padding) (- offset-y padding) (+ offset-x item-width padding) (+ offset-y item-width padding))))))

  :on-mouse-moved (fn [screen [entities]]
                    (let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})
                          selected-entity (first (filter #((:box %) x y) (:items entities)))]
                      (if selected-entity
                        (assoc entities :highlighted-item (:item selected-entity))
                        (assoc entities :highlighted-item nil))))

  :on-touch-down (fn [screen [entities]]
                   (when (:shown? entities)
                     (run! @(resolve 'advent.screens.scene/scene) :on-reactivate)
                     (let [{:keys [highlighted-item]} entities]
                       (when highlighted-item
                         (run! @(resolve 'advent.screens.scene/scene) :on-chose-item :item highlighted-item)))
                     (-> entities
                         (assoc :shown? false)
                         (assoc :start-showing? false))))

  :on-resize (fn [screen entities]
               (height! screen 960)
               entities))
