package splashmenu

import (
	"cosmos/diego/groovy"
	splashmenu "cosmos/diego/groovy/examples/splashmenu/fonts"
	"image/color"

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

const (
	splashText = "schmoopysoft"
)

type Splash struct {
	Dimensions groovy.Area
	bgcolor    color.RGBA
	increment  int
	events     map[groovy.SceneEvent]func()
}

func NewSplash() Splash {
	return Splash{
		bgcolor: color.RGBA{0xFF, 0xFF, 0xFF, 0xFF},
		events:  make(map[groovy.SceneEvent]func()),
	}
}

func (s *Splash) Update() error {
	s.increment++

	s.bgcolor.R = (s.bgcolor.R - 1) % 0xFF
	s.bgcolor.G = (s.bgcolor.G - 1) % 0xFF
	s.bgcolor.B = (s.bgcolor.B - 1) % 0xFF

	if s.bgcolor.R == 0x00 {
		s.events[groovy.COMPLETED]()

		//scene cleanup (for next time)
		s.increment = 0
		s.bgcolor.R = 0xFF
		s.bgcolor.G = 0xFF
		s.bgcolor.B = 0xFF
	}

	return nil
}

func (s *Splash) Draw(screen *ebiten.Image) {
	screen.Fill(s.bgcolor)
	x := s.Dimensions.Width/2 - 400
	y := s.Dimensions.Height / 2
	text.Draw(screen, splashText, splashmenu.SplashFont.MegaTitle, x, y, color.White)

	text.Draw(screen, "\"how was the gravy\"\n- christorpher hollick", splashmenu.SplashFont.Title, x, y+40, color.White)
}

func (s *Splash) SetEventHandler(event groovy.SceneEvent, f func()) {
	s.events[event] = f
}

// sets sene dimensions
func (s *Splash) SetDimensions(a groovy.Area) {
	s.Dimensions = a
}
