(ns advent.screens.safe
  (: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]))


(def start-x 60)
(def start-y 83)

(defn close [entities]
  (screen! @(resolve 'advent.screens.scene/scene) :on-reactivate)
  (-> entities
      (assoc :shown? false)
      (assoc :start-showing? false)))

(defn button-box [x y]
  (zone/box (+ start-x x) (+ start-y y) (+ start-x x 16) (+ start-y y 16)))

(defn get-button [[x y]]
  (first (filter (fn [{:keys [in?] :as button}] 
                   (when (in? x y)
                     button))
                 [{:in? (button-box 9 17) :sound (sound "inside-house/safe-1.ogg") :id 1 }
                  {:in? (button-box 29 17) :sound (sound "inside-house/safe-2.ogg") :id 2}
                  {:in? (button-box 49 17) :sound (sound "inside-house/safe-3.ogg") :id 3}
                  {:in? (button-box 70 17) :sound (sound "inside-house/safe-4.ogg") :id 4}
                  {:in? (button-box 91 17) :sound (sound "inside-house/safe-5.ogg") :id 5}])))

(defscreen safe-screen
  :on-show
  (fn [screen entities]
    (update! screen :renderer (stage) :camera (orthographic))
    {:fade (assoc (texture "black.png")
              :scale-x 80
              :scale-y 80 
              :opacity 0.7)
     :safe (assoc (texture "inside-house/safe-screen.png") :x start-x :y start-y)
     :start-showing? false
     :shown? false})

  :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) (:safe entities)]))
      entities))

  :show-screen (fn [{:keys [success failure]} [entities]]
                 (assoc entities
                   :start-showing? true
                   :button-choices []
                   :success success
                   :failure failure))

  :on-mouse-moved (fn [screen [entities]]
                    entities)

  :on-touch-up (fn [screen [entities]]
                   (when (:shown? entities)
                     (let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})]
                       (if-let [button (get-button [x y])]
                         (do (sound! (:sound button) :play)
                             (let [new-state (update-in entities [:button-choices] #(conj % (:id button)))]
                               (cond (= [4 2 1 5 3] (:button-choices new-state))
                                     (do ((:success entities) (-> @(resolve 'advent.screens.scene/scene)
                                                                  :entities
                                                                  deref
                                                                  first))
                                         (close entities))
                                     (= 5 (count (:button-choices new-state)))
                                     (do ((:failure entities) (-> @(resolve 'advent.screens.scene/scene)
                                                                  :entities
                                                                  deref
                                                                  first))  
                                         (close entities))
                                     :else
                                     new-state)))
                         (close entities)))))

  :on-resize (fn [screen entities]
               (size! screen 320 240)))
