;;; config.el -*- lexical-binding: t; -*-

;;; Fix non-POSIX SHELL
(setq shell-file-name
      (executable-find "bash"))
(let ((fish-exe (executable-find "fish")))
  (setq-default vterm-shell
                fish-exe)
  (setq-default explicit-shell-file-name
                fish-exe))

(setq doom-font (font-spec
                 :family "Maple Mono NF CN"
                 :size 20))
(setq doom-theme 'doom-gruvbox)
;;; Fix failure to loacate 'Symbols Nerd Font Mono' font
(setq nerd-icons-font-family "Maple Mono NF CN")

(setq telega-emoji-use-images
      nil)

(map! :map global-map
      "C-c t"
      telega-prefix-map)

(setq display-line-numbers-type
      'relative)

(add-hook 'telega-load-hook
          'telega-notifications-mode)

(defvar my/emacs-deps-exec-path (seq-filter
                                 (lambda (p)
                                   (string-match-p
                                    "/nix/store/.*-emacs-packages-deps/bin"
                                    p))
                                 exec-path))

(advice-add #'envrc--update
            :after #'(lambda ()
                       (setq exec-path
                             (append exec-path
                                     my/emacs-deps-exec-path))))

(advice-add #'telega-chatbuf-attach-clipboard
            :override #'(lambda (doc-p)
                          (interactive "P")
                          (let* ((selection-coding-system 'no-conversion) ;for rawdata
                                 (temporary-file-directory telega-temp-dir)
                                 (tmpfile (telega-temp-name "clipboard" ".png"))
                                 (coding-system-for-write 'binary))
                            (if (eq system-type 'darwin)
                                (progn
                                  ;; NOTE: On MacOS, try extracting clipboard using pngpaste
                                  (unless (executable-find "pngpaste")
                                    (error "Please install pngpaste to paste images"))
                                  (unless (= 0 (telega-screenshot-with-pngpaste tmpfile))
                                    (error "No image in CLIPBOARD")))
                              (let ((image-data (or (gui-get-selection 'CLIPBOARD 'image/png)
                                                    (gui-get-selection 'CLIPBOARD 'image/jpeg)
                                                    (error "No image in CLIPBOARD"))))
                                (write-region image-data nil tmpfile nil 'quiet)))
                            (telega-chatbuf-attach-media tmpfile (when doc-p 'preview)))))

(defun +telega-save-file-to-clipboard (msg)
  "Save file at point to clipboard.
NOTE: wayland only."
  (interactive (list (telega-msg-for-interactive)))
  (let ((file (telega-msg--content-file msg)))
    (unless file
      (user-error "No file associated with message"))
    (telega-file--download file
                           :priority 32
                           :update-callback
                           (lambda (dfile)
                             (telega-msg-redisplay msg)
                             (message "Wait for downloading to finish…")
                             (when (telega-file--downloaded-p dfile)
                               (let* ((fpath (telega--tl-get dfile :local :path)))
                                 (shell-command (format "wl-copy < \"%s\"" fpath))
                                 (message (format "File saved to clipboard: %s" fpath))))))))
