package elements

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

	_ "embed"
	"image/color"
	_ "image/png"

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

const (
	MoverActionDefault = iota
	MoverActionDamaged
	MoverActionDying
	MoverActionExploding
	MoverActionDead
	MoverActionMax
)

type MoverAction uint

type Mover struct {
	Sprite          *ebiten.Image
	Maks            *ebiten.Image
	MaksDest        *ebiten.Image
	Angle           float64
	Pos             gamedata.Coordinates
	Origin          gamedata.Coordinates
	Action          MoverAction
	cycles          int
	rotating        bool
	Toggled         bool
	Hit             bool
	Touched         bool
	SplodeInitiated bool
	dyingcount      int
}

func NewMover() *Mover {
	m := &Mover{
		Sprite:          ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
		Maks:            ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
		MaksDest:        ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
		Action:          MoverActionDefault,
		cycles:          4,
		Angle:           0,
		rotating:        false,
		Toggled:         false,
		dyingcount:      0,
		SplodeInitiated: false,
	}

	m.Maks.Fill(color.White)
	return m
}

func (m *Mover) ToggleRotate() {
	m.rotating = !m.rotating
}

func (m *Mover) SetAngle(a float64) {
	m.Angle = a
}

func (m *Mover) SetOrigin(coords gamedata.Coordinates) {
	m.Origin = coords
	m.Pos = coords
}

func (m *Mover) Draw() {
	m.Sprite.Clear()
	m.MaksDest.Clear()

	idx := (m.cycles / 8) % 4

	y0 := 0
	y1 := 48
	x0 := 48 * idx
	x1 := x0 + 48

	switch m.Action {
	case MoverActionDefault:
		m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeNormal].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
	case MoverActionDamaged:
		m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
	case MoverActionDying:

		if (m.cycles/5)%2 == 0 {

			m.MaksDest.DrawImage(assets.ImageBank[assets.FlyEyeDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
			op := &ebiten.DrawImageOptions{}
			op.GeoM.Reset()
			op.Blend = ebiten.BlendSourceAtop
			m.MaksDest.DrawImage(m.Maks, op)
			m.Sprite.DrawImage(m.MaksDest, nil)

		} else {
			m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
		}
		if m.dyingcount >= 31 {
			m.cycles = 0
			m.SetHit()
		}
	case MoverActionExploding:
		m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDying].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
		if idx == 3 {
			m.SetHit()
		}
	default:
	}
}

func (m *Mover) Update() error {
	/*
		dx := 0. //40 * math.Cos(float64(m.cycles)/16)
		dy := 0. //40 * math.Sin(float64(m.cycles)/16)

		m.Pos = Coordinates{X: m.Origin.X + dx, Y: m.Origin.Y + dy}
	*/
	/*
		if m.rotating {
			m.Angle = float64(m.cycles) / (math.Pi * 2)
		}
	*/
	if m.Action == MoverActionDying {
		m.dyingcount++
	}
	m.cycles++
	return nil
}

func (m *Mover) SetHit() {
	m.Action++ // = (m.Action + 1) % MoverActionMax
}

func (m *Mover) ToggleColor() {
	//m.Toggled = !m.Toggled
	if m.Action == MoverActionDefault {
		m.Action = MoverActionDamaged
	} else if m.Action == MoverActionDamaged {
		m.Action = MoverActionDefault
	}
}
