package sim

import (
	"slices"

	"tinyadventure/internal/model"
)

func (b *battle) choose(actor *combatant, extra bool) model.Ability {
	allies, foes := b.side(actor.side), b.side(1-actor.side)
	ready := map[string]model.Ability{}
	for _, a := range actor.abils {
		if actor.cd[a.ID] == 0 && !(extra && a.ID == "haste") {
			ready[a.ID] = a
		}
	}
	a := func(id string) float64 { return ready[id].Power }
	use := func(id string, when bool) (model.Ability, bool) {
		a, ok := ready[id]
		return a, ok && when
	}
	low := 0
	hurt := false
	for _, al := range allies {
		if al.hp*10 < al.stats.MaxHP*7 {
			low++
		}
		if al != actor && al.hurt() {
			hurt = true
		}
	}
	for _, try := range []struct {
		id   string
		when bool
	}{
		{"prayer", low >= 2},
		{"heal", slices.ContainsFunc(allies, (*combatant).hurt)},
		{"taunt", hurt && !actor.hurt()},
		{"dodge", actor.hurt()},
		{"sneak", actor.hurt()},
		{"ward", !actor.under("ward") && b.threatens(foes, allies)},
		{"rage", b.payback(actor, foes, a("rage")*b.best(actor, foes, 1))},
		{"rally", b.payback(actor, foes, a("rally")*b.team(allies, foes, 1))},
		{"mark", b.payback(actor, foes, a("mark")*b.team(allies, foes, 1)/float64(len(foes)))},
		{"haste", len(allies) > 1 && b.payback(actor, foes, b.best(b.hasteTarget(actor), foes, 0))},
	} {
		if a, ok := use(try.id, try.when); ok {
			return a
		}
	}
	best, bestScore := mustAbility(StrikeID), -1.0
	for _, a := range slices.Concat(actor.abils, []model.Ability{best}) {
		if (a.Kind != KindDamage && a.Kind != KindAoE) || actor.cd[a.ID] > 0 {
			continue
		}
		if s := b.expected(actor, a, foes); s > bestScore {
			best, bestScore = a, s
		}
	}
	return best
}

func (b *battle) payback(actor *combatant, foes []*combatant, gain float64) bool {
	return gain > b.best(actor, foes, 0)
}

func (b *battle) best(c *combatant, foes []*combatant, slack int) float64 {
	top := 0.0
	for _, a := range slices.Concat(c.abils, []model.Ability{mustAbility(StrikeID)}) {
		if (a.Kind == KindDamage || a.Kind == KindAoE) && c.cd[a.ID] <= slack {
			top = max(top, b.expected(c, a, foes))
		}
	}
	return top
}

func (b *battle) team(allies, foes []*combatant, slack int) float64 {
	n := 0.0
	for _, al := range allies {
		n += b.best(al, foes, slack)
	}
	return n
}

func (b *battle) expected(actor *combatant, a model.Ability, foes []*combatant) float64 {
	typ := actor.typeOf(a)
	total, top := 0.0, 0.0
	for _, f := range foes {
		v := float64(max(actor.statFor(a), 1)) * a.Power
		if slices.Contains(f.unit.Weak, typ) {
			v *= weakMult
		}
		if f.guarded(typ) && !actor.has("pierce") {
			v = v * 10 / float64(10+f.defense())
		}
		total += v
		top = max(top, v)
	}
	if a.Kind == KindAoE {
		return total
	}
	return top
}

func (b *battle) threatens(foes, allies []*combatant) bool {
	for _, f := range foes {
		for _, a := range f.abils {
			typ := f.typeOf(a)
			if (a.Kind == KindDamage || a.Kind == KindAoE) && slices.ContainsFunc(allies, func(al *combatant) bool { return !al.guarded(typ) }) {
				return true
			}
		}
		if typ := f.typeOf(mustAbility(StrikeID)); slices.ContainsFunc(allies, func(al *combatant) bool { return !al.guarded(typ) }) {
			return true
		}
	}
	return false
}
