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

let
  cfg = config.local.alacritty;
  themeCfg = config.local.theme;
in
{
  options.local.alacritty = with lib; {
    fontSize = mkOption {
      type = types.number;
      default = 11.0;
      description = "Alacritty font size";
    };
  };

  config = {
    programs.alacritty = {
      enable = true;
      settings = {
        window = {
          padding = { x = 5; y = 5; };
          decorations = "none";
          dynamic_title = true;
        };

        scrolling = {
          history = 10000;
          multiplier = 3;
        };

        font =
          let
            family = "monospace";
            mkFont = style: { inherit family style; };
          in
          {
            normal = mkFont "Regular";
            bold = mkFont "Bold";
            italic = mkFont "Italic";
            bold_italic = mkFont "Bold Italic";

            size = cfg.fontSize;
          };

        colors = {
          primary = {
            background = themeCfg.window.background;
            foreground = themeCfg.window.mainText;

            dim_foreground = themeCfg.window.mainText;
            bright_foreground = themeCfg.window.mainText;
          };

          cursor = {
            text = themeCfg.window.cursorText;
            cursor = themeCfg.window.cursor;
          };

          vi_mode_cursor = {
            text = themeCfg.window.cursorText;
            cursor = themeCfg.window.cursorVi;
          };

          search = {
            matches = {
              foreground = themeCfg.window.searchText;
              background = themeCfg.window.search;
            };
            focused_match = {
              foreground = themeCfg.window.searchText;
              background = themeCfg.window.searchFocused;
            };
            footer_bar = {
              foreground = themeCfg.window.footerText;
              background = themeCfg.window.footer;
            };
          };

          hints = {
            start = {
              foreground = themeCfg.window.hintsText;
              background = themeCfg.window.hintsStart;
            };
            end = {
              foreground = themeCfg.window.hintsText;
              background = themeCfg.window.hintsEnd;
            };
          };

          selection = {
            text = themeCfg.window.selectionText;
            background = themeCfg.window.selection;
          };

          normal = {
            black = themeCfg.window.regular.color0;
            red = themeCfg.window.regular.color1;
            green = themeCfg.window.regular.color2;
            yellow = themeCfg.window.regular.color3;
            blue = themeCfg.window.regular.color4;
            magenta = themeCfg.window.regular.color5;
            cyan = themeCfg.window.regular.color6;
            white = themeCfg.window.regular.color7;
          };

          bright = {
            black = themeCfg.window.bold.color8;
            red = themeCfg.window.bold.color9;
            green = themeCfg.window.bold.color10;
            yellow = themeCfg.window.bold.color11;
            blue = themeCfg.window.bold.color12;
            magenta = themeCfg.window.bold.color13;
            cyan = themeCfg.window.bold.color14;
            white = themeCfg.window.bold.color15;
          };

          dim = {
            black = themeCfg.window.regular.color0;
            red = themeCfg.window.regular.color1;
            green = themeCfg.window.regular.color2;
            yellow = themeCfg.window.regular.color3;
            blue = themeCfg.window.regular.color4;
            magenta = themeCfg.window.regular.color5;
            cyan = themeCfg.window.regular.color6;
            white = themeCfg.window.regular.color7;
          };

          indexed_colors = [
            {
              index = 16;
              color = themeCfg.window.extended.color16;
            }
            {
              index = 17;
              color = themeCfg.window.extended.color17;
            }
            {
              index = 18;
              color = themeCfg.window.extended.color18;
            }
            {
              index = 19;
              color = themeCfg.window.extended.color19;
            }
          ];
        };
      };
    };
  };
}
