package tactic

import . "github.com/golangconf/gophers-and-dragons/game"

func ChooseCard(s State) CardType {
	if s.Can(CardHeal) && s.Avatar.HP < 28 {
		return CardHeal
	}

	stunCards := s.Deck[CardStun].Count
	parryCards := s.Deck[CardParry].Count
	powerCards := s.Deck[CardPowerAttack].Count
	fbCards := s.Deck[CardFirebolt].Count

	switch s.Creep.Type {
	case CreepClaws:
		if stunCards > 1 && !s.Creep.IsStunned() {
			return CardStun
		}

		if s.Creep.IsFull() && s.Avatar.HP < 7 {
			return CardRetreat
		}

		if parryCards > 3 && !s.Creep.IsStunned() {
			return CardParry
		}

		if powerCards > 2 && s.Creep.HP > 3 {
			return CardPowerAttack
		}
	case CreepFairy:
		if stunCards > 0 && !s.Creep.IsStunned() {
			return CardStun
		}

		if powerCards > 1 && s.Creep.HP > 3 {
			return CardPowerAttack
		}

		if s.Creep.HP == 3 && s.Avatar.MP > 4 {
			return CardMagicArrow
		}
	case CreepMummy:
		if s.Creep.IsFull() && !(s.Can(CardHeal) && s.Avatar.MP > 9 &&
			fbCards > 0 || s.Avatar.HP > 7) {
			return CardRetreat
		}

		if stunCards > 2 && !s.Creep.IsStunned() {
			return CardStun
		}

		if fbCards > 0 && s.Creep.HP > 7 && s.Avatar.MP > 5 {
			return CardFirebolt
		}

		if powerCards > 0 && s.Creep.HP > 3 {
			return CardPowerAttack
		}
	case CreepKubus:
		if s.Creep.IsFull() && !(s.Can(CardHeal) && s.Avatar.MP > 7 &&
			fbCards > 0 && s.Avatar.HP > 14 || s.Avatar.HP > 32) {
			return CardRetreat
		}

		if fbCards > 0 && s.Creep.HP > 4 && s.Avatar.MP > 3 {
			return CardFirebolt
		}
	case CreepDragon:
		if s.Can(CardParry) {
			return CardParry
		}

		if s.Can(CardStun) && !s.Creep.IsStunned() {
			return CardStun
		}

		if stunCards == 0 {
			hp := s.Avatar.HP
			drHP := s.Creep.HP

			for hp > -6 && drHP > 0 {
				hp -= 6

				if powerCards > 0 {
					drHP -= 4
					powerCards--
				} else {
					drHP -= 3
				}
			}

			if drHP > 0 {
				return CardRetreat
			}
		}

		if s.Can(CardPowerAttack) {
			return CardPowerAttack
		}
	}

	if s.Avatar.MP == 3 && s.Creep.HP == 3 && s.Creep.Type != CreepDragon {
		return CardMagicArrow
	}

	return CardAttack
}