;; Drawing options

(setq red/render/status-line #T)
(setq red/render/status-line/key-sequence #T)

(setq red/render/mode-line #T)
(setq red/render/mode-line/arrow #T)

;; Drawing functions

(defun _red/colorize-mode
  (mode)
  (cond
    ((= mode 'normal) '(black cyan))
    ((= mode 'insert) '(black yellow))
    ((= mode 'command) '(black green))
    (&otherwise nil)))

(defun _red/render-mode-line
  (row mode name modified)
  (unless red/render/mode-line (return))
  (term/set-cursor row 0)
  (let (color (_red/colorize-mode mode))
    (when color
      (when (car color)
        (term/fg-color (car color)))
      (when (cadr color)
        (term/bg-color (cadr color))
        )
      )
    (term/write (+ " " (string/to-upper (->string mode)) " "))
    ;; Invert colors and draw the arrow
    (when color
      (when (and red/render/mode-line/arrow (cadr color))
        (term/fg-color (cadr color))
        (term/bg-color 'black)
        (term/write "🭬"))
      (term/reset)
      )
    )
  ;; Render bufer name
  (when name
    (term/write (+ " " name)))
  (when modified
    (term/write " [+]"))
  )

(defun _red/render-status-line
  (row)
  (let (message (cond
                  (_red/current-message (list _red/current-message 'red))
                  (_red/current-status (list _red/current-status 'white)))
                )
    (unless message (return))
    (term/set-cursor row 0)
    (when (cadr message)
      (term/fg-color (cadr message)))
    (term/write (car message))))

(defun _red/render-key-line
  (row width)
  (let (keys (string/join (map ->string _red/key-sequence) "+"))
    (unless keys (return))
    (term/set-cursor row (- width (+ (string/length keys) 1)))
    (term/write keys)
    ))

(defun _red/render-command (row command)
  (term/set-cursor row 0)
  (term/write ":")
  (when command
    (term/write command)))

(defun _red/render-bottom-bar
  (width height)
  (let* (mode (red/buffer/mode) top-mode (car mode) buffer-mode (cadr mode))
    (when (= top-mode 'command) (setq buffer-mode 'command))

    ;; Display mode line
    (_red/render-mode-line
      (- height _red/mode-line-offset)
      buffer-mode
      (red/buffer/name)
      (red/buffer/modified?))

    (when (= top-mode 'normal)
      ;; Status text
      (_red/render-status-line (- height _red/status-line-offset))
      ;; Current key sequence
      (_red/render-key-line (- height _red/status-line-offset) width)
      )
    (when (= top-mode 'command)
      ;; Render command
      (_red/render-command (- height _red/status-line-offset) _red/current-command))
    )
  )

(defun _red/root-post-render-hook (width height)
  (_red/render-bottom-bar width height)
  (term/reset)
  ;; If not in command mode, set cursor to buffer
  (when (= 'normal (car (red/buffer/mode)))
    (red/cursor-to-buffer))
  )

;; command line + status line
(defun _red/update-render-params ()
  (setq _red/status-line-offset 1)
  (setq _red/mode-line-offset 2)
  (setq red/bottom-margin (+ (and red/render/mode-line 1) 1)))

(_red/update-render-params)
