package elements

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

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

const (
	FG_MAXHEALTH = 100
)

type FlyGoblin struct {
	Sprite           *ebiten.Image
	Maks             *ebiten.Image
	MaksDest         *ebiten.Image
	position         gamedata.Coordinates
	target           gamedata.Coordinates
	state            gamedata.EnemyState
	cycle            int
	health           int
	damageduration   int
	right            bool
	touched          bool
	toggle           bool
	sploding         bool
	damage           bool
	called           bool
	deathcallback    func()
	fireballcallback func()
}

func NewFlyGoblin() *FlyGoblin {
	fg := &FlyGoblin{
		Sprite:         ebiten.NewImage(96, 96),
		Maks:           ebiten.NewImage(96, 96),
		MaksDest:       ebiten.NewImage(96, 96),
		health:         FG_MAXHEALTH,
		damageduration: 0,
		called:         false,
	}
	fg.Maks.Fill(color.White)
	return fg
}

func (f *FlyGoblin) Update() error {

	if f.damage {
		f.damageduration++
		if f.damageduration > 30 {
			f.damage = false
			f.damageduration = 0
		}
	}

	if f.state < gamedata.EnemyStateDying {
		dx := f.target.X - f.position.X
		dy := f.target.Y - f.position.Y

		f.right = dx/48 > 0

		f.position.X += dx / 48
		f.position.Y += dy / 48

		//10% chance to summon fireball
		fb := rand.Float64() >= 0.9

		if (f.cycle/8)%4 == 0 && fb {
			if f.fireballcallback != nil {
				f.fireballcallback()
			}
		}

	}

	if f.state == gamedata.EnemyStateDead && !f.called {
		f.called = true
		if f.deathcallback != nil {
			f.deathcallback()
		}
	}

	f.cycle++
	return nil
}

func (f *FlyGoblin) Draw() {
	f.Sprite.Clear()
	f.MaksDest.Clear()

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

	op := &ebiten.DrawImageOptions{}
	if f.right {
		op.GeoM.Scale(-1, 1)
		op.GeoM.Translate(MOVER_WIDTH*2, 0)
	}

	switch f.state {
	case gamedata.EnemyStateDefault:
		if !f.toggle {
			f.Sprite.DrawImage(assets.ImageBank[assets.Worm].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
		} else {
			f.Sprite.DrawImage(assets.ImageBank[assets.WormDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
		}
	case gamedata.EnemyStateHit:
		if (f.cycle/5)%2 == 0 && f.damage {
			f.MaksDest.DrawImage(assets.ImageBank[assets.WormDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
			op := &ebiten.DrawImageOptions{}
			op.GeoM.Reset()
			op.Blend = ebiten.BlendSourceAtop
			f.MaksDest.DrawImage(f.Maks, op)
			f.Sprite.DrawImage(f.MaksDest, nil)
		} else {
			f.Sprite.DrawImage(assets.ImageBank[assets.WormDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
		}
	case gamedata.EnemyStateExploding:
		op.GeoM.Scale(2, 2)
		//op.GeoM.Translate(-48, -48)
		f.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDying].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
		if idx == 3 {
			f.state = gamedata.EnemyStateDead

		}
	}

}

func (f *FlyGoblin) GetPosition() gamedata.Coordinates {
	return f.position
}

func (f *FlyGoblin) SetPosition(pos gamedata.Coordinates) {
	f.position = pos
}

func (f *FlyGoblin) SetTarget(target gamedata.Coordinates) {
	f.target = target
}

func (f *FlyGoblin) GetSprite() *ebiten.Image {
	return f.Sprite
}

func (f *FlyGoblin) GetEnemyState() gamedata.EnemyState {
	return f.state
}

func (f *FlyGoblin) SetHit() {
	f.health--
	f.damage = true
	f.state = gamedata.EnemyStateHit

	if f.health <= 0 {
		f.state = gamedata.EnemyStateExploding
		f.cycle = 0
	}
}

func (f *FlyGoblin) SetToggle() {
	f.toggle = !f.toggle

	if !f.toggle {
		f.state = gamedata.EnemyStateDefault
	}
}

func (f *FlyGoblin) IsToggled() bool {
	return f.toggle
}

func (f *FlyGoblin) SetTouched() {
	f.touched = true
}

func (f *FlyGoblin) ClearTouched() {
	f.touched = false
}

func (f *FlyGoblin) IsTouched() bool {
	return f.touched
}

func (f *FlyGoblin) ExplosionInitiated() bool {
	return f.sploding
}

func (f *FlyGoblin) SetExplosionInitiated() {
	f.sploding = true
}

func (f *FlyGoblin) Health() int {
	return f.health
}

func (f *FlyGoblin) MaxHealth() int {
	return FG_MAXHEALTH
}

func (f *FlyGoblin) SetDeathEvent(somefunc func()) {
	f.deathcallback = somefunc
}

func (f *FlyGoblin) SetFireballCallback(somefunc func()) {
	f.fireballcallback = somefunc
}

func (f *FlyGoblin) GetAngle() float64 {
	return 0
}
