{ config, inputs, lib, options, pkgs, ... }:

with lib;
let
  cfg = config.samfelag.modules.editors.emacs;
  i3Cfg = config.samfelag.modules.desktop.wm.i3;
  hyprCfg = config.samfelag.modules.desktop.wm.hyprland;
  emacsPackages = epkgs: [
    epkgs.vterm
  ];
  emacsWithPackages = with pkgs; ((emacsPackagesFor emacs-unstable).emacsWithPackages emacsPackages);
in {
  options.samfelag.modules.editors.emacs = {
    enable = mkEnableOption "emacs";
  };

  config = mkIf cfg.enable {
    nixpkgs.overlays = [ inputs.emacs-overlay.overlay ];

    environment.systemPackages = with pkgs; [
      ## Emacs itself
      binutils
      emacsWithPackages

      # emacsPgtkGcc

      ## Doom dependencies
      git
      (ripgrep.override { withPCRE2 = true; })
      gnutls

      ## Opt deps
      fd
      imagemagick
      pinentry-emacs
      zstd

      ## Modules deps
      # :tools vterm
      gcc
      gnumake
      libtool
      cmake

      # Fonts
      emacs-all-the-icons-fonts
      # fira-code
      # fira-code-symbols

      graphviz
    ];

    fonts.packages = with pkgs; [
      iosevka
      emacs-all-the-icons-fonts
    ];

    env.PATH = [ "$HOME/.config/emacs/bin" ];

    hm.xdg.configFile = {
      "doom/modules".source = ../../config/doom/modules;
      "doom/config.el".source = ../../config/doom/config.el;
      "doom/custom.el".source = ../../config/doom/custom.el;
      "doom/init.el".source = ../../config/doom/init.el;
      "doom/packages.el".source = ../../config/doom/packages.el;
      "doom/themes".source = ../../config/doom/themes;

      # # Themeing
      # "doom/themes/base16-samfelag-theme.el".text = ''
      #   (require 'base16-theme)
      #   (defvar base16-samfelag-theme-colors
      #     '(:base00 "#${config.colorScheme.palette.base00}"
      #       :base01 "#${config.colorScheme.palette.base01}"
      #       :base02 "#${config.colorScheme.palette.base02}"
      #       :base03 "#${config.colorScheme.palette.base03}"
      #       :base04 "#${config.colorScheme.palette.base04}"
      #       :base05 "#${config.colorScheme.palette.base05}"
      #       :base06 "#${config.colorScheme.palette.base06}"
      #       :base07 "#${config.colorScheme.palette.base07}"
      #       :base08 "#${config.colorScheme.palette.base08}"
      #       :base09 "#${config.colorScheme.palette.base09}"
      #       :base0A "#${config.colorScheme.palette.base0A}"
      #       :base0B "#${config.colorScheme.palette.base0B}"
      #       :base0C "#${config.colorScheme.palette.base0C}"
      #       :base0D "#${config.colorScheme.palette.base0D}"
      #       :base0E "#${config.colorScheme.palette.base0E}"
      #       :base0F "#${config.colorScheme.palette.base0F}")
      #     "All colors for Base16 Samfelag are defined here.")
      #   (deftheme base16-samfelag)
      #   (base16-theme-define 'base16-samfelag base16-samfelag-theme-colors)
      #   (provide-theme 'base16-samfelag)
      #   (provide 'base16-samfelag-theme)
      # '';
    };


    system.userActivationScripts = {
      installDoomEmacs = ''
        EMACS_FOLDER="$HOME/.config/emacs"
        if [ ! -d "$EMACS_FOLDER" ]; then
          ${pkgs.git}/bin/git clone --depth=1 --single-branch "https://github.com/doomemacs/doomemacs" "$EMACS_FOLDER"
        fi
      '';
    };

    samfelag.modules.desktop.wm.i3.extraKeybindings = lib.mkIf i3Cfg.enable {
      "${i3Cfg.mod}+Shift+e" =  "exec ${emacsWithPackages}/bin/emacs";
    };

    samfelag.modules.desktop.wm.hyprland.extraKeybindings = lib.mkIf hyprCfg.enable [
      "${hyprCfg.mod} SHIFT, e, exec, ${emacsWithPackages}/bin/emacs"
    ];

  };
}
