package elements

import (
	"ducky/gamedata"
	"image"

	"github.com/hajimehoshi/ebiten/v2"
	"golang.org/x/exp/rand"
)

const (
	DuckyWidth  = 36
	DuckyHeight = DuckyWidth
)

type Ducky struct {
	Sprite   *ebiten.Image
	asset    *ebiten.Image
	cycle    int
	position gamedata.Coordinates
}

func NewDucky(asset *ebiten.Image) *Ducky {
	d := &Ducky{
		Sprite: ebiten.NewImage(DuckyWidth, DuckyHeight),
		asset:  asset,
		cycle:  rand.Intn(11),
	}
	return d
}

func (d *Ducky) Update() {
	d.cycle++
}

func (d *Ducky) Draw() {
	d.Sprite.Clear()

	idx := d.cycle / 4 % 10
	x0 := idx * DuckyWidth
	y0 := 0
	x1 := x0 + DuckyWidth
	y1 := DuckyHeight

	d.Sprite.DrawImage(d.asset.SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
}

func (d *Ducky) SetPosition(pos gamedata.Coordinates) {
	d.position = pos
}

func (d *Ducky) GetPosition() gamedata.Coordinates {
	return d.position
}
