package game

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

	"github.com/ebitenui/ebitenui"
	"github.com/ebitenui/ebitenui/widget"

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

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

const ABOUT string = `
This is a little Quell clone written by
Thomas von Dein <tom@vondein.org>.

Download it on repo.daemon.de/openquell/.

Copyright (c) 2024 by Thomas von Dein.

Version: `

func NewAboutScene(game *Game) Scene {
	scene := &AboutScene{Whoami: About, Game: game, Next: About}

	scene.SetupUI()

	return scene
}

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

func (scene *AboutScene) SetNext(next SceneName) {
	slog.Debug("about setnext", "next", next)
	scene.Next = next
}

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

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

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

func (scene *AboutScene) Update() error {
	scene.Ui.Update()
	return nil
}

func (scene *AboutScene) Draw(screen *ebiten.Image) {
	scene.Ui.Draw(screen)
}

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

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

	label := widget.NewText(
		widget.TextOpts.Text("About this game", *assets.FontRenderer.FontBig, config.FontColorFG),
		widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter),
	)

	about := widget.NewText(
		widget.TextOpts.Text(ABOUT+config.VERSION, *assets.FontRenderer.FontNormal, config.FontColorFG),
		widget.TextOpts.Position(widget.TextPositionStart, widget.TextPositionCenter),
	)

	rowContainer.AddChild(label)
	rowContainer.AddChild(about)
	rowContainer.AddChild(buttonBack)

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