(eval-when (:compile-toplevel :load-toplevel :execute)
  (load "~/.local/share/quicklisp/setup.lisp")
  (ql:quickload :cl-raylib))

(defpackage :raylib-user
  (:use :cl :3d-vectors)
  (:local-nicknames (:rl :raylib)))

(in-package :raylib-user)

(defparameter *move-speed* 5.0)

(defclass health ()
  ((current-amount :initform 100.0 :accessor current-amount)
   (max-amount     :initform 100.0 :accessor max-amount)))

(defclass animation ()
  ((current-amount :initform 100.0 :accessor current-amount)
   (max-amount     :initform 100.0 :accessor max-amount)))

(defclass knight ()
  ((pos :initform (vec 270 360) :accessor pos)
   (anim-state :initform 0 :accessor anim-state)
   (velocity :initform (vec 0 0) :accessor velocity)
   (health :initform 100.0 :accessor health)))

(defclass archer ()
  ((pos :initform (vec 145.0 253.0) :accessor pos)
   (velocity :initform (vec 0 0) :accessor velocity)
   (health :initform 100.0 :accessor health)))

(defclass goblin ()
  ((health :initform 100.0 :accessor health)))

(defclass tower ()
  ((health :initform 100.0 :accessor health)))

(defparameter knight1 (make-instance 'knight))
(defparameter archer1 (make-instance 'archer))
(defparameter goblin1 (make-instance 'goblin))

(defun deal-damage (obj damage)
  (with-accessors ((h health)) obj
    (setf h (- h damage))))

(mapcar (lambda (go) (deal-damage go 10)) (list knight1 archer1 goblin1))

(defclass game-state ()
  ((click-pos :initform nil :accessor click-pos)))

(defparameter *game-state* nil)
(defparameter *textures* (make-hash-table))

(defun bind-texture (key path)
  (setf (gethash key *textures*) (rl:load-texture (uiop:native-namestring path))))

(defun game-init ()
  (setf *game-state* (make-instance 'game-state))
  ;; (add-component (knight *game-state*) (make-instance 'transform))
  (bind-texture 'terrain "~/Development/tinyswords/assets/Terrain/Ground/Tilemap_Flat.png")
  (bind-texture 'knight "~/Development/tinyswords/assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png")
  (bind-texture 'archer "~/Development/tinyswords/assets/Factions/Knights/Troops/Archer/Blue/Archer_Blue.png")
  (bind-texture 'tower "~/Development/tinyswords/assets/Factions/Knights/Buildings/Tower/Tower_Blue.png"))

(defun game-input ()
  (when (rl:is-mouse-button-pressed 0)
    (print (rl:get-mouse-position))
    (setf (click-pos *game-state*) (rl:get-mouse-position))))
  ;; (with-slots ((pos archer-pos) (kpos knight-pos)) *game-state*
    ;; (let ((dx 0.0) (dy 0.0))
    ;;   (when (rl:is-key-down :key-right) (incf dx 1))
    ;;   (when (rl:is-key-down :key-left)  (decf dx 1))
    ;;   (when (rl:is-key-down :key-up)    (decf dy 1))
    ;;   (when (rl:is-key-down :key-down)  (incf dy 1))
    ;;   (setf pos (v+ pos (v* (vunit* (vec dx dy)) *move-speed*))))))

(defun game-update ()
  (with-accessors ((click-pos click-pos)) *game-state*
    (when click-pos
      (setf (anim-state knight1) 1)
      (with-accessors ((kpos pos)) knight1
        (let* ((dist (v- click-pos kpos))
               (mag (vsqrlength dist)))
          (if (<= mag 10.0)
              (progn
                (setf (anim-state knight1) 0)
                (setf kpos click-pos)
                ;; (setf (velocity knight1) (vec 0 0))
                (setf click-pos nil))
              (let ((vel (v* (* (rl:get-frame-time) 150.0) (vunit* dist))))
                (setf (velocity knight1) vel)
                (setf kpos (v+ kpos vel)))))))))

(defun get-tile-wrapped (n wrap-count) (if (> n 0) (1+ (mod (1- n) (- wrap-count 2))) 0))

(defun draw-tile (tilemap size col row)
  (let* ((tile-count 6))
    (rl:draw-texture-rec
     tilemap
     (rl:make-rectangle :x (* size (get-tile-wrapped col tile-count))
                        :y (* size (get-tile-wrapped row tile-count))
                        :width size :height size)
     (v+ (vec 15 150) (vec (* col size) (* row size)))
     :white)))

(defun draw-ground ()
  (let ((size 32)
        (terrain-tex (gethash 'terrain *textures*)))
    (loop for row from 0 to 16
          do (loop for col from 0 to 32
                   do (draw-tile terrain-tex size col row)))))

(defun animate-sprite (tex-key row pos vel)
  (let ((time-interval (coerce (mod (truncate (/ (rl:get-time) 0.1)) 6) 'single-float))
        (side (if (< (vx vel) 0) -1 1)))
    (rl:draw-texture-pro (gethash tex-key *textures*)
                         (rl:make-rectangle :x (* time-interval 192.0) :y (* row 192)
                                            :width (* side 192.0) :height 192.0)
                         (rl:make-rectangle :x (vx pos) :y (vy pos)
                                            :width 192.0 :height 192.0)
                         (vec 95.0 128.0)
                         0.0
                         :white)))

(defun game-draw ()
  (rl:clear-background (rl:make-rgba 71 171 169 1))
  (draw-ground)
  (rl:draw-texture-v (gethash 'tower *textures*) (vec 80 150) :white)
  (animate-sprite 'knight (anim-state knight1) (pos knight1) (velocity knight1))
  (animate-sprite 'archer 0 (pos archer1) (velocity archer1))
  (rl:draw-fps 10 5))

(defun game ()
  (let* ((screen-width 900)
         (screen-height 500))
    (rl:with-window (screen-width screen-height "RTS")
      (rl:set-target-fps 120)
      (game-init)
      (loop :until (rl:window-should-close)
            :do (game-input)
                (game-update)
                (rl:with-drawing
                  (game-draw)))
      (loop for value being the hash-values of *textures*
            do (rl:unload-texture value)))))
