using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Left is Player, right is enemy.
public enum Side { left = 1, right = -1 }

public class CombatManager : MonoBehaviour
{
    [SerializeField] private List<Unit> leftSideUnits = new List<Unit>();
    [SerializeField] private List<Unit> rightSideUnits = new List<Unit>();
    [SerializeField] private CellMap cellMap;

    // Start is called before the first frame update
    void Start()
    {
        // hack
        cellMap = FindObjectOfType<CellMap>();

        foreach (var unit in rightSideUnits)
        {
            foreach (var ability in unit.Abilities)
            {
                ability.OnPlaceUnit(unit);
            }
        }

        foreach (var unit in rightSideUnits)
        {
            foreach (var ability in unit.Abilities)
            {
                ability.OnEnterBattleGround(unit);
            }
        }
    }

    List<Unit> QueryTargets(Unit attacker)
    {
        // TO DO:
        return new List<Unit>();
    }

    // Called once before drawing cards
    void BeginFight()
    {

    }

    void BeginRound(Side side)
    {
        List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
        foreach (var unit in units)
        {
            foreach (var ability in unit.Abilities)
            {
                ability.OnBeginRound(unit);
            }
        }
    }

    void BeginMovePhase(Side side)
    {
        List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
        foreach (var unit in units)
        {
            foreach (var ability in unit.Abilities)
            {
                ability.OnBeginMovePhase(unit);
            }
        }
    }

    void EndMovePhase(Side side)
    {
        List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
        foreach (var unit in units)
        {
            foreach (var ability in unit.Abilities)
            {
                ability.OnEndMovePhase(unit);
            }
        }
    }

    void BeginAttackPhase(Side side)
    {
        List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
        foreach (var unit in units)
        {
            foreach (var ability in unit.Abilities)
            {
                ability.OnBeginAttackPhase(unit);
            }
        }
    }

    void UnitAttack(Unit unit, List<Unit> targets)
    {
        foreach (var ability in unit.Abilities)
        {
            ability.OnBeginAttack(unit, targets);
        }

        foreach (var ability in unit.Abilities)
        {
            ability.OnAttack(unit, targets);
        }

        foreach (var ability in unit.Abilities)
        {
            ability.OnEndAttack(unit, targets);
        }

        foreach (var target in targets)
        {
            foreach (var ability in target.Abilities)
            {
                ability.OnCounterAttack(target, unit);
            }
        }
    }

    void Attack(Side side)
    {
        List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
        
        foreach (var unit in units)
        {
            UnitAttack(unit, QueryTargets(unit));
        }
    }

    void EndAttackPhase(Side side)
    {
        List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
        foreach (var unit in units)
        {
            foreach (var ability in unit.Abilities)
            {
                ability.OnEndAttackPhase(unit);
            }
        }
    }

    void EndRound(Side side)
    {
        List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
        foreach (var unit in units)
        {
            foreach (var ability in unit.Abilities)
            {
                ability.OnEndRound(unit);
            }
        }
    }

    // Called once after result
    void EndFight()
    {

    }
}
