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

let 
  cfg = config.samfelag.modules.desktop.tools.polybar;
  i3Cfg = config.samfelag.modules.desktop.wm.i3;
  polybar_pkg = pkgs.polybar.override {
    i3Support = true;
    pulseSupport = true;
  };
  script = ''
    polybar top &
    polybar bottom &
  '';
  # Paths
  paths = {
    config = ../../../config/polybar/config.ini;
    bars = ../../../config/polybar/bars;
    scripts = ../../../config/polybar/scripts;
  };
in {

  options.samfelag.modules.desktop.tools.polybar = {
    enable = lib.mkEnableOption "polybar";
  };

  config = lib.mkIf cfg.enable {

    # - Configuration ----------------------------

    environment.systemPackages = [
      polybar_pkg
    ];

    hm.xdg.configFile."polybar/config.ini".source = paths.config;
    hm.xdg.configFile."polybar/bars".source = paths.bars;
    hm.xdg.configFile."polybar/scripts".source = paths.scripts;

    # - Themeing ---------------------------------
    hm.xdg.configFile."polybar/colour-scheme.ini".text = ''
      [colour-scheme]
      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}
    '';

    # - Dependencies -----------------------------

    # The polybar modules use rofi
    samfelag.modules.desktop.tools.rofi.enable = true;
    samfelag.modules.desktop.wm.i3.extraKeybindings = lib.mkIf i3Cfg.enable {
      "${i3Cfg.mod}+Shift+w" = "exec $HOME/.config/rofi/menus/wifi/wifi.sh";
    };
    samfelag.modules.desktop.wm.i3.extraStartup = lib.mkIf i3Cfg.enable [
      { command = "systemctl --user restart polybar"; always = true; notification = false; }
      { command = "feh --bg-fill -B \"#${config.colorScheme.palette.base00}\" -z --no-fehbg ${cfg.wallpaper}"; always = true; notification = false; }
    ];

    # - Services ---------------------------------

    hm.systemd.user.services.polybar = {
      Unit = {
        Description = "Polybar status bar";
        PartOf = [ "tray.target" ];
        X-Restart-Triggers = [ "${paths.config}" ];
      };

      Service = {
        Type = "forking";
        PassEnvironment = "PATH";
        ExecStart =
          let scriptPkg = pkgs.writeShellScriptBin "polybar-start" script;
          in "${scriptPkg}/bin/polybar-start";
        Restart = "on-failure";
      };

      Install = { WantedBy = [ "tray.target" ]; };
    };
  };
}
