package elements

import (
	"image"
	"mover/assets"
	"mover/gamedata"

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

type Laser struct {
	Sprite    *ebiten.Image
	position  gamedata.Coordinates
	angle     float64
	cycle     int
	firing    bool
	numcycles int
}

func NewLaser(pos gamedata.Coordinates, angle float64) *Laser {
	l := &Laser{
		Sprite:    ebiten.NewImage(200, 20),
		angle:     angle,
		cycle:     0,
		position:  pos,
		firing:    false,
		numcycles: 5,
	}
	return l
}

func (l *Laser) Update() error {
	l.cycle++
	return nil
}

func (l *Laser) Draw() {
	l.Sprite.Clear()
	//l.Sprite.Fill(color.White)

	idx := (l.cycle / 4) % l.numcycles
	x0 := 0
	y0 := 20 * idx
	x1 := 200
	y1 := y0 + 20

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

}

func (l *Laser) GetPosition() gamedata.Coordinates {
	return l.position
}

func (l *Laser) SetPosition(pos gamedata.Coordinates) {
	l.position = pos
}
func (l *Laser) GetAngle() float64 {
	return l.angle
}

func (l *Laser) SetAngle(a float64) {
	l.angle = a
}

func (l *Laser) SetFiring(b bool) {
	l.firing = b
}

func (l *Laser) IsFiring() bool {
	return l.firing
}
