{ pkgs, ... }:
let
  swwwMorning = "${pkgs.swww}/bin/swww img ${../wallpapers/tropic_island_morning.jpg}";
  swwwDay = "${pkgs.swww}/bin/swww img ${../wallpapers/tropic_island_day.jpg}";
  swwwEvening = "${pkgs.swww}/bin/swww img ${../wallpapers/tropic_island_evening.jpg}";
  swwwNight = "${pkgs.swww}/bin/swww img ${../wallpapers/tropic_island_night.jpg}";
  dynamicWallpaper = pkgs.writeShellScriptBin "dynamic-wallpaper" ''
    swww query || exit 1 # Check SWWW daemon is actually running
    case $(date +%H) in
        06 | 07 | 08) # Morning
            ${swwwMorning}
            ;;
        09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17) # Day
            ${swwwDay}
            ;;
        18 | 19 | 20) # Evening
            ${swwwEvening}
            ;;
        21 | 22 | 23 | 00 | 01 | 02 | 03 | 04 | 05) # Night
            ${swwwNight}
            ;;
    esac

  '';
in
{
  systemd.user = {
    timers.dynamic-wallpaper = {
      Install.WantedBy = [ "timers.target" ];
      Timer = {
        OnCalendar = "*:0";
        Unit = "dynamic-wallpaper.service";
      };
    };
    services.dynamic-wallpaper = {
      Unit = {
        Description = "Change wallpaper based on time of day";
        Wants = [ "graphical-session.target" ];
        After = [ "graphical-session.target" ];
      };
      Service = {
        Type = "oneshot";
        ExecStart = "${dynamicWallpaper}/bin/dynamic-wallpaper";
      };
    };
  };
  home.packages = with pkgs; [ swww ];
}