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

let
  cfg = config.samfelag.modules.desktop.tools.greetd;
in {

  options.samfelag.modules.desktop.tools.greetd = with lib; {
    enable = mkEnableOption "Greetd based greeter";
    wm_cmd = lib.my.mkOpt' types.str "" "Window manager command";
  };

  config = lib.mkIf cfg.enable {

    environment.systemPackages = with pkgs; [
      greetd.gtkgreet
      cage
    ];

    services.greetd = {
      enable = true;
      settings = {
        default_session = {
          command = "${pkgs.cage}/bin/cage -d -s ${pkgs.greetd.gtkgreet}/bin/gtkgreet -- -c ${cfg.wm_cmd} -s /etc/greetd/greetd.css";
          user = config.user.name;
        };
      };
    };

    environment.etc."greetd/environments".text = ''
      ${cfg.wm_cmd}
    '';

    environment.etc."greetd/greetd.css".text = ''
      window {
        background-image: url("file://${config.samfelag.modules.desktop.wallpaper}");
        background-size: cover;
        background-position: center;
        font-family: Iosevka;
      }

      label#clock {
        font-size: 25px;
        color: #${config.colorScheme.palette.base00};
      }

      entry {
        color: #${config.colorScheme.palette.base05};
        padding: 0px 15px;
        background-color: #${config.colorScheme.palette.base00};
        border-color: #${config.colorScheme.palette.base02};
        border-radius: 20px;
      }

      button.combo {
        box-shadow: none;
        margin-left: 5px;
        border-radius: 20px;
        border-color: #${config.colorScheme.palette.base02};
        background-image: image(#${config.colorScheme.palette.base00});
        color: #${config.colorScheme.palette.base05};
      }

      menu {
        color: #${config.colorScheme.palette.base05};
        background-color: #${config.colorScheme.palette.base00};
      }

      menuitem {
        border-radius: 20px;
        background-color: #${config.colorScheme.palette.base00};
      }

      menuitem:hover {
        background-color: #${config.colorScheme.palette.base0C};
      }

      .text-button {
        border-radius: 20px;
        border-color: #${config.colorScheme.palette.base02};
        color: #${config.colorScheme.palette.base05};
        background-image: image(#${config.colorScheme.palette.base00});
      }


      box#body {
          padding: 50px;
      }
    '';
  };
}
