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

with lib;
let
  cfg = config.samfelag.modules.desktop.tools.hyprlock;
  hexToRGBString = inputs.nix-colors.lib-core.conversions.hexToRGBString ",";
in
{
  options.samfelag.modules.desktop.tools.hyprlock = {
    enable = mkEnableOption "Hyprlock screen locker";
  };

  config = mkIf cfg.enable {

    environment.systemPackages = with pkgs; [
      hypridle
    ];

    hm.programs.hyprlock = {
      enable = true;
      settings = {
        general = {
          # disable_loading_bar = true;
          # grace = 5;
        };

        background = [
          {
            path = "${config.samfelag.modules.desktop.wallpaper}";
            blur_passes = 2;
            blur_size = 4;
          }
        ];

        input-field = [
          {
            font_family = "Iosevka";
            size = "300, 40";
            position = "0, -80";
            outline_thickness = 1;
            fade_on_empty = "false";
            placeholder_text = "";

            outer_color = "rgba(${hexToRGBString config.colorScheme.palette.base02},1.0)";
            inner_color = "rgba(${hexToRGBString config.colorScheme.palette.base00},1.0)";
            font_color = "rgba(${hexToRGBString config.colorScheme.palette.base05},1.0)";

            check_color = "rgba(${hexToRGBString config.colorScheme.palette.base07},1.0)";
            fail_color = "rgba(${hexToRGBString config.colorScheme.palette.base06},1.0)";
          }
        ];

        label = [
          {
            text = "$TIME";
            color = "rgba(${hexToRGBString config.colorScheme.palette.base00},1.0)";
            font_size = 25;
            font_family = "Iosevka";

            position = "0, 80";
            halign = "center";
            valign = "center";
          }
        ];
      };
    };

    hm.services.hypridle = {
      enable = true;
      settings = {
        general = {
            lock_cmd = "pidof hyprlock || hyprlock";       # avoid starting multiple hyprlock instances.
            before_sleep_cmd = "loginctl lock-session";    # lock before suspend.
            after_sleep_cmd = "hyprctl dispatch dpms on";  # to avoid having to press a key twice to turn on the display.
        };

        listener = [
          {
              timeout = 90;
              on-timeout = "brightnessctl -s set 10";      # set monitor backlight to minimum, avoid 0 on OLED monitor.
              on-resume = "brightnessctl -r";              # monitor backlight restore.
          }

          {
              timeout = 90;
              on-timeout = "brightnessctl -sd platform::kbd_backlight set 0"; # turn off keyboard backlight.
              on-resume = "brightnessctl -rd platform::kbd_backlight";        # turn on keyboard backlight.
          }

          {
              timeout = 300;                               # 5min
              on-timeout = "loginctl lock-session";        # lock screen when timeout has passed
          }

          {
              timeout = 330;                               # 5.5min
              on-timeout = "hyprctl dispatch dpms off";    # screen off when timeout has passed
              on-resume = "hyprctl dispatch dpms on";      # screen on when activity is detected after timeout has fired.
          }

          {
              timeout = 1800;                              # 30min
              on-timeout = "systemctl suspend";            # suspend pc
          }
        ];
      };
    };

    # - Execute on startup -----------------------
    hm.wayland.windowManager.hyprland.settings.exec-once = [
      "hypridle"
    ];

  };
}
