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

{
  # Home Manager needs a bit of information about you and the paths it should
  # manage.
  home.username = "jay";
  home.homeDirectory = "/home/jay";

  imports = [ 
    inputs.hyprpanel.homeManagerModules.hyprpanel 
    ./programs/bash.nix
    ./programs/git.nix
    ./programs/lazygit.nix
    ./programs/rofi.nix
    ./services/arrpc.nix
    ./services/fluidsynth.nix
    ./services/hypridle.nix
  ];


  # This value determines the Home Manager release that your configuration is
  # compatible with. This helps avoid breakage when a new Home Manager release
  # introduces backwards incompatible changes.
  #
  # You should not change this value, even if you update Home Manager. If you do
  # want to update the value, then make sure to first check the Home Manager
  # release notes.
  home.stateVersion = "24.11"; # Please read the comment before changing.

  # The home.packages option allows you to install Nix packages into your
  # environment.
  home.packages = [
    pkgs.legcord
    pkgs.libgtop
    #pkgs.mpvpaper
    # pkgs.hyprlandPlugins.hyprtrails
  ];

  # Home Manager is pretty good at managing dotfiles. The primary way to manage
  # plain files is through 'home.file'.
  home.file = {
  #  ".config/elinks/elinks.conf".source = ../../dotfiles/elinks/elinks.conf;
  #  ".config/jellyfin-tui/config.yaml".source = ../../dotfiles/jellyfin-tui/config.yaml;
  #  ".config/newsboat/config".source = ../../dotfiles/newsboat/config;
  #  "quickemu/kubuntu-24.04.conf".source = ../../dotfiles/quickemu/kubuntu-24.04.conf;
  };

  home.sessionVariables = {
    EDITOR = "nano";
  };

  programs.alacritty = lib.mkForce {
    enable = true;
    settings = {
      window.opacity = 0.05;
      # window.blur = true;
      font.normal = { 
        family = "BigBlueTermPlus Nerd Font"; 
        style = "Regular"; 
      };
    };
  };

  programs.yazi = {
    enable = true;
    package = pkgs.yazi;
    plugins = {
      "bypass" = pkgs.yaziPlugins.bypass;
      "chmod" = pkgs.yaziPlugins.chmod;
      "full-border" = pkgs.yaziPlugins.full-border;
      "lazygit" = pkgs.yaziPlugins.lazygit;
      "mediainfo" = pkgs.yaziPlugins.mediainfo;
      "no-status" = pkgs.yaziPlugins.no-status;
      "ouch" = pkgs.yaziPlugins.ouch;
      "restore" = pkgs.yaziPlugins.restore;
      "smart-enter" = pkgs.yaziPlugins.smart-enter;
      "toggle-pane" = pkgs.yaziPlugins.toggle-pane;
    };
  };
  
    
  programs.oh-my-posh = {
    enable = true;
    enableBashIntegration = true;
    useTheme = "blueish";
  };

  programs.rbw = {
    enable = true;
  #  settings = {
  #    email = "jay@zerojay.com";
  #    base_url = "https://bw.zerojay.com";
  #  };
  };

  # Let Home Manager install and manage itself.
  programs.home-manager.enable = true;

  programs.hyprlock = {
    enable = true;
  };

  # programs.starship.presets = [ "nerd-font-symbols" ];

  programs.hyprpanel = {
    enable = true;
    overlay.enable = true;
    hyprland.enable = true;
    settings = {
      layout = {
        "bar.layouts" = {
          "0" = {
            left = [ "dashboard" "separator" "microphone" "volume" "bluetooth" "separator" "network" "separator" "workspaces" "windowtitle" ];
            middle = [ "media" "cava" ];
            right = [ "cpu" "cputemp" "ram" "storage" "separator" "systray" "notifications" "separator" "weather" "clock" ];
          };
        };
      };

      bar = {
        customModules = {
          cava = {
            showIcon = false;
          };
          cpuTemp.sensor = "/sys/devices/pci0000\:00/0000\:00\:18.3/hwmon/hwmon2/temp3_input";
          netstat.rateUnit = "MiB";
          weather.unit = "metric";
          updates = {
            label = true;
            autoHide = false;
            padZero = true;
            pollingInterval = 14400000;
            updateCommand = "cd ~/nixos-config/ && nix flake update && nix build .#nixosConfigurations.nixos.config.system.build.toplevel && nvd diff /run/current-system ./result | grep -e '\[U' | wc -l";
            icon = {
              pending = "󰏗";
              updated = "󰏖";
            };
          };
        };

        launcher.autoDetectIcon = true;
        media = {
          truncation = true;
          truncation_size = 70;
        };
        workspaces = {
          show_icons = true;
          showApplicationIcons = true;
          ignored = "-\\\\d+";
        };
        network = {
          showWifiInfo = true;
          truncation = false;
          label = true;
        };
        windowtitle = {
          class_name = false;
          custom_title = false;
          truncation = true;
          truncation_size = 70;
        };
      };

      #theme.name = "nord";
      # Wipeout HD Theme
      #theme.name = "catppuccin_latte";
      # Wipeout HD Reverse Theme
      theme.name = "nord";

      menus.clock = {
        time = {
          military = true;
          hideSeconds = true;
        };
        weather = {
          location = "Montreal";
          key = "0bec3053c1c64737a5b42727251005";
          unit = "metric";
        };
      };

      menus.dashboard = {
        directories = {
          enabled = true;
          left = {
            directory1.command = "bash -c \"xdg-open $HOME/downloads/\"";
            directory2.command = "bash -c \"xdg-open $HOME/videos/\"";
            directory3.command = "bash -c \"xdg-open $HOME/games/\"";
            directory3.label = "  Games";
          };
          right = {
            directory1.command = "bash -c \"xdg-open $HOME/documents/\"";
            directory2.command = "bash -c \"xdg-open $HOME/pictures/\"";
            directory3.command = "bash -c \"xdg-open $HOME/\"";
          };
        };
        shortcuts = {
          left = {
            shortcut1 = {
              command = "zen-twilight";
              icon = "";
              tooltip = "Zen Twilight";
            };
            shortcut2 = {
              command = "steam --disable-gpu-compositing";
              icon = "";
              tooltip = "Steam";
            };
            shortcut3 = {
              command = "vesktop";
              tooltip = "Vesktop";
            };
          };

          right = {
            shortcut3.command = "hyprshot -zm region --clipboard-only";
          };
        };
        stats.enable_gpu = true;
        recording.path = "$HOME/Videos/recording";
      };

      menus.media = {
        displayTime = true;
        displayTimeTooltip = true;
      };

      theme = {
        bar = {
          transparent = true;
          floating = false;
        };
        
        font = {
          size = "12px";
          name = "BigBlueTermPlus Nerd Font";
        };
      };

      wallpaper.enable = false;

      #bar.workspaces.workspaceIconMap = {
      #  "1" = "";
      #  "3" = "󰙯";
      #  "4" = "󰄛";
      # };
    };
  };

  stylix = {
    enable = true;
    base16Scheme = "${pkgs.base16-schemes}/share/themes/nord.yaml";
    image = ../../wallpapers/nixos-wallpaper.png;
    #image = ./qat02.png;
    # Wipeout HD Theme
    #image = ./wipeout.png;
    # Wipeout HD Reverse Theme
    #image = ./wipeoutreverse.png;         
    targets = {
      # hyprpaper.enable = lib.mkForce false;
      mako.enable = false;
      qt.enable = false;
    };
    
  };

  wayland.windowManager.hyprland = {
    # Allow home-manager to configure hyprland
    enable = true;
    xwayland.enable = true;
    systemd.enable = true;

    plugins = [
      #pkgs.hyprlandPlugins.hyprtrails
      #pkgs.hyprlandPlugins.hyprbars
      #inputs.hyprland-plugins.packages."${pkgs.system}".hyprtrails
      #inputs.hypr-darkwindow.packages.${pkgs.system}.Hypr-DarkWindow
    ];

    settings = {
      "$terminal" = "alacritty";
      "$filemanager" = "dolphin";
      "$menu" = "rofi -show drun -show-icons";

      "$mainMod" = "SUPER"; 

      bind = [
        "$mainMod, Q, exec, $terminal"
        "$mainMod, C, killactive,"
        "$mainMod, M, exit,"
        "$mainMod, E, exec, $fileManager"
        "$mainMod, F, togglefloating,"
        "$mainMod, R, exec, $menu"
        "$mainMod, P, pseudo"
        "$mainMod, J, togglesplit"
        "$mainMod, l, exec, hyprlock"
        
        # "$mainMod, V, exec, cliphist list | rofi -dmenu | cliphost decode | wl-copy"
        "$mainMod, V, exec, $terminal --class clipse -e 'clipse'"

        "$mainMod, left, movefocus, l"
        "$mainMod, right, movefocus, r"
        "$mainMod, up, movefocus, u"
        "$mainMod, down, movefocus, d"

        "$mainMod, 1, workspace, 1"
        "$mainMod, 2, workspace, 2"
        "$mainMod, 3, workspace, 3"
        "$mainMod, 4, workspace, 4"
        "$mainMod, 5, workspace, 5"
        "$mainMod, 6, workspace, 6"
        "$mainMod, 7, workspace, 7"
        "$mainMod, 8, workspace, 8"
        "$mainMod, 9, workspace, 9"
        "$mainMod, 0, workspace, 10"

        "$mainMod SHIFT, Q, exec, hdrop --floating --gap 60 --width 50 $terminal --class alacritty"

        "$mainMod SHIFT, 1, movetoworkspace, 1"
        "$mainMod SHIFT, 2, movetoworkspace, 2"
        "$mainMod SHIFT, 3, movetoworkspace, 3"
        "$mainMod SHIFT, 4, movetoworkspace, 4"
        "$mainMod SHIFT, 5, movetoworkspace, 5"
        "$mainMod SHIFT, 6, movetoworkspace, 6"
        "$mainMod SHIFT, 7, movetoworkspace, 7"
        "$mainMod SHIFT, 8, movetoworkspace, 8"
        "$mainMod SHIFT, 9, movetoworkspace, 9"
        "$mainMod SHIFT, 0, movetoworkspace, 10"

        "$mainMod SHIFT, right, resizeactive, 10 0"
        "$mainMod SHIFT, left, resizeactive, -10 0"
        "$mainMod SHIFT, up, resizeactive, 0 -10"
        "$mainMod SHIFT, down, resizeactive, 0 10"

        "$mainMod, S, exec, hyprshot -zm region --clipboard-only"
        "$mainMod SHIFT, S, exec, hyprshot -zm region"

        "$mainMod, F9, exec, playerctl previous"
        "$mainMod, F10, exec, playerctl play-pause"
        "$mainMod, F11, exec, playerctl next"

        #"$mainMod, S, togglespecialworkspace, magic"
        #"$mainMod SHIFT, S, movetoworkspace, special:magic" 
      ];

      bindm = [
        "$mainMod, mouse:272, movewindow"
        "$mainMod, mouse:273, resizewindow"
      ];

      bindel = [
        ",XF86AudioRaiseVolume, exec, wpctl set-volume -l 1 @DEFAULT_AUDIO_SINK@ 5%+"
        ",XF86AudioLowerVolume, exec, wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-"
        ",XF86AudioMute, exec, wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"
        ",XF86AudioMicMute, exec, wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle"
        ",XF86MonBrightnessUp, exec, brightnessctl s 10%+"
        ",XF86MonBrightnessDown, exec, brightnessctl s 10%-"
      ];

      bindl = [
        " , XF86AudioNext, exec, playerctl next"
        " , XF86AudioPause, exec, playerctl play-pause"
        " , XF86AudioPlay, exec, playerctl play-pause"
        " , XF86AudioPrev, exec, playerctl previous"
      ];

      env = [
        "XCURSOR_SIZE,24"
        "HYPRCURSOR_SIZE,24"
        "LIBVA_DRIVER_NAME,nvidia"
        "__GLX_VENDOR_LIBRARY_NAME,nvidia"
        "ELECTRON_OZONE_PLATFORM_HINT,auto"
        "NVD_BACKEND,direct"
      ];

      general = {
        layout = "dwindle";
        gaps_in = 5;
        gaps_out = 20;
        border_size = 2;
        #"col.active_border" = "rgba(33ccffee) rgba(00ff99ee) 45deg";
        #"col.inactive_border" = "rgba(595959aa)";
        

        allow_tearing = true;
        resize_on_border = true;
      };

      misc = {
        enable_anr_dialog = false;
        vrr = 0;
      #  anr_missed_pings = 3;
        disable_hyprland_logo = true;
      };

      debug = {
        full_cm_proto = true;
      };

      decoration = {
        rounding = 10;
        rounding_power = 2;
        active_opacity = 1.0;
        inactive_opacity = 1.0;
        blur = {
          enabled = false;
          vibrancy = 0.1696;

          passes = 1;
          size = 3;
        };
        shadow = {
          enabled = true;
          range = 4;
          render_power = 3;
          #color = "rgba(1a1a1aee)";
        };
      };

      animations = {
        enabled = true;

        bezier = [
          "easeOutQuint,0.23,1,0.32,1"
          "easeInOutCubic,0.65,0.05,0.36,1"
          "linear,0,0,1,1"
          "almostLinear,0.5,0.5,0.75,1.0"
          "quick,0.15,0,0.1,1"
        ];

        animation = [
          "global, 1, 10, default"
          "border, 1, 5.39, easeOutQuint"
          "windows, 1, 4.79, easeOutQuint"
          "windowsIn, 1, 4.1, easeOutQuint, popin 87%"
          "windowsOut, 1, 1.49, linear, popin 87%"
          "fadeIn, 1, 1.73, almostLinear"
          "fadeOut, 1, 1.46, almostLinear"
          "fade, 1, 3.03, quick"
          "layers, 1, 3.81, easeOutQuint"
          "layersIn, 1, 4, easeOutQuint, fade"
          "layersOut, 1, 1.5, linear, fade"
          "fadeLayersIn, 1, 1.79, almostLinear"
          "fadeLayersOut, 1, 1.39, almostLinear"
          "workspaces, 1, 1.94, almostLinear, fade"
          "workspacesIn, 1, 1.21, almostLinear, fade"
          "workspacesOut, 1, 1.94, almostLinear, fade"
        ];
      };

      #"plugin:borders-plus-plus" = {
      #  add_borders = 1; # 0 - 9

        # you can add up to 9 borders
        #"col.border_1" = "rgb(ffffff)";
        #"col.border_2" = "rgb(2222ff)";

        # -1 means "default" as in the one defined in general:border_size
       # border_size_1 = 10;
       # border_size_2 = -1;

        # makes outer edges match rounding of the parent. Turn on / off to better understand. Default = on.
       # natural_rounding = "yes";
      #};

      #"plugin:hyprbars" = {
      #   enable = true;
      #};

      # Execute on startup.
      exec-once = [
        # "systemctl --user start hyprpolkitagent"
        "steam --disable-gpu-compositing" # nvidia pls let me have nice things
        #"vesktop"
        "legcord"
        # "[workspace 9 silent] ELECTRON_OZONE_PLATFORM_HINT=x11 1password" # fix for promts not showing up anymore
        "zen-twilight"
        # "${pkgs.xorg.xhost}/bin/xhost +"
        #"${pkgs.waybar}/bin/waybar &"
        #"${pkgs.dunst}/bin/dunst &"
        "clipse -listen"
        "streamcontroller"
        #"session-desktop"
        #"wl-clipboard-history -t"
        #"wl-paste --watch cliphist store"
        #"rm $HOME/.cache/cliphist/db"
        #"hyprpanel &"
      ];
      
      windowrulev2 = [
        "workspace 1 silent, class:^(zen-twilight)$"
        "workspace 2 silent, class:^(steam)$"
        "workspace 3 silent, class:^(legcord)$"
        "workspace 4 silent, class:^(com.core447.StreamController)$"
        "float,class:(clipse)$"
        "size 622 652,class:(clipse)$"
      ];

    };
  };

}

