package main

import (
	"tetris/assets"
	"tetris/engine/audio"
	"tetris/engine/graphics"
	"tetris/engine/render"
	"tetris/game/state/handlers"
	"tetris/game/state/machine"

	rl "github.com/gen2brain/raylib-go/raylib"
)

func main() {
	render.Init(render.Config{
		Title:        "Tetris",
		WindowWidth:  685,
		WindowHeight: 600,
		RenderWidth:  685,
		RenderHeight: 600,
		ScaleFlags:   render.SCALE_INTEGER,
	})
	defer render.Exit()

	// Set window icon
	if icon := graphics.LoadImageFromMemory(".png", assets.Icon); icon != nil {
		rl.SetWindowIcon(*icon)
		rl.UnloadImage(icon)
	}

	audio.Init()
	defer audio.Exit()

	audio.LoadLibrary(assets.LoadSound())

	// Load texture
	texture := graphics.LoadTextureFromMemory(".png", assets.Sprite)
	defer rl.UnloadTexture(texture)
	render.SetTexture(texture)
	render.SetFont(&assets.Font)

	// Setup state machine.
	fsm := machine.New()
	fsm.Register("menu", handlers.NewMenu())
	fsm.Register("gameover", &handlers.GameOver{})
	fsm.Register("gameplay", handlers.NewGamePlay())
	fsm.Start("menu")

	// Enter game loop
	for !rl.WindowShouldClose() {
		audio.Update()
		if !fsm.Update(rl.GetFrameTime()) {
			break
		}
		fsm.Render()
	}
}
