; SPDX-FileCopyrightText: 2023 Jummit
;
; SPDX-License-Identifier: GPL-3.0-or-later

;; Add interactions with game elements.

(fn remove-cell-actions [world]
  (each [id entity (ipairs world)]
    (when entity.highlighted-cell
      (world.remove id))))

(fn remove-card-actions [world]
  (each [_ entity (ipairs world)]
    (when (and entity.card entity.on-select)
      (set entity.on-select nil)
      (set entity.card.border nil))))

(fn remove-slot-actions [world]
  (each [_ entity (ipairs world)]
    (when (and entity.slot entity.on-select)
      (set entity.on-select nil)
      (set entity.slot.border nil))))

(fn add-cell-action [world cell on-select]
  (case cell
    {: circuit-x : circuit-y}
    (do
      (var cell nil)
      (each [_ entity (ipairs world)]
        (match entity {:highlighted-cell true : circuit-x : circuit-y} (set cell entity)))
      (when (not cell) (set cell {}) (table.insert world cell))
      (set cell.circuit-x circuit-x)
      (set cell.circuit-y circuit-y)
      (set cell.highlighted-cell true)
      (set cell.on-select on-select)
      (set cell.w 16)
      (set cell.h 16))))

(fn add-card-action [card on-select]
  (set card.card.border true)
  (set card.w 11)
  (set card.h 16)
  (set card.on-select on-select))

(fn add-slot-action [slot on-select]
  (set slot.slot.border true)
  (set slot.w 16)
  (set slot.h 16)
  (set slot.on-select on-select))

{: remove-cell-actions
 : add-cell-action
 : remove-card-actions
 : remove-slot-actions
 : add-card-action
 : add-slot-action}