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

let
  cfg = config.samfelag.modules.app.alacritty;
  i3Cfg = config.samfelag.modules.desktop.wm.i3;
  hyprCfg = config.samfelag.modules.desktop.wm.hyprland;
in
{
  options.samfelag.modules.app.alacritty = {
    enable = lib.mkEnableOption "alacritty";
  };
  config = lib.mkIf cfg.enable {
    hm.home.packages = with pkgs; [
      alacritty
    ];

    fonts.fontconfig.enable = true;

    fonts.packages = with pkgs; [
      iosevka
      (nerdfonts.override { fonts = [ "Iosevka" ]; })
    ];

    hm.xdg.configFile."alacritty/alacritty.toml".source = ../../config/alacritty/alacritty.toml;

    # - Themeing ---------------------------------
    hm.xdg.configFile."alacritty/theme.toml".text = ''
    [colors]
    draw_bold_text_with_bright_colors = false

    [colors.primary]
    background = "0x${config.colorScheme.palette.base00}"
    foreground = "0x${config.colorScheme.palette.base05}"

    [colors.cursor]
    text = "0x${config.colorScheme.palette.base00}"
    cursor = "0x${config.colorScheme.palette.base05}"

    [colors.normal]
    black =   "0x${config.colorScheme.palette.base00}"
    red =     "0x${config.colorScheme.palette.base08}"
    green =   "0x${config.colorScheme.palette.base0B}"
    yellow =  "0x${config.colorScheme.palette.base0A}"
    blue =    "0x${config.colorScheme.palette.base0D}"
    magenta = "0x${config.colorScheme.palette.base0E}"
    cyan =    "0x${config.colorScheme.palette.base0C}"
    white =   "0x${config.colorScheme.palette.base05}"

    [colors.bright]
    black =   "0x${config.colorScheme.palette.base03}"
    red =     "0x${config.colorScheme.palette.base09}"
    green =   "0x${config.colorScheme.palette.base01}"
    yellow =  "0x${config.colorScheme.palette.base02}"
    blue =    "0x${config.colorScheme.palette.base04}"
    magenta = "0x${config.colorScheme.palette.base06}"
    cyan =    "0x${config.colorScheme.palette.base0F}"
    white =   "0x${config.colorScheme.palette.base07}"
    '';

    samfelag.modules.desktop.wm.i3.extraKeybindings = lib.mkIf i3Cfg.enable {
      "${i3Cfg.mod}+Return"       =  "exec ${pkgs.alacritty}/bin/alacritty";
      "${i3Cfg.mod}+Shift+Return" =  "exec ${pkgs.alacritty}/bin/alacritty -t floating-term";
    };

    samfelag.modules.desktop.wm.hyprland.extraKeybindings = lib.mkIf hyprCfg.enable [
      "${hyprCfg.mod}, Return, exec, ${pkgs.alacritty}/bin/alacritty"
      "${hyprCfg.mod} SHIFT, Return, exec, [float] ${pkgs.alacritty}/bin/alacritty"
    ];

  };
}
