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

with lib;
let
  cfg = config.my.theming;

  background = "282a36";
  currentLine = "44475a";
  foreground = "f8f8f2";
  comment = "6272a4";
  cyan = "8be9fd";
  green = "50fa7b";
  orange = "ffb86c";
  pink = "ff79c6";
  purple = "bd93f9";
  red = "ff5555";
  yellow = "f1fa8c";
in
{
  config = mkIf (cfg.enable && cfg.scheme == "dracula") {
    home-manager.users.moritz = {
      programs = {
        fzf.colors = {
          "bg+" = "#${currentLine}";
          "fg+" = "#${foreground}";
          "hl+" = "#${purple}";
          bg = "#${background}";
          fg = "#${foreground}";
          header = "#${comment}";
          hl = "#${purple}";
          info = "#${orange}";
          marker = "#${pink}";
          pointer = "#${pink}";
          prompt = "#${green}";
          spinner = "#${orange}";
        };
        kitty.extraConfig =
          ''
            # https://draculatheme.com/kitty
            foreground            #f8f8f2
            background            #282a36
            selection_foreground  #ffffff
            selection_background  #44475a

            url_color #8be9fd

            # black
            color0  #21222c
            color8  #6272a4

            # red
            color1  #ff5555
            color9  #ff6e6e

            # green
            color2  #50fa7b
            color10 #69ff94

            # yellow
            color3  #f1fa8c
            color11 #ffffa5

            # blue
            color4  #bd93f9
            color12 #d6acff

            # magenta
            color5  #ff79c6
            color13 #ff92df

            # cyan
            color6  #8be9fd
            color14 #a4ffff

            # white
            color7  #f8f8f2
            color15 #ffffff

            # Cursor colors
            cursor            #f8f8f2
            cursor_text_color background

            # Tab bar colors
            active_tab_foreground   #282a36
            active_tab_background   #f8f8f2
            inactive_tab_foreground #282a36
            inactive_tab_background #6272a4

            # Marks
            mark1_foreground #282a36
            mark1_background #ff5555

            # Splits/Windows
            active_border_color #f8f8f2
            inactive_border_color #6272a4
          '';
        tmux.plugins = with pkgs.tmuxPlugins; [
          dracula
        ];
        zathura.extraConfig = ''
          set window-title-basename "true"
          set selection-clipboard "clipboard"

          # Dracula color theme for Zathura
          # Swaps Foreground for Background to get a light version if the user prefers

          #
          # Dracula color theme
          #

          set notification-error-bg       "#ff5555" # Red
          set notification-error-fg       "#f8f8f2" # Foreground
          set notification-warning-bg     "#ffb86c" # Orange
          set notification-warning-fg     "#44475a" # Selection
          set notification-bg             "#282a36" # Background
          set notification-fg             "#f8f8f2" # Foreground

          set completion-bg               "#282a36" # Background
          set completion-fg               "#6272a4" # Comment
          set completion-group-bg         "#282a36" # Background
          set completion-group-fg         "#6272a4" # Comment
          set completion-highlight-bg     "#44475a" # Selection
          set completion-highlight-fg     "#f8f8f2" # Foreground

          set index-bg                    "#282a36" # Background
          set index-fg                    "#f8f8f2" # Foreground
          set index-active-bg             "#44475a" # Current Line
          set index-active-fg             "#f8f8f2" # Foreground

          set inputbar-bg                 "#282a36" # Background
          set inputbar-fg                 "#f8f8f2" # Foreground
          set statusbar-bg                "#282a36" # Background
          set statusbar-fg                "#f8f8f2" # Foreground

          set highlight-color             "#ffb86c" # Orange
          set highlight-active-color      "#ff79c6" # Pink

          set default-bg                  "#282a36" # Background
          set default-fg                  "#f8f8f2" # Foreground

          set render-loading              true
          set render-loading-fg           "#282a36" # Background
          set render-loading-bg           "#f8f8f2" # Foreground

          #
          # Recolor mode settings
          #

          set recolor-lightcolor          "#282a36" # Background
          set recolor-darkcolor           "#f8f8f2" # Foreground

          #
          # Startup options
          #
          set adjust-open width
          set recolor true
        '';
        rofi.theme = "dracula";
      };
      xsession.windowManager.bspwm = {
        settings = {
          focused_border_color = "#${purple}";
          normal_border_color = "#${background}";
          active_border_color = "#${background}";
        };
      };
      services = {
        dunst.settings = {
          global = {
            # Defines color of the frame around the notification window.
            frame_color = "#282a36";

            # Define a color for the separator.
            # possible values are:
            #  * auto: dunst tries to find a color fitting to the background;
            #  * foreground: use the same color as the foreground;
            #  * frame: use the same color as the frame;
            #  * anything else will be interpreted as a X color.
            separator_color = "frame";
          };

          urgency_low = {
            background = "#282a36";
            foreground = "#6272a4";
            timeout = 10;
          };
          urgency_normal = {
            background = "#282a36";
            foreground = "#bd93f9";
            timeout = 10;
          };
          urgency_critical = {
            background = "#ff5555";
            foreground = "#f8f8f2";
            timeout = 0;
          };
        };
        polybar = {
          config = {
            "bar/bottom" = {
              background = "#${background}";
              foreground = "#${foreground}";
              border-color = "#${background}";
            };
            "module/bspwm" = {
              label-focused-foreground = "#${pink}";
              label-occupied-foreground = "#${comment}";
              label-urgent-foreground = "#${red}";
              label-empty-foreground = "#${currentLine}";
              label-separator-foreground = "#${background}";
            };
            "module/cpu" = {
              format-foreground = "#${background}";
              format-background = "#${green}";
            };
            "module/time" = {
              format-foreground = "#${background}";
              format-background = "#${cyan}";
            };
            "module/date" = {
              format-foreground = "#${background}";
              format-background = "#${yellow}";
            };
            "module/memory" = {
              format-foreground = "#${background}";
              format-background = "#${cyan}";
            };
            "module/pulseaudio" = {
              format-volume-foreground = "#${background}";
              format-volume-background = "#${purple}";
              label-muted = "%{F#${red}}婢 %{F#${background}}muted";
              format-muted-foreground = "#${background}";
              format-muted-background = "#${red}";
            };
            "module/network" = {
              format-connected-foreground = "#${background}";
              format-connected-background = "#${purple}";
            };
          };
        };
      };
      xdg.configFile = {
        "fish/conf.d/theme.fish".text = ''
          # Dracula Color Palette
          set -l foreground f8f8f2
          set -l selection 44475a
          set -l comment 6272a4
          set -l red ff5555
          set -l orange ffb86c
          set -l yellow f1fa8c
          set -l green 50fa7b
          set -l purple bd93f9
          set -l cyan 8be9fd
          set -l pink ff79c6

          # Syntax Highlighting Colors
          set -gx fish_color_normal $foreground
          set -gx fish_color_command $cyan
          set -gx fish_color_keyword $pink
          set -gx fish_color_quote $yellow
          set -gx fish_color_redirection $foreground
          set -gx fish_color_end $orange
          set -gx fish_color_error $red
          set -gx fish_color_param $purple
          set -gx fish_color_comment $comment
          set -gx fish_color_selection --background=$selection
          set -gx fish_color_search_match --background=$selection
          set -gx fish_color_operator $green
          set -gx fish_color_escape $pink
          set -gx fish_color_autosuggestion $comment
          set -gx fish_color_cancel $red --reverse
          set -gx fish_color_option $orange

          # Default Prompt Colors
          set -gx fish_color_cwd $green
          set -gx fish_color_host $purple
          set -gx fish_color_host_remote $purple
          set -gx fish_color_user $cyan

          # Completion Pager Colors
          set -gx fish_pager_color_progress $comment
          set -gx fish_pager_color_background
          set -gx fish_pager_color_prefix $cyan
          set -gx fish_pager_color_completion $foreground
          set -gx fish_pager_color_description $comment
          set -gx fish_pager_color_selected_background --background=$selection
          set -gx fish_pager_color_selected_prefix $cyan
          set -gx fish_pager_color_selected_completion $foreground
          set -gx fish_pager_color_selected_description $comment
          set -gx fish_pager_color_secondary_background
          set -gx fish_pager_color_secondary_prefix $cyan
          set -gx fish_pager_color_secondary_completion $foreground
          set -gx fish_pager_color_secondary_description $comment
        '';
        "rofi/dracula.rasi".text = ''
          * {
              /* Dracula theme colour palette */
              drac-bgd: #282a36;
              drac-cur: #44475a;
              drac-fgd: #f8f8f2;
              drac-cmt: #6272a4;
              drac-cya: #8be9fd;
              drac-grn: #50fa7b;
              drac-ora: #ffb86c;
              drac-pnk: #ff79c6;
              drac-pur: #bd93f9;
              drac-red: #ff5555;
              drac-yel: #f1fa8c;

              font: "FiraCode Nerd Font Mono 13";

              foreground: @drac-fgd;
              background-color: @drac-bgd;
              active-background: @drac-pnk;
              urgent-foreground: @foreground;
              urgent-background: @drac-red;

              selected-background: @active-background;
              selected-urgent-background: @urgent-background;
              selected-active-background: @active-background;
              separatorcolor: @active-background;
              bordercolor: #6272a4;
          }

          #window {
              background-color: @background-color;
              border:           3;
              border-radius: 6;
              border-color: @bordercolor;
              padding:          5;
          }
          #mainbox {
              border:  0;
              padding: 5;
          }
          #message {
              border:       1px dash 0px 0px ;
              border-color: @separatorcolor;
              padding:      1px ;
          }
          #textbox {
              text-color: @foreground;
          }
          #listview {
              fixed-height: 0;
              border:       2px dash 0px 0px ;
              border-color: @bordercolor;
              spacing:      2px ;
              scrollbar:    false;
              padding:      2px 0px 0px ;
          }
          #element {
              border:  0;
              padding: 1px ;
          }
          #element.normal.normal {
              background-color: @background-color;
              text-color:       @foreground;
          }
          #element.normal.urgent {
              background-color: @urgent-background;
              text-color:       @urgent-foreground;
          }
          #element.normal.active {
              background-color: @active-background;
              text-color:       @background-color;
          }
          #element.selected.normal {
              background-color: @selected-background;
              text-color:       @foreground;
          }
          #element.selected.urgent {
              background-color: @selected-urgent-background;
              text-color:       @foreground;
          }
          #element.selected.active {
              background-color: @selected-active-background;
              text-color:       @background-color;
          }
          #element.alternate.normal {
              background-color: @background-color;
              text-color:       @foreground;
          }
          #element.alternate.urgent {
              background-color: @urgent-background;
              text-color:       @foreground;
          }
          #element.alternate.active {
              background-color: @active-background;
              text-color:       @foreground;
          }
          #scrollbar {
              width:        2px ;
              border:       0;
              handle-width: 8px ;
              padding:      0;
          }
          #sidebar {
              border:       2px dash 0px 0px ;
              border-color: @separatorcolor;
          }
          #button.selected {
              background-color: @selected-background;
              text-color:       @foreground;
          }
          #inputbar {
              spacing:    0;
              text-color: @foreground;
              padding:    1px ;
          }
          #case-indicator {
              spacing:    0;
              text-color: @foreground;
          }
          #entry {
              spacing:    0;
              text-color: @drac-cya;
          }
          #prompt {
              spacing:    0;
              text-color: @drac-grn;
          }
          #inputbar {
              children:   [ prompt,textbox-prompt-colon,entry,case-indicator ];
          }
          #textbox-prompt-colon {
              expand:     false;
              str:        ":";
              margin:     0px 0.3em 0em 0em ;
              text-color: @drac-grn;
          }
          element-text, element-icon {
              background-color: inherit;
              text-color: inherit;
          }
        '';
      };
    };
  };
}
