(in-package #:nyxt-user) ; While implicit, this allows SLY to know which package we are in.

(dolist (file (list (nyxt-init-file "slynk.lisp"))))
(dolist (file (list (nyxt-init-file "glyphs.lisp"))))

(load-after-system :slynk (nyxt-init-file "slynk.lisp"))
;; (defvar *chris-prompt-keymap (make-keymap "chris-prompt-map"))
;; (define-key *chris-prompt-keymap*
;;   "escape" 'cancel-input
;;   "C-j" 'select-next
;;   "C-k" 'select-previous)

;; (define-mode chris-prompt-mode ()
;;   "Dummy mode for the custom key bindings in `*chris-prompt-keymap*'."
;;   ((keymap-scheme (keymap:make-scheme
;;                    scheme:vi-insert *chris-prompt-keymap*))
;;    (visible-in-status-p nil)))

;; Always restore history and don't ask
(define-configuration browser
    ((session-restore-prompt :always-restore)))

(define-configuration (prompt-buffer)
  ((default-modes
    (append
     '(vi-insert-mode)
     %slot-default%))))

(define-configuration browser
  ((external-editor-program '("/usr/bin/emacsclient"))))

;; Create a function to launch mpv with given url
(defun mpv (url)
  "MPV launches with given url using the fast profile."
  (uiop:launch-program (list "mpv" "--profile=fast" url "&")))

;; Create a function to download videos with youtube-dl in alacritty
(defun youtube-dl (url)
  "Download videos and audio with youtube-dl in alacritty for feedback"
  (uiop:launch-program
   (list "alacritty" "-e" "yt-dlp" "-o ~/Videos/%(title)s.%(ext)s" url)))

;; Let's create a function to hint videos, convert the url to a sting, and play them in MPV
(define-command hint-mpv (&key nyxt/web-mode::annotate-visible-only-p)
                "Show a set of element hints, and copy the URL of the user inputted one."
                (nyxt/web-mode:query-hints "Copy element URL"
                                           (lambda (nyxt/web-mode::result)
                                             ;; this converts the url to a string to be used in mpv
                                             (let*
                                                 ((url
                                                   (format nil "~a"
                                                           (url (first nyxt/web-mode::result)))))
                                               ;; here we take that string and pipe it into mpv
                                               (mpv url)))
                                           :annotate-visible-only-p
                                           nyxt/web-mode::annotate-visible-only-p)
                (nyxt/)                )

;; Let's create a function to hint videos, convert the url to a sting, and download with ytdl
(define-command hint-ytdl (&key nyxt/web-mode::annotate-visible-only-p)
                "Show a set of element hints, and copy the URL of the user inputted one."
                (nyxt/web-mode:query-hints "Copy element URL"
                                           (lambda (nyxt/web-mode::result)
                                             ;; this converts the url to a string to be used in yt-dlp
                                             (let*
                                                 ((url
                                                   (format nil "~a"
                                                           (url (first nyxt/web-mode::result)))))
                                               ;; here we take that string and pipe it into yt-dlp
                                               (youtube-dl url)))
                                           :annotate-visible-only-p
                                           nyxt/web-mode::annotate-visible-only-p))

;; These are my own keys that are layered over vi-normal. A lot of these
;; are similar to qutebrowser.
(defvar *chris-keymap* (make-keymap "chris-map"))
(define-key *chris-keymap*
  "K" 'switch-buffer-next
  "J" 'switch-buffer-previous
  "b" 'switch-buffer
  "v" 'hint-mpv
  "V" 'hint-ytdl
  "d" 'delete-current-buffer
  "D" 'delete-buffer
  "r" 'reload-current-buffer
  "R" 'reload-buffers)

(define-mode chris-mode ()
  "Dummy mode for the custom key bindings in `*chris-keymap*'."
  ((keymap-scheme
    (keymap:make-scheme
     scheme:vi-normal *chris-keymap*))
   (visible-in-status-p nil)))

(define-configuration buffer
  ((default-modes
    (append
     '(vi-normal-mode
       auto-mode
       reduce-tracking-mode
       force-https-mode
       chris-mode)
     %slot-default%))))

(define-configuration buffer
  ((search-engines
    (list
     (make-instance 'search-engine
                    :shortcut "s"
                    :search-url "https://search.tfcconnection.org/?q=~a"
                    :fallback-url "https://search.tfcconnection.org")))))

(define-configuration nyxt/vi-mode:vi-normal-mode
  ((visible-in-status-p nil)))

(define-configuration nyxt/vi-mode:vi-insert-mode
  ((visible-in-status-p nil)))

(define-configuration status-buffer
  ((style
    (str:concat
     %slot-default%
     (cl-css:css
      '((body
         :background "#282a36"
         :color "#e2e4e5"
         :line-height "1fr")
        ("#container-vi"
         :grid-template-columns "0px 30px 2fr 0px 240px")
        ("#controls"
         :background-color "#282a36"
         :color "#f3f99d"
         :width "0px"
         :padding-left "0px"
         :hidden)
        ("#url"
         :background-color "#282a36"
         :color "#5af78e")
        ("#modes"
         :background-color "#282a36"
         :color "#f3f99d")
        ("#tabs"
         :background-color "#282a36"
         :color "#5af78e")
        (".tab"
         :background-color "#282a36"
         :color "#9aedfe")
        (".tab:hover"
         :background-color "#282a36"
         :color "#f1f1f0")
        (".button"
         :background-color "#282a36"
         :color "#5af78e")
        (".button:hover"
         :background-color "#282a36"
         :color "#f1f1f0")
        (".arrow"
         :width "0px"
         :height "0px")
        (".arrow-right"
         :background-color "#282a36"
         :color "#5af78e")
        (".arrow-left"
         :background-color "#282a36"
         :color "#5af78e")
        ))))))

(define-configuration internal-buffer
    ((style
      (str:concat
       %slot-default%
       (cl-css:css
        '((body
           :background "#282a36"
           :color "#e2e4e5")
          (.button
           :color "#e2e4e5")))))))

(define-configuration window
  ((message-buffer-style
    (str:concat
     %slot-default%
     (cl-css:css
      '((body
         :background-color "#282a36"
         :color "#e2e4e5")))))))

(define-configuration prompt-buffer
  ((style
    (str:concat
     %slot-default%
     (cl-css:css
      '((body
         :background-color "#282a36"
         :color "#e2e4e5")
        ("#prompt-area-vi"
         :background-color "#282a36"
         :color "#57c7ff")
        ("#prompt"
         :background-color "#282a36"
         :color "#e2e4e5")
        ("#input"
         :background-color "#282a36"
         :color "#e2e4e5")
        ("#suggestions"
         :background-color "#282a36"
         :color "#e2e4e5")
        (.vi-insert-mode
         :background-color "#282a36")
        (.vi-normal-mode
         :background-color "#282a36")
        (.source
         :margin-left "5px")
        (.source-name
         :background-color "#282a36"
         :color "#e2e4e5")
        (.source-content
         :background-color "#282a36"
         :color "#e2e4e5")
        ;; (".source-content td"
        ;;  :background-color "#282a36"
        ;;  :color "#e2e4e5")
        (".source-content th"
         :background-color "#43454f"
         :color "#e2e4e5")
        ("#selection"
         :background-color "#57c7ff"
         :color "#34353e")
        (.marked
         :background-color "#5af78e"
         :color "#34353e")
        (.selected
         :background-color "#57c7ff"
         :color "#34353e")
        (.button
         :color "#e2e4e5")))))))



(in-package #:nyxt-user) ; While implicit, this allows SLY to know which package we are in.

(load "~/quicklisp/setup.lisp")
;; (ql:quickload :slynk)

;; (define-command-global start-slynk (&optional (slynk-port *swank-port*))
;;   "Start a Slynk server that can be connected to, for instance, in
;; Emacs via SLY.

;; Warning: This allows Nyxt to be controlled remotely, that is, to execute
;; arbitrary code with the privileges of the user running Nyxt.  Make sure
;; you understand the security risks associated with this before running
;; this command."
;;   (slynk:create-server :port slynk-port :dont-close t)
;;   (echo "Slynk server started at port ~a" slynk-port))

;; (start-slynk)
