package game

import (
	"openquell/assets"
	"openquell/config"
	"openquell/gameui"

	"github.com/ebitenui/ebitenui"
	"github.com/ebitenui/ebitenui/widget"
	"github.com/hajimehoshi/ebiten/v2"
)

type WelcomeScene struct {
	Game     *Game
	Next     SceneName
	Whoami   SceneName
	UseCache bool
	Ui       *ebitenui.UI
}

func NewWelcomeScene(game *Game) Scene {
	scene := &WelcomeScene{Whoami: Welcome, Game: game, Next: Welcome}
	scene.SetupUI()
	return scene
}

func (scene *WelcomeScene) SetNext(next SceneName) {
	scene.Next = next
}

func (scene *WelcomeScene) GetNext() SceneName {
	return scene.Next
}

func (scene *WelcomeScene) ResetNext() {
	scene.Next = scene.Whoami
}

func (scene *WelcomeScene) SetLevel(level int) {}

func (scene *WelcomeScene) Clearscreen() bool {
	return true
}

func (scene *WelcomeScene) Update() error {
	switch {
	case ebiten.IsKeyPressed(ebiten.KeyEnter):
		scene.SetNext(Menu)
	}

	scene.Ui.Update()

	return nil
}

func (scene *WelcomeScene) Draw(screen *ebiten.Image) {
	screen.Clear()

	scene.Ui.Draw(screen)
}

func (scene *WelcomeScene) SetupUI() {
	rowContainer := gameui.NewRowContainer()

	button := gameui.NewMenuButton("Start", *assets.FontRenderer.FontNormal,
		func(args *widget.ButtonClickedEventArgs) {
			scene.SetNext(Menu)
		})

	label := widget.NewText(
		widget.TextOpts.Text("Welcome to OpenQuell", *assets.FontRenderer.FontBig, config.FontColorFG),
		widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter),
	)

	rowContainer.AddChild(label)
	rowContainer.AddChild(button)

	scene.Ui = &ebitenui.UI{
		Container: rowContainer.Container(),
	}
}
