;;; -*- lexical-binding: t; -*-

(require 'syd/base)
(require 'syd/popups)

(defun syd-dired-here ()
  (interactive)
  (dired default-directory))

(defun syd-dired-goto-file (file)
  "Like `dired-goto-file', but will act as `find-file' if FILE is inside another
directory."
  (interactive
   (prog1 (let ((dir (dired-current-directory)))
            (list (directory-file-name
                   (file-relative-name
                    (read-file-name "Goto file: " dir)
                    dir))))
     ;; let push-mark display its message
     (push-mark))
   dired-mode)
  (if (file-name-directory file)
      (find-file file)
    (dired-goto-file (expand-file-name file))))

(use-package dired
  ;; Built-in to Emacs.
  :straight nil
  :general
  (:keymaps 'syd-leader-open-map
            "-" #'syd-dired-here)
  (:keymaps 'syd-leader-project-map
            "-" #'project-dired)
  (:keymaps 'dired-mode-map
            :states '(normal motion)
            "g r" #'revert-buffer)
  (:keymaps 'dired-mode-map
            [remap dired-goto-file] #'syd-dired-goto-file)
  :commands dired-jump
  :custom (;; When there are other Dired windows open, suggest them as targets
           ;; for renaming/copying.
           (dired-dwim-target t)
           ;; Don't prompt to revert, just do it.
           (dired-auto-revert-buffer #'dired-buffer-stale-p)
           (dired-listing-switches
            (mapconcat
             #'identity
             '("-l" ; Mandatory!
               "--almost-all" ; Show hidden files; omit '.' and '..'.
               "--human-readable" ; Display sizes in human-readable units.
               "--time-style=+") ; Omit times/dates.
             " "))
           ;; Always copy recursively
           (dired-recursive-copies 'always)
           ;; Prompt for confirmation for each top-level directory being
           ;; deleted.
           (dired-recursive-deletes 'top)
           (dired-create-destination-dirs 'ask)
           (dired-create-destination-dirs-on-trailing-dirsep t)
           ;; Where to store image caches
           (image-dired-dir (concat syd-cache-dir "image-dired/"))
           (image-dired-db-file (concat image-dired-dir "db.el"))
           (image-dired-gallery-dir (concat image-dired-dir "gallery/"))
           (image-dired-temp-image-file (concat image-dired-dir "temp-image"))
           (image-dired-temp-rotate-image-file
            (concat image-dired-dir "temp-rotate-image"))
           ;; Increase thumbnail sizes.
           (image-dired-thumb-size 150))
  :config
  (syd-push shackle-rules
            '("^\\*image-dired"
              :size 0.8 :select t :inhibit-window-quit t))

  ;; On ESC, abort `wdired-mode' (will prompt)
  (syd-add-hook 'syd-escape-hook
    (defun syd-dired--wdired-exit-h ()
      (when (eq major-mode 'wdired-mode)
        (wdired-exit)
        t))))

(use-package diredfl
  :hook ((dired-mode . diredfl-mode)
         (dirvish-directory-view-mode . diredfl-mode)))

(use-package dirvish
  :custom ((dirvish-cache-dir (file-name-concat syd-cache-dir "dirvish/"))
           (dirvish-reuse-session 'open)
           (dirvish-default-layout '(0 0.4 0.6))
           (dirvish-hide-details '(dirvish dirvish-fd dirvish-side)))
  :general
  (:keymaps 'dired-mode-map
   "<tab>" #'dirvish-subtree-toggle
   "TAB" #'dirvish-subtree-toggle)
  :init
  ;; Ugh… I'm fairly certain this leads to eager loading of the entire package.
  (dirvish-override-dired-mode 1)
  :config
  ;; Fix random void-variable errors.
  (require 'autorevert))

(provide 'syd/dired)
