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

(local {: draw-portraits} (require :portraits))
(local {: new-world} (require :world))
(local {: new-match} (require :match.match))
(local {: select-with-mouse : perform-select} (require :select))
(local {: opponents} (require :opponents))

(fn draw-select-opponent []
  (_G.cls 0)
  (_G.print "SELECT OPPONENT:" 30 20 13 false 2))

(fn highlight-selected-opponent [world]
  (each [_ entity (ipairs world)]
    (when (and entity.portrait entity.on-select)
      (set entity.border entity.selected))))

(fn new-opponent-select []
  (_G.music)
  (local world (new-world [draw-select-opponent
                           select-with-mouse
                           highlight-selected-opponent
                           perform-select
                           draw-portraits] []))
  (each [i opponent (ipairs opponents)]
    (table.insert world
                  {:portrait opponent
                   :x (+ (* (- i 1) 60) 6)
                   :y 60
                   :w 40
                   :h 40
                   :on-select (fn []
                                (set world.next (new-match opponent)))}))
  world)

{: new-opponent-select}