(ns sekai.entity.component
  #_:clj-kondo/ignore
  (:require [sekai.io.pixi :as pixi]
            [sekai.entity.entity :as entity]
            [sekai.util.util :as util :refer [and-f]]
            [sekai.io.display :as render]
            [sekai.entity.tags :as tags])
  (:require-macros [sekai.macros.components :refer [defcomponent]]))

(defn manip-tag-f [fun tag] (fn [eid] (fun eid tag)))
(defn add-tag-f [tag] (manip-tag-f tags/register-tag! tag))
(defn remove-tag-f [tag] (manip-tag-f tags/deregister-tag! tag))

(defprotocol IComponent
  (attach! [component entity])
  (_detach! [component entity]))

(defn detach! [component entity]
  (_detach! (entity/get-component entity component) entity))

(defcomponent position [x y z] nil
  (add-tag-f :spatial) (remove-tag-f :spatial))

(defcomponent sprite [sprite]
  pixi/make-sprite!

  (and-f (add-tag-f :visual) render/enable!)
  (and-f (remove-tag-f :visual) render/disable!))

(defcomponent screen-position [x y] nil
  (add-tag-f :ui) (remove-tag-f :ui))
