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

public class MinionControllerScript : CharacterScript {

    public enum AgeType { Baby, Adult };
    public AgeType currentAge = AgeType.Baby;

    public const int turnsToMature = 5;
    public int turnsLeftToMature = 5;

    #region Actions
    override public void NextAction()
    {
        if (currentAge == AgeType.Baby)
        {
            if (currentAction == Action.Wait)
            {
                //ResetActionPoints();
                currentAction = Action.Move;
            }
            else if (currentAction == Action.Move)
            {
                currentAction = Action.Wait;
            }
        }
        else if (currentAge == AgeType.Adult)
        {
            if (currentAction == Action.Wait)
            {
                //ResetActionPoints();
                currentAction = Action.Move;
            }
            else if (currentAction == Action.Move)
            {
                currentAction = Action.Attack;
            }
            else if (currentAction == Action.Attack)
            {
                currentAction = Action.Defend;
            }
            else if (currentAction == Action.Defend)
            {
                currentAction = Action.Wait;
            }
            else if (currentAction == Action.Dodge)
            {
                currentAction = Action.Infect;
            }
            //else if (currentAction == Action.Infect)
            //{
            //    currentAction = Action.Wait;
            //}
        }
    }

    override public void HandleActions()
    {
        if(currentAge == AgeType.Baby)
        {
            if (currentAction == Action.Wait)
            {
                //ResetActionPoints();
            }
            else if (currentAction == Action.Move)
            {
                MovePlayer();
            }
        }
        else if (currentAge == AgeType.Adult)
        {
            if (currentAction == Action.Wait)
            {
                //ResetActionPoints();
            }
            else if (currentAction == Action.Move)
            {
                MovePlayer();
            }
            else if (currentAction == Action.Attack)
            {
                Attack();
            }
            else if (currentAction == Action.Defend)
            {
                Defend();
            }
            //else if (currentAction == Action.Infect)
            //{
            //    Infect();
            //}
        }
    }
    #endregion

    #region Phase Functions

    #region Infect
    override protected void Infect()
    {

    }

    override protected void InfectTargeter()
    {

    }
    #endregion

    #region Defend
    override protected void Defend()
    {

    }

    override protected void ChargeDefencePoints()
    {

    }
    #endregion

    #region Attack
    override protected void Attack()
    {

    }

    override protected void AttackTargeter()
    {

    }
    #endregion

    #region Movement
    override protected void MovePlayer()
    {
        FindMovementTargetSquare();
        currentXLoc = targetXLoc;
        currentYLoc = targetYLoc;

        NextAction();
        lastActionPoints = currentActionPoints;
        locationsTargeted.Clear();
        turnsLeftToMature -= 1;
    }

    override protected void FindMovementTargetSquare()
    {
        float rand = Random.Range(0, 4);//5 options none and cardinal directions
        bool isValidMovement = false;
        int newXLoc = targetXLoc;
        int newYLoc = targetYLoc;
        while (isValidMovement == false)
        {
            if (rand == 1)
            {
                newYLoc -= 1;
                currentActionPoints -= 1;
            }
            else if (rand == 2)
            {
                newXLoc += 1;
                currentActionPoints -= 1;
            }
            else if (rand == 3)
            {
                newYLoc += 1;
                currentActionPoints -= 1;
            }
            else if (rand == 4)
            {
                newXLoc -= 1;
                currentActionPoints -= 1;
            }

            if (newXLoc < 0)
            {
                newXLoc = 0;
                currentActionPoints += 1;
            }
            else if (newXLoc >= MapScript.GetXTileCount())
            {
                newXLoc = MapScript.GetXTileCount() - 1;
                currentActionPoints += 1;
            }
            else if (newYLoc < 0)
            {
                newYLoc = 0;
                currentActionPoints += 1;
            }
            else if (newYLoc >= MapScript.GetYTileCount())
            {
                newYLoc = MapScript.GetYTileCount() - 1;
                currentActionPoints += 1;
            }
            else
            {
                locationsTargeted.Add(new Vector2(newXLoc, newYLoc));
                bool tileInUse = false;
                for (int i = 0; i < MapScript.GetCharacters().Count; i++)
                {
                    if (locationsTargeted.Contains(MapScript.GetCharacters()[i].GetCurrentLocation()) &&
                        MapScript.GetCharacters()[i] != this)
                    {
                        tileInUse = true;
                    }
                }
                if (!tileInUse)
                {
                    isValidMovement = true;
                }
            }

            
            if(!isValidMovement)
            {
                locationsTargeted.Clear();
                rand = Random.Range(0, 4);
            } 
        }
    }
    #endregion

    #endregion
}
