package main

import (
	"fmt"
	"log"
	"log/slog"
	"openquell/config"
	"openquell/game"
	"os"

	"github.com/alecthomas/repr"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/tlinden/yadu"
)

const (
	width    int = 640
	height   int = 384
	cellsize int = 32
)

func main() {
	ebiten.SetWindowSize(width, height)
	ebiten.SetWindowTitle("openquell")

	config, err := config.InitConfig()
	if err != nil {
		log.Fatal(err)
	}

	repr.Print(config)
	if config.TPS > 0 {
		fmt.Printf("Setting TPS to %d\n", config.TPS)
		ebiten.SetMaxTPS(config.TPS)
	}

	logLevel := &slog.LevelVar{}
	opts := &yadu.Options{
		Level:     logLevel,
		AddSource: true,
	}

	if config.Debug {
		logLevel.Set(slog.LevelDebug)
	}

	handler := yadu.NewHandler(os.Stdout, opts)

	debuglogger := slog.New(handler)
	slog.SetDefault(debuglogger)

	g := game.NewGame(width, height, cellsize, config, game.Welcome)

	err = ebiten.RunGame(g)
	if err != nil {
		log.Fatalf("unable to run game: %s", err)
	}

}
