/*
 For:            hyprpaper, https://github.com/hyprwm/hyprpaper
 Author:         https://github.com/5hubham5ingh
 Prerequisite:   hyprpaper daemon should be running
 Changes:        Set to change wallpaper on all screens
*/

export function setWallpaper(wallpaperPath) {
  // OS.exec(["hyprctl", "-q", "hyprpaper unload all"]);
  // OS.exec(["hyprctl", "-q", `hyprpaper wallpaper ,${wallpaperPath}`]);

  // For swww
  OS.exec(["swww", "img", wallpaperPath, "--transition-type=any", "--transition-duration=1"]);

  // For hyprpaper
  // OS.exec(["hyprctl", "-q", `hyprpaper reload , ${wallpaperPath}`]);

  // SYMLINK
  // Extract directory and extension
  const lastSlashIndex = wallpaperPath.lastIndexOf('/');
  const directory = wallpaperPath.substring(0, lastSlashIndex + 1);
  
  const lastDotIndex = wallpaperPath.lastIndexOf('.');
  const extension = lastDotIndex > lastSlashIndex ? wallpaperPath.substring(lastDotIndex) : '';
  
  // Create lockpaper path with same extension
  const lockpaperPath = `${directory}lockpaper${extension}`;
  
  // DELETE ANY OLD IMAGES
  //OS.exec(["zsh", "-c", `rm ${directory}lockpaper*`]);
  // Create symlink from wallpaper to lockpaper
  //OS.exec(["zsh", "-c", `ln -s ${wallpaperPath} ${lockpaperPath}`]);
  OS.exec(["zsh", "-c", `ln -sf ${wallpaperPath} /home/nathan/.config/lockpaper`]);
}
