;;-*-Lisp-*-
(in-package goal)

;; name: board-util.gc
;; name in dgo: board-util
;; dgos: ENGINE, GAME

(define-extern ja-post (function none :behavior process-drawable))
(define-extern ja-channel-set! (function int int :behavior process-drawable))
(define-extern ja-channel-push! (function int time-frame int :behavior process-drawable))
(define-extern ja-done? (function int symbol :behavior process-drawable))
(define-extern *target-shadow-control* shadow-control)

;; DECOMP BEGINS

;; WARN: Return type mismatch process-drawable vs board.
(defmethod relocate ((this board) (arg0 int))
  (if (nonzero? (-> this main))
      (&+! (-> this main) arg0)
      )
  (the-as board ((method-of-type process-drawable relocate) this arg0))
  )

(defbehavior board-post board ()
  (let* ((a0-0 (-> self parent))
         (v1-0 (if a0-0
                   (-> (the-as target (-> a0-0 0)) self)
                   )
               )
         )
    (set! (-> self root trans quad) (-> v1-0 board board-trans quad))
    (let ((a0-4 (-> v1-0 board board-quat quad)))
      (set! (-> self root quat quad) a0-4)
      )
    (set! (-> self root scale quad) (-> v1-0 board board-scale quad))
    (set! (-> self draw light-index) (-> v1-0 draw light-index))
    (let ((a0-10 (-> v1-0 draw color-mult quad)))
      (set! (-> self draw color-mult quad) a0-10)
      )
    (let ((a0-12 (-> v1-0 draw color-emissive quad)))
      (set! (-> self draw color-emissive quad) a0-12)
      )
    (set! (-> self draw force-fade) (-> v1-0 draw force-fade))
    (set! (-> self draw death-vertex-skip) (-> (the-as target (-> self parent 0)) draw death-vertex-skip))
    (set! (-> self draw death-effect) (-> (the-as target (-> self parent 0)) draw death-effect))
    (set! (-> self draw death-timer) (-> (the-as target (-> self parent 0)) draw death-timer))
    (set! (-> self draw death-timer-org) (-> (the-as target (-> self parent 0)) draw death-timer-org))
    (set! (-> self draw death-draw-overlap) (-> (the-as target (-> self parent 0)) draw death-draw-overlap))
    (let ((a0-37 (-> v1-0 draw shadow-ctrl settings shadow-dir quad)))
      (set! (-> self draw shadow-ctrl settings shadow-dir quad) a0-37)
      )
    (if (or (logtest? (-> v1-0 draw status) (draw-control-status no-draw no-draw-temp no-draw-bounds no-draw-bounds2))
            (zero? (-> v1-0 skel active-channels))
            )
        (logior! (-> self draw status) (draw-control-status no-draw))
        (logclear! (-> self draw status) (draw-control-status no-draw))
        )
    (if (logtest? (-> v1-0 draw status) (draw-control-status force-fade))
        (logior! (-> self draw status) (draw-control-status force-fade))
        (logclear! (-> self draw status) (draw-control-status force-fade))
        )
    )
  (ja-post)
  0
  (none)
  )

(defstate hidden (board)
  :virtual #t
  :trans (behavior ()
    (let ((v1-0 (-> self parent)))
      (if (not (focus-test?
                 (the-as target (if v1-0
                                    (the-as target (-> v1-0 0 self))
                                    )
                         )
                 in-head
                 )
               )
          (go-virtual idle #f)
          )
      )
    )
  :code (behavior ()
    (ja-channel-set! 0)
    (ja-post)
    (logior! (-> self mask) (process-mask sleep-code))
    (suspend)
    0
    )
  )

(defstate idle (board)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('open)
       (go-virtual use)
       )
      )
    )
  :trans (behavior ()
    (let ((v1-0 (-> self parent)))
      (if (focus-test?
            (the-as target (if v1-0
                               (the-as target (-> v1-0 0 self))
                               )
                    )
            in-head
            )
          (go-virtual hidden)
          )
      )
    )
  :code (behavior ((arg0 symbol))
    (ja-channel-push! 1 (seconds 0.1))
    (cond
      (arg0
        (ja :group! board-close-ja :num! max)
        )
      (else
        (ja-no-eval :group! board-close-ja :num! (seek!) :frame-num 0.0)
        (until (ja-done? 0)
          (suspend)
          (ja :num! (seek!))
          )
        )
      )
    (set! (-> self draw shadow) #f)
    (sleep-code)
    )
  :post board-post
  )

(defstate use (board)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('close)
       (go-virtual idle #f)
       )
      )
    )
  :trans (behavior ()
    (let ((v1-0 (-> self parent)))
      (if v1-0
          (-> v1-0 0 self)
          )
      )
    (let ((v1-2 (-> self parent)))
      (cond
        ((focus-test?
           (the-as target (if v1-2
                              (the-as target (-> v1-2 0 self))
                              )
                   )
           in-head
           )
         (go-virtual hidden)
         )
        ((let ((v1-9 #x40000)
               (a0-3 (-> self parent))
               )
           (not (logtest? (the-as focus-status v1-9) (-> (the-as target (if a0-3
                                                                            (the-as target (-> a0-3 0 self))
                                                                            )
                                                                 )
                                                         focus-status
                                                         )
                          )
                )
           )
         (go-virtual idle #f)
         )
        )
      )
    )
  :code (behavior ()
    (set! (-> self draw shadow) (-> self shadow-backup))
    (ja-channel-push! 1 (seconds 0.1))
    (ja-no-eval :group! board-open-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (sleep-code)
    )
  :post board-post
  )

;; WARN: Return type mismatch object vs none.
(defbehavior board-init board ()
  (set! (-> self root) (new 'process 'trsqv))
  (initialize-skeleton
    self
    (the-as skeleton-group (art-group-get-by-name *level* "skel-board" (the-as (pointer uint32) #f)))
    (the-as pair 0)
    )
  (let ((v1-5 (-> *target-shadow-control* settings shadow-dir quad)))
    (set! (-> *board-shadow-control* settings shadow-dir quad) v1-5)
    )
  (set! (-> self draw shadow-ctrl) *board-shadow-control*)
  (set! (-> self shadow-backup) (-> self draw shadow))
  (set! (-> self main) (new 'process 'joint-mod (joint-mod-mode flex-blend) self 3))
  (ja-channel-set! 0)
  (go-virtual idle #t)
  (none)
  )
