{ config, pkgs, lib, ... }:
let
  cfg = config.nix-config.style;

  powerMenu = pkgs.writeShellScriptBin "power-menu" ''
    options=" Power Off\n Reboot\n Lock\n Suspend"
    selected=$(echo -e "$options" | fuzzel --dmenu --prompt "Power Menu" --lines 4 --width 20)

    case "$selected" in
      " Power Off") systemctl poweroff ;;
      " Reboot") systemctl reboot ;;
      " Lock") hyprlock ;;
      " Suspend") systemctl suspend ;;
      *) exit 1 ;;
    esac
  '';

  filePicker = pkgs.writeShellScriptBin "file-picker" ''
    files=$(${lib.getExe pkgs.fd} . $HOME --type f)
    filesShortened=$(echo "$files" | sed "s|$HOME|~|g" | awk -F/ '{print $(NF)" ("$0")"}')

    selected=$(echo "$filesShortened" | fuzzel --dmenu --prompt "🔎 " --placeholder "File search" --index)


    if [ -n "$selected" ]; then
      # Since fuzzel was launched with the --index option the actual path is now files[selected]
      selectedPlusOne=$((selected + 1))
      file=$(echo "$files" | sed -n "''${selectedPlusOne}p")
      xdg-open "$file"
      # plus one
    else
      echo "No file selected"
      exit 1
    fi
  '';

  emojiPicker = pkgs.writeShellScriptBin "emoji-picker" ''
    curl -s https://raw.githubusercontent.com/littleboycoding/emoji-dmenu/refs/heads/master/emoji.txt | fuzzel --dmenu | cut -d ' ' -f 1 | wl-copy
  '';
in
{

  options.nix-config = {
    powerMenu = lib.mkOption {
      description = "Package to use as a power menu";
      type = lib.types.package;
      default = powerMenu;
    };

    launcher = lib.mkOption {
      description = "Configuration for the launcher";
      type = lib.types.package;
      default = pkgs.fuzzel;
    };

    filePicker = lib.mkOption {
      description = "Package to use as a file picker";
      type = lib.types.package;
      default = filePicker;
    };

    emojiPicker = lib.mkOption {
      description = "Package to use as a emoji picker";
      type = lib.types.package;
      default = emojiPicker;
    };
  };

  config = {
    programs.fuzzel = {
      enable = true;

      settings = {
        main = {
          font = "monospace:size=${builtins.toString (cfg.fontSizes.applications - 1)}";
          terminal = "${lib.getExe pkgs.kitty}";
          layer = "overlay"; #or top?
          use-bold = true;
          dpi-aware = true;
          icons-enabled = true;
          match-mode = "fzf";
          horizontal-pad = cfg.fontSizes.applications + 5;
          vertical-pad = cfg.fontSizes.applications + 5;
          inner-pad = cfg.fontSizes.applications;
          show-actions = true;
          lines = 12;
          width = 50;
        };

        colors = {
          background = "000000aa";
          text = "ffffffaa";

          # prompt

          # placeholder

          # input

          match = "ffffffaa";

          selection = "00000000";

          selection-text = "ffffffbb";

          selection-match = "ffffffaa";
        };

        border = {
          width = 0;
          radius = 15;
        };
      };
    };


    home.packages = with pkgs; [
      powerMenu
      filePicker
      emojiPicker
    ];
  };
}
