;;; kkk-org.el --- Org Mode Configuration            -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Vinicius Teshima <vini.tes@pm.me>

;; Author: Vinicius Teshima <vini.tes@pm.me>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Org Config

;;; Code:

(require 'kkk-utils)

(defvar kkk-org--capture-folder "~/Sync/Documents/Emacs/Captures/")
(defvar kkk-org--agenda-folder "~/Sync/Documents/Emacs/Agenda/")

(kkk-utils-mkdir kkk-org--capture-folder)
(kkk-utils-mkdir kkk-org--agenda-folder)

(defun kkk-org--capture-folder+file (FILE)
  "Will concat `kkk-org--capture-folder' with FILE."
  (concat kkk-org--capture-folder FILE))

(defun kkk-org--agenda-folder+file (FILE)
  "Will concat `kkk-org--agenda-folder' with FILE."
  (concat kkk-org--agenda-folder FILE))

(defconst kkk-org--capture-work-task-template
  "** %^{Task Number} :task:
:PROPERTIES:
:DATE_CREATE: %^U
:DESC: %^{Enter the Description}
:SECS: 0
:MERGED: N
:END:

*** COMMENT Links
**** Task
**** PR
***** QA
****** Description
#+begin_src markdown
#+end_src
***** PROD
****** Description
#+begin_src markdown
#+end_src

%i
%?")


(use-package org-agenda
  :straight nil
  :bind (("C-c a" . org-agenda))
  :config
  (setq org-agenda-span 'day
	org-agenda-start-with-log-mode t)
  (setq org-agenda-files (list (kkk-org--agenda-folder+file "Agenda.org")))
  )

(defun kkk-org--make-work-capture-template (keys what)
  "KEYS WHAT."
  (list keys what 'entry
	(list 'file+headline "~/Sync/Work/BRANCHES.org" what)
	kkk-org--capture-work-task-template))

(defun kkk-org--make-work-templates (prefix list)
  "PREFIX LIST."
  (unless (symbolp list)
    (error "LIST *MUST* be a SYMBOL"))
  (unless (stringp prefix)
    (error "PREFIX *MUST* be a STRING"))

  (let ((s_ (shell-command-to-string
	     "grep '^* ' ~/Sync/Work/BRANCHES.org | tr -d '* '")))
    (let* ((i_ 0)
	   (t_ (string-lines s_))
	   (l_ (length t_)))
      (while (< i_ l_)
	(let ((it_ (nth i_ t_)))
	  (unless (string-empty-p it_)
	    (add-to-list list (kkk-org--make-work-capture-template
			       (concat prefix (int-to-string i_)) it_)
			 t)
	    )
	  )
	(setq i_ (1+ i_))
	)
      )
    )
  )

(use-package org-capture
  :straight nil
  :bind (("C-c c" . org-capture))
  :init
  (setq org-capture-templates
	(list
	 (list "t" "Todo"
	       'entry
	       (list 'file+headline
		     (kkk-org--capture-folder+file "Todo.org")
		     "Tasks")
	       "* TODO %?\n%i\n%a")

	 (list "j" "Journal"
	       'entry
	       (list 'file+headline
		     (kkk-org--capture-folder+file "Journal.org")
		     "Journal")
	       "* %?\nEntered on %U\n%i\n%a")

	 (list "i" "Idea"
	       'entry
	       (list 'file+headline
		     (kkk-org--capture-folder+file "Ideas.org")
		     "Ideas")
	       "* %?\n%i\n%a")

	 '("w" "Work")
	 '("wt" "Task")
	 )
	)
  (kkk-org--make-work-templates "wt" 'org-capture-templates)
  )

(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (python . t)
   (shell . t)))

(require 'org-tempo)

(dolist (it '(("sh" . "src shell") ("el" . "src emacs-lisp")
	      ("py" . "src python") ("c" . "src C") ("lu" . "src lua")
	      ("md" . "src markdown") ("sql" . "src sql")))
  (add-to-list 'org-structure-template-alist it))

(use-package org
  :config
  (setq org-ellipsis " ▼"
	org-src-preserve-indentation t
	org-return-follows-link t
	;; Org-Agenda
	org-log-done 'time
	org-log-into-drawer t)
  (setq org-todo-keywords '((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d!)"))))

(provide 'kkk-org)
;;; kkk-org.el ends here
