;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC Sucursal en España SL

(ns app.util.debug
  (:require
   [app.main.store :as st]
   [app.util.storage :as storage]
   [okulary.core :as l]))

(def ^:private storage-key :app.util.debug/enabled-options)

(def options
  #{;; Displays the bounding box for the shapes
    :bounding-boxes

    ;; Displays an overlay over the groups
    :group

    ;; Displays in the console log the events through the application
    :events

    ;; Display the boxes that represent the rotation and resize handlers
    :handlers

    ;; Displays the center of a selection
    :selection-center

    ;; When active the single selection will not take into account previous transformations
    ;; this is useful to debug transforms
    :simple-selection

    ;; When active the thumbnails will be displayed with a sepia filter
    :thumbnails

    ;; When active we can check in the browser the export values
    :show-export-metadata

    ;; Show text fragments outlines
    :text-outline

    ;; Disable thumbnail cache
    :disable-thumbnail-cache

    ;; Disable frame thumbnails
    :disable-frame-thumbnails

    ;; Force thumbnails always (independent of selection or zoom level)
    :force-frame-thumbnails

    ;; Enable a widget to show the auto-layout drop-zones
    :layout-drop-zones

    ;; Display the layout lines
    :layout-lines

    ;; Display the bounds for the hug content adjust
    :layout-content-bounds

    ;; Makes the pixel grid red so its more visibile
    :pixel-grid

    ;; Show the bounds relative to the parent
    :parent-bounds

    ;; Show html text
    :html-text

    ;; Show history overlay
    :history-overlay

    ;; Show shape name and id
    :shape-titles

    ;; Show an asterisk for touched copies
    :show-touched

    ;; Show the id with the name
    :show-ids

    ;;
    :grid-layout

    ;; Show an overlay to the grid cells to know its properties
    :grid-cells

    ;; Show info about shapes
    :shape-panel

    ;; Show the floating components debugger window
    :components-debugger

    ;; Show what is touched in copies
    :display-touched

    ;; Show some visual indicators for bool shape
    :bool-shapes

    ;; Show some information about the WebGL context.
    :gl-context

    ;; Show viewbox.
    :wasm-viewbox

    ;; Makes the GL context to fail on initialization.
    :wasm-gl-context-init-error

    ;; Event times
    :events-times})

(defn- load-state
  []
  (let [stored (get storage/user storage-key #{})]
    (into #{} (filter options) stored)))

(defonce state (l/atom (load-state)))

(defn- persist-state!
  [state]
  (swap! storage/user assoc storage-key state))

(defn handle-change
  []
  (set! st/*debug-events* (contains? @state :events))
  (set! st/*debug-events-time* (contains? @state :events-times)))

(when *assert*
  (handle-change)
  (add-watch state :watcher handle-change)
  (add-watch state :persistence (fn [_ _ _ new-state]
                                  (persist-state! new-state))))

(defn enable!
  [option]
  (swap! state conj option))

(defn disable!
  [option]
  (swap! state disj option))

(defn enabled?
  ^boolean
  [option]
  (contains? @state option))

(defn toggle!
  [option]
  (if (enabled? option)
    (disable! option)
    (enable! option)))
