(in-package :cl-user)
(defpackage :opencortex.tui
  (:use :cl :croatoan)
  (:export :main))
(in-package :opencortex.tui)

(defvar *daemon-host* "127.0.0.1")
(defvar *daemon-port* 9105)
(defvar *socket* nil)
(defvar *stream* nil)
(defvar *chat-history* (list))
(defvar *status-text* "Connecting...")
(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t))
(defvar *is-running* t)
(defvar *queue-lock* (bt:make-lock))
(defvar *incoming-msgs* nil)

(defun enqueue-msg (msg)
  (bt:with-lock-held (*queue-lock*)
    (push msg *incoming-msgs*)))

(defun dequeue-msgs ()
  (bt:with-lock-held (*queue-lock*)
    (let ((msgs (nreverse *incoming-msgs*)))
      (setf *incoming-msgs* nil)
      msgs)))

(defun listen-thread ()
  (loop while *is-running* do
    (handler-case
        (when (and *stream* (open-stream-p *stream*))
          (let ((line (read-line *stream* nil :eof)))
            (if (eq line :eof)
                (setf *is-running* nil)
                (let* ((*read-eval* nil)
                       (sexp (ignore-errors (read-from-string line))))
                  (if (and sexp (listp sexp))
                      (cond
                        ((eq (getf sexp :type) :status)
                         (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" 
                                                     (getf sexp :scribe)
                                                     (getf sexp :gardener))))
                        ((eq (getf sexp :type) :chat)
                         (enqueue-msg (getf sexp :text)))
                        ((eq (getf sexp :type) :info)
                         (enqueue-msg (format nil "*System*: ~a" (getf sexp :text))))
                        ((eq (getf sexp :type) :error)
                         (enqueue-msg (format nil "*Error*: ~a" (getf sexp :text))))
                        (t (enqueue-msg line)))
                      (enqueue-msg line))))))
      (error () (setf *is-running* nil)))
    (sleep 0.05)))

(defun main ()
  (handler-case
      (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*))
    (error (e) (format t "Error connecting: ~a~%" e) (return-from main)))
  (setf *stream* (usocket:socket-stream *socket*))
  (bt:make-thread #'listen-thread :name "tui-listener")
  
  (unwind-protect
    (with-screen (scr :input-echoing nil :input-blocking nil :enable-colors t :cursor-visible t)
      (let* ((h (height scr))
             (w (width scr))
             (chat-win (make-window (list (- h 2) w 0 0)))
             (status-win (make-window (list 1 w (- h 2) 0)))
             (input-win (make-window (list 1 w (- h 1) 0))))
        
        (loop while *is-running* do
          ;; Handle incoming messages
          (let ((new-msgs (dequeue-msgs)))
            (when new-msgs
              (dolist (msg new-msgs)
                (push msg *chat-history*)
                (setf *chat-history* (subseq *chat-history* 0 (min (length *chat-history*) 500))))
              
              (clear chat-win)
              (let ((line-num 0))
                (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3)))))
                  (add-string chat-win m :y line-num :x 0)
                  (incf line-num)))
              (refresh chat-win)))

          ;; Render Status Bar
          (clear status-win)
          (add-string status-win *status-text* :attributes '(:reverse))
          (refresh status-win)

          ;; Handle Keyboard Input
          (let ((ch (get-char scr)))
            (when ch
              (cond
                ((eq ch #\Newline)
                 (let ((cmd (coerce *input-buffer* 'string)))
                   (setf (fill-pointer *input-buffer*) 0)
                   (format *stream* "~a~%" cmd)
                   (finish-output *stream*)
                   (when (string= cmd "/exit") (setf *is-running* nil))))
                ((eq ch :backspace)
                 (when (> (length *input-buffer*) 0)
                   (decf (fill-pointer *input-buffer*))))
                ((characterp ch)
                 (vector-push-extend ch *input-buffer*)))
              
              (clear input-win)
              (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string)))
              (refresh input-win)))
          
          (sleep 0.02))))
    (setf *is-running* nil)
    (when *socket* (usocket:socket-close *socket*))))
