{ config, pkgs, lib, ... }:
with lib;
with types;
let
  cfg = config.modules.hyprland;
in
{
  options.modules.hyprland = {
    enable = mkEnableOption "Hyprland Wrapper";

    # monitor setup
    monitor = mkOption {
      type = listOf str;
      description = '''';
      example = '''';
    };

    # hyprpaper
    hyprpaper = {
      enable = mkEnableOption "Hyprpaper wrapper";
      wallpaper = mkOption {
        type = types.path;
        default = [];
        description = ''
          Wallpaper to be set on every monitor
        '';
        example = ''
          ./wallpaper.jpg
        '';
      };
    };
  };

  # TODO expose monitor setup


  # TODO expose color scheme / add OLED mode!

  # TODO start script

  config = mkIf cfg.enable {
    services.hyprpaper = mkIf cfg.hyprpaper.enable {
      enable = true;
      settings = {
        preload = [ "${cfg.hyprpaper.wallpaper}" ];
        wallpaper = [ ",${cfg.hyprpaper.wallpaper}" ];
      };
    };
    # hyprland setup
    wayland.windowManager.hyprland = {
      enable = true;
      xwayland.enable = true;
      settings = {
        ecosystem = {
          no_donation_nag = true;
        };
        workspace = [
          "1, monitor:HDMI-A-1,  default:true, persistent:true"
          "2, monitor:DP-1    ,  default:true, persistent:true"
          "special:scratchpad ,  on-created-empty:foot"
        ];
        monitor = [
          "DP-1    , 2560x1440@144, 1920x0, 1"
          "HDMI-A-1, 1920x1080@75 , 0x0   , 1"
        ];
        input = {
          kb_layout = "de";
          kb_variant = "us";
          follow_mouse = 1;
        };
        "$mod" = "SUPER";
        "$modd" = "SUPER_SHIFT";
        general = {
          gaps_in = 8;
          gaps_out = 15;
          border_size = 3;

          resize_on_border = true;
          layout = "dwindle";
          
          "col.active_border" = "rgba(cba6f7ff) rgba(89b4faff) rgba(94e2d5ff) 10deg";
          "col.inactive_border" = "0xff45475a";
          "col.nogroup_border" = "0xff89dceb";
          "col.nogroup_border_active" = "0xfff9e2af";
        };
        decoration = {
          blur = {
            new_optimizations = true;
            size = 1;
            passes = 1;
          };
          shadow = {
            enabled = true;
            range = 100;
            render_power = 5;
            color = "0x33000000";
            color_inactive = "0x22000000";
          };
          rounding = 15;
        };
        animations = {
          enabled = 1;
          bezier = "overshot,0.13,0.99,0.29,1.1";
          animation = [
            "windows,1,4,overshot,slide"
            "border,1,10,default"
            "fade,1,10,default"
            "workspaces,1,6,overshot,slidevert"
          ];
        };
        misc = {
          disable_hyprland_logo = true;
        };
        bind = [
          # opening programs
          "$mod, Return, exec, foot"
          "$mod, D, exec, tofi-drun | xargs hyprctl dispatch exec --"
          "$mod, M, exit, "
          "$mod, Q, killactive, "

          # moving around
          "$mod, left , movefocus, l"
          "$mod, right, movefocus, r"
          "$mod, down , movefocus, d"
          "$mod, up   , movefocus, u"

          # moving windows
          "$modd, left , movewindow, l"
          "$modd, right, movewindow, r"
          "$modd, down , movewindow, d"
          "$modd, up   , movewindow, u"

          # workspaces
          "$mod, 1, workspace, 1"
          "$mod, 2, workspace, 2"
          "$mod, 3, workspace, 3"
          "$mod, 4, workspace, 4"
          "$mod, 5, workspace, 5"
          "$mod, 6, workspace, 6"
          "$modd, 1, movetoworkspacesilent, 1"
          "$modd, 2, movetoworkspacesilent, 2"
          "$modd, 3, movetoworkspacesilent, 3"
          "$modd, 4, movetoworkspacesilent, 4"
          "$modd, 5, movetoworkspacesilent, 5"
          "$modd, 6, movetoworkspacesilent, 6"

          # fullscreen
          "$mod, F11, fullscreen, 0"
          # "$mod, m, fullscreen, 1" # maximize

          # floating
          "$mod, F, togglefloating, active"

          # screenshot
          ", Print, exec, grimblast copysave area $HOME\"/screenshots/\"$(date +'%F-%T.png');"

          # scratchpad
          "$mod, C, togglespecialworkspace, scratchpad"

          # status bar
          "$mod, T, exec, eww open --toggle top-bar"
        ];
        bindm = [
          "$mod, mouse:272, movewindow"
        ];
        bindle = [
          ", XF86AudioRaiseVolume, exec, wpctl set-volume @DEFAULT_AUDIO_SINK@ 1%+"
          ", XF86AudioLowerVolume, exec, wpctl set-volume @DEFAULT_AUDIO_SINK@ 1%-"
          ", XF86AudioMute, exec, wpctl set-volume @DEFAULT_AUDIO_SINK@ 0%"
        ];
        exec-once = [
          "bash ~/Git/nixos/gunther/hypr/start.sh"
          "dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_CURRENT_DESKTOP"
        ];
      };
    };

    # tell electron to use wayland
    home.sessionVariables.NIXOS_OZONE_WL = "1";

    # terminal
    programs.foot = {
      enable = true;
      settings = {
        main = {
          font = "monospace:size=12";
          dpi-aware = "yes";
        };
        colors = {
          foreground = "cad3f5";
          background = "24273a";
          regular0 = "494d64";
          regular1 = "ed8796";
          regular3 = "eed49f";
          regular4 = "8aadf4";
          regular5 = "f5bde6";
          regular6 = "8bd5ca";
          regular7 = "b8c0e0";
          bright0 = "5b6078";
          bright1 = "ed8796";
          bright3 = "eed49f";
          bright4 = "8aadf4";
          bright5 = "f5bde6";
          bright6 = "8bd5ca";
          bright7 = "a5adcb";
        };
      };
    };

    services.mako = {
      enable = true;
      extraConfig = ''
        background-color=#24273a
        text-color=#cad3f5
        border-color=#8aadf4
        progress-color=over #363a4f
        sort=-time
        layer=overlay
        width=300
        height=110
        border-size=2
        border-radius=15
        max-icon-size=64
        default-timeout=5000
        ignore-timeout=1

        [urgency=high]
        border-color=#f5a97f
      '';
    };
  };
}