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

(require 'syd/base)
(require 'syd/org)
(require 'syd/leader)

(use-package evil-org-agenda
  :hook (org-agenda-mode . evil-org-agenda-mode)
  :straight nil
  :config
  (evil-org-agenda-set-keys)
  ;; Stay away from my leader key!
  (evil-define-key* 'motion evil-org-agenda-mode-map
    (kbd syd-leader-key) nil))

(use-package org-agenda
  :straight nil
  :general
  (:keymaps 'org-agenda-mode-map
            :states '(normal visual motion emacs insert)
            :major-modes t
            :prefix syd-localleader-key
            :non-normal-prefix syd-alt-localleader-key
            "t" #'org-agenda-todo
            "r" #'org-agenda-refile
            "q" #'org-agenda-set-tags
            "p p" #'org-agenda-priority
            "p u" #'org-agenda-priority-up
            "p d" #'org-agenda-priority-down
            "p k" #'org-agenda-priority-up
            "p j" #'org-agenda-priority-down
            "d d" #'org-agenda-deadline
            "d s" #'org-agenda-schedule)
  (:keymaps 'syd-leader-open-map
   "A" #'org-agenda)
  :custom
  ((org-agenda-files (list org-directory))
   (org-agenda-deadline-faces '((1.001 . error)
                                (1.0 . org-warning)
                                (0.5 . org-upcoming-deadline)
                                (0.0 . org-upcoming-distant-deadline)))       
   (org-agenda-window-setup 'current-window)
   (org-agenda-skip-unavailable-files t)
   ;; Shift the agenda to show the previous 3 days and the next 7 days
   ;; for better context on your week. The past is less important than
   ;; the future.
   (org-agenda-span 10)
   (org-agenda-start-on-weekday nil)
   (org-agenda-start-day "-3d")
   ;; Optimize `org-agenda' by inhibiting extra work while opening
   ;; agenda buffers in the background. They'll be "restarted" if the
   ;; user switches to them anyway (see
   ;; `syd-org-exclude-agenda-buffers-from-workspace-h')
   (org-agenda-inhibit-startup t)


   ;; The lengths of these leaders take account for our added
   ;; `syd-org--agenda-repeater'.
   (org-agenda-scheduled-leaders '("Sched" "S.%2dx"))
   (org-agenda-deadline-leaders '("Deadl" "In%2dd" "D.%2dx"))
   (org-agenda-timerange-leaders '("" ; Range within a single day.
                                   "%2d/%2d")) ; Range spanning many days.
   ;; Hide "upcoming deadlines" until the scheduled date.
   (org-agenda-skip-deadline-prewarning-if-scheduled 'pre-scheduled)
   ;; Hide completed tasks.
   (org-agenda-skip-scheduled-if-done t)
   (org-agenda-custom-commands
    '(("k" "My agenda for today"
       ((agenda
         ""
         ((org-agenda-span 1)
          (org-deadline-warning-days 0)))
        (agenda
         ""
         ((org-agenda-span 7)
          (org-agenda-start-day "+1d")
          (org-deadline-warning-days 0)
          (org-agenda-time-grid nil)
          (org-agenda-entry-types '(:deadline))
          (org-agenda-overriding-header "Upcoming deadlines"))))))))
  :config
  ;; Show the repeater (the repeat interval, e.g. +1d) in repeating agenda
  ;; entries.
  (defun syd-org--agenda-repeater ()
    "The repeater information (e.g. +1w) in the agenda."
    (let ((pom (org-get-at-bol 'org-marker)))
      (if (or (org-get-scheduled-time pom) (org-get-deadline-time pom))
          (format "%5s: " (or (org-get-repeat) ""))
        "┄┄┄┄┄┄┄┄┄┄┄┄")))

  ;; Add `syd-org--agenda-repeater' to the agenda prefix.
  (setcdr (assoc 'agenda org-agenda-prefix-format)
          " %i %-12:c%?-12t%s%(syd-org--agenda-repeater)"))

(provide 'syd/org/agenda)
