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

let
  cfg = config.samfelag.modules.app.alacritty;
  i3Cfg = config.samfelag.modules.desktop.i3;
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.yml".source = ../../config/alacritty/alacritty.yml;

    # - Themeing ---------------------------------
    hm.xdg.configFile."alacritty/theme.yml".text = ''
    colors:
      primary:
        background: '0x${config.colorScheme.colors.base00}'
        foreground: '0x${config.colorScheme.colors.base05}'
      cursor:
        text: '0x${config.colorScheme.colors.base00}'
        cursor: '0x${config.colorScheme.colors.base05}'
      normal:
        black:   '0x${config.colorScheme.colors.base00}'
        red:     '0x${config.colorScheme.colors.base08}'
        green:   '0x${config.colorScheme.colors.base0B}'
        yellow:  '0x${config.colorScheme.colors.base0A}'
        blue:    '0x${config.colorScheme.colors.base0D}'
        magenta: '0x${config.colorScheme.colors.base0E}'
        cyan:    '0x${config.colorScheme.colors.base0C}'
        white:   '0x${config.colorScheme.colors.base05}'
      bright:
        black:   '0x${config.colorScheme.colors.base03}'
        red:     '0x${config.colorScheme.colors.base09}'
        green:   '0x${config.colorScheme.colors.base01}'
        yellow:  '0x${config.colorScheme.colors.base02}'
        blue:    '0x${config.colorScheme.colors.base04}'
        magenta: '0x${config.colorScheme.colors.base06}'
        cyan:    '0x${config.colorScheme.colors.base0F}'
        white:   '0x${config.colorScheme.colors.base07}'
    draw_bold_text_with_bright_colors: false
    '';

    samfelag.modules.desktop.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";
    };
  };
}
