package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/xyproto/wallutils"
)

func slideshow(ch <-chan string) {
	dir := <-ch
	ticker := time.NewTicker(time.Duration(config.Duration) * time.Minute)

	go func() {
		for {
			err := setRandomWallpaper(dir)
			if err != nil {
				fmt.Printf("Failed to set random wallpaper from directory %v.", dir)
				panic(err)
			}
			select {
			case dir = <-ch:
				log.Println("directory set!")
				continue
			case <-ticker.C:
				continue
			}
		}
	}()
}

func setRandomWallpaper(dir string) error {
	img, err := pickRandomImage(dir)
	if err != nil {
		return err
	}

	desktopEnv, bool := os.LookupEnv("DESKTOP_SESSION")
	if !bool {
		panic("cannot determine desktop environment")
	}
	var mode string
	switch desktopEnv {
	case "cinnamon":
		mode = "zoom"
	case "hyprland":
		mode = "fit"
	default:
		mode = ""
	}

	return wallutils.SetWallpaperCustom(img, mode, false)
}
