package elements

import (
	"image"
	"math/rand/v2"
	"mover/assets"
	"mover/gamedata"

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

type RainSplash struct {
	Sprite   *ebiten.Image
	position gamedata.Coordinates
	cycle    int
	counter  int
}

func NewRainSplash() *RainSplash {
	rd := &RainSplash{
		Sprite:  ebiten.NewImage(10, 4),
		cycle:   rand.IntN(4),
		counter: 0,
	}
	return rd
}

func (rd *RainSplash) Update() error {
	rd.counter++
	rd.cycle++
	return nil
}

func (rd *RainSplash) Draw() {
	rd.Sprite.Clear()
	//rd.Sprite.Fill(color.White)

	idx := (rd.cycle / 8) % 4
	x0 := idx * 10
	y0 := 0
	x1 := x0 + 10
	y1 := 4

	rd.Sprite.DrawImage(assets.ImageBank[assets.RainSplash].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)

}

func (rd *RainSplash) GetPosition() gamedata.Coordinates {
	return rd.position
}

func (rd *RainSplash) SetPosition(pos gamedata.Coordinates) {
	rd.position = pos
}

func (rd *RainSplash) Expired() bool {
	return rd.counter > 30
}
