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

(local {: draw-text} (require :text))
(local {: new-world} (require :world))
(local {: draw-cards :kinds card-kinds} (require :cards))
(local {: make-circuit-mat : make-slots : make-decks} (require :match.make))
(local {: arrange-on-circuit-mat
        : draw-cells
        : draw-highlighted-cells
        : draw-slots
        : draw-game-over
        : arrange-in-slots} (require :match.draw))

(fn page [title text other]
  (table.insert other {: text :x 10 :y 29})
  (table.insert other {:text title :x 10 :y 9})
  other)

(local circuit-mat [])
(make-circuit-mat circuit-mat)

(local cards
       (icollect [i kind (ipairs card-kinds)]
         {:card {: kind :open true :owner 1} :x (+ (* i 16) 100) :y 95}))

(local won [])

(for [i 1 4]
  (let [x (+ 20 (* i 16))
        y 60]
    (table.insert won {:card {:kind :link :open true :owner 1} : x : y})
    (table.insert won {:card {:kind :virus :open true :owner 2}
                       :x (+ x 100)
                       : y})))

(fn spotlight [kind]
  {:card {: kind :owner 1 :open true} :x 110 :y 80})

(fn draw-board [world]
  (each [_ entity (ipairs world)]
    (case entity {:board true : x : y}
      (_G.map 0 17 8 8 x y))))

(local pages
       [(page :Setup "Two players play on an 8x8 board.


Setup Zone ->








Setup Zone ->" [{:board true :x 80 :y 45}])
        (page :Setup "Each player places their Online cards
(4 Link cards and 4 Virus cards) in their
Setup Zone.

Only you know the alignment of your cards.

Each player starts with 4 Terminal cards:
LINE BOOST, FIRE WALL, VIRUS CHECKER and
404 NOT FOUND" cards)
        (page :Winning "A player wins when they have 4 Link
cards in their Stack Area or their
opponent has 4 Virus cards in their
Stack Area." won)
        (page :Turns "You can move your Online cards to empty
neigboring cells, or to an enemy card to
capture it. The enemy card is revealed
and put into your Stack Area.

When your card is placed on an EXIT,
you can use it to infiltrate the opponent
by clicking on your Stack Area, moving the
card there." [])
        (page "Fire Wall" "Can be placed on any cell without an
enemy card except on EXITs.

Only your cards can move to that cell.

Can be uninstalled by clicking the empty
Fire Wall slot."
              [(spotlight :fire-wall)])
        (page "Line Boost" "Can be used on an online card to extend
its movement range.

Can also be uninstalled." [(spotlight :line-boost)])
        (page "404 Not Found" "Allows you to swap two cards by selecting
them.

Can only be used once." [(spotlight :404)])
        (page "Virus Check"
              "Reveals an enemies card.\n\nCan only be used once."
              [(spotlight :virus-check)])
        (page :Interface "The only accepts mouse input.

Clickable areas are highlighted like so:"
              [{:card {:kind :link :border true :open true :owner 1}
                :x 70
                :y 60}
               {:highlighted-cell true :x 100 :y 60}
               {:slot {:kind :link :border true} :x 135 :y 60}])])

(fn turn-to-page [world page]
  (set world.page page)
  (each [i entity (ipairs world)]
    (when entity.in-page
      (world.remove i)))
  (each [_ entity (ipairs (. pages page))]
    (set entity.in-page true)
    (table.insert world entity)))

(fn next-page [world]
  (let [(x y down) (_G.mouse)]
    (if (and down (not world.handled))
        (do
          (set world.handled true)
          (if (= world.page (length pages)) (set world.next world.previous)
              (turn-to-page world (+ world.page 1))))
        (not down)
        (set world.handled false))))

(fn draw-page []
  (_G.cls 8)
  (_G.rect 0 24 250 89 9))

(fn new-tutorial [title-screen]
  (local world (new-world [draw-page
                           arrange-on-circuit-mat
                           draw-cells
                           draw-highlighted-cells
                           draw-slots
                           draw-board
                           draw-cards
                           draw-game-over
                           arrange-in-slots
                           draw-text
                           next-page] []))
  (set world.previous title-screen)
  (turn-to-page world 1)
  (set world.handled true)
  world)

{: new-tutorial}