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

public class EnemyControllerScript : CharacterScript {

    List<CharacterScript> friendlyTargets = new List<CharacterScript>();//targets the enemy is attacking/focusing
    [SerializeField] public Action[] priorityAction = new Action[4];
    [SerializeField] public Action actionToTake = Action.Wait;
    private bool inCorutine = false;
    private int tempDamage = 0;
    protected override void Update()
    {
        SetHealthText();
        HandlePriority();
    }

    #region Handlers
    void ActionPicker()
    {
        actionToTake = Action.Attack;
        //int rand = Random.Range(0, 100);
        //if(rand <= 10)
        //{
        //    actionToTake = priorityAction[3];
        //}
        //else if (rand > 10 && rand <= 25)
        //{
        //    actionToTake = priorityAction[2];
        //}
        //else if (rand > 25 && rand <= 50)
        //{
        //    actionToTake = priorityAction[1];
        //}
        //else if (rand <= 100)
        //{
        //    actionToTake = priorityAction[0];
        //}

    }

    int GetPriorityActionIndexValue(Action act)
    {
        for(int i = 0; i < priorityAction.Length; i++)
        {
            if(priorityAction[i] == act)
            {
                return i;
            }
        }
        Debug.Log("INDEX VALUE NOT FOUND");
        return 0;
    }

    void HandlePriority()
    {
        if(GetHealthPercentage() > critalhealthPercent)
        {
            priorityAction[0] = Action.Dodge;
            priorityAction[1] = Action.Move;
            priorityAction[2] = Action.Defend;
            priorityAction[3] = Action.Attack;
        }
        else if (GetHealthPercentage() > weakhealthPercent)
        {
            priorityAction[0] = Action.Move;
            priorityAction[1] = Action.Defend;
            priorityAction[2] = Action.Attack;
            priorityAction[3] = Action.Dodge;
        }
        else
        {
            priorityAction[0] = Action.Attack;
            priorityAction[1] = Action.Move;
            priorityAction[2] = Action.Defend;
            priorityAction[3] = Action.Dodge;
        }
    }

    IEnumerator GoToNextActionAndWait(Action actionWaitingOn)
    {
        inCorutine = true;
        yield return new WaitForSeconds(2.5f);
        locationsTargeted.Clear();
        if(actionWaitingOn == Action.Move)
        {
            currentXLoc = targetXLoc;
            currentYLoc = targetYLoc;
        }
        else if (actionWaitingOn == Action.Attack)
        {
            //clear targeter
            targetXLoc = currentXLoc;
            targetYLoc = currentYLoc;

            //deal damage
            MapScript.GetPlayer().TakeDamage(tempDamage);
        }
        else if (actionWaitingOn == Action.Defend)
        {

        }
        else if (actionWaitingOn == Action.Dodge)
        {

        }
        else if (actionWaitingOn == Action.Wait)
        {

        }

        NextAction();
        inCorutine = false;
    }

    override public void StartTurn()
    {
        ResetActionPoints();
        ActionPicker(); //pick action to take
        currentAction = Action.Move;
        currentDefendPoints = 0;
        currentDodgePoints = 0;
        targetXLoc = currentXLoc;
        targetYLoc = currentYLoc;
    }
    override public void HandleActions()
    {
        if (currentAction == Action.Wait)
        {

        }
        else if (currentAction == Action.Move)
        {
            MovePlayer();
        }
        else if (currentAction == Action.Attack)
        {
            Attack();
        }
        else if (currentAction == Action.Defend)
        {
            Defend();
        }
        else if (currentAction == Action.Dodge)
        {
            Dodge();
        }
    }

    override public void NextAction()
    {
        //Debug.Log(characterName + "Last Action: " + currentAction);
        lastActionPoints = currentActionPoints;
        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.Dodge;
        }
        else if (currentAction == Action.Dodge)
        {
            currentAction = Action.Wait;
        }
    }
    #endregion

    #region Phase Functions

    #region Infect
    override protected void Infect()
    {

    }

    override protected void InfectTargeter()
    {

    }
    #endregion

    #region Dodge
    override protected void Dodge()
    {
        if (!inCorutine)
        {
            ChargeDodgePoints();
            StartCoroutine(GoToNextActionAndWait(Action.Dodge));
            lastActionPoints = currentActionPoints;
        }
    }

    override protected void ChargeDodgePoints()
    {
        //if dodge takes priority dodge otherwise do nothing
        if (GetPriorityActionIndexValue(Action.Dodge) < GetPriorityActionIndexValue(Action.Defend))
        {
            currentDodgePoints = currentActionPoints;
            currentActionPoints = 0;
            SoundPlayerScript.PlayValChangeSound();
        }
    }
    #endregion

    #region Defend
    override protected void Defend()
    {
        if (!inCorutine)
        {
            ChargeDefencePoints();
            StartCoroutine(GoToNextActionAndWait(Action.Defend));
            lastActionPoints = currentActionPoints;
        }
    }

    override protected void ChargeDefencePoints()
    {
        //if Defend takes priority Defend otherwise do nothing
        if (GetPriorityActionIndexValue(Action.Defend) < GetPriorityActionIndexValue(Action.Dodge))
        {
            currentDefendPoints = currentActionPoints;
            currentActionPoints = 0;
            SoundPlayerScript.PlayValChangeSound();
        }
    }
    #endregion

    #region Attack
    override protected void Attack()
    {

        if (!inCorutine)
        {
            if (currentActionPoints > 0)
            {
                AttackTargeter();
            }
            lastActionPoints = currentActionPoints;
            StartCoroutine(GoToNextActionAndWait(Action.Attack));
        }
    }

    override protected void AttackTargeter()
    {
        int pXLoc = (int)MapScript.GetPlayer().GetCurrentLocation().x;
        int pYLoc = (int)MapScript.GetPlayer().GetCurrentLocation().y;
        if (actionToTake == Action.Attack)
        {
            if (pYLoc == currentYLoc && currentXLoc > pXLoc && Mathf.Abs(pXLoc - currentXLoc) <= currentActionPoints)
            {
                for(int i = 0; (i + pXLoc) <= currentXLoc; i++)
                {
                    locationsTargeted.Add(new Vector2(pXLoc + i, currentYLoc));
                }
                if(Mathf.Abs(currentXLoc - pXLoc) <= currentActionPoints && currentActionPoints > 0)
                {
                    //locationsTargeted.Add();
                    tempDamage = damage - (Mathf.Abs(currentXLoc - pXLoc) - 1);
                    SoundPlayerScript.PlayAttackSound();
                }
                else
                {
                    tempDamage = 0;
                    Debug.Log("Can't Attack 0 AP");
                }
                currentActionPoints -= Mathf.Abs(pXLoc - currentXLoc);
            }
            else
            {
                tempDamage = 0;
            }
        }
        else
        {
            tempDamage = 0;
        }
    }
    #endregion

    #region Movement
    override protected void MovePlayer()
    {
        if (!inCorutine)
        {
            FindMovementTargetSquare();
            StartCoroutine(GoToNextActionAndWait(Action.Move));
            lastActionPoints = currentActionPoints;
        }
    }

    override protected void FindMovementTargetSquare()
    {
        int pXLoc = (int)MapScript.GetPlayer().GetCurrentLocation().x;
        int pYLoc = (int)MapScript.GetPlayer().GetCurrentLocation().y;
        int newXLoc = targetXLoc = currentXLoc;
        int newYLoc = targetYLoc = currentYLoc;
        
        if (actionToTake == Action.Attack)
        {
            //store distances
            int xMoveDist = Mathf.Abs(newXLoc - currentXLoc);
            int yMoveDist = Mathf.Abs(newYLoc - currentYLoc);

            //y movement
            if (pYLoc != currentYLoc)
            {
                newYLoc = pYLoc;
                yMoveDist = Mathf.Abs(newYLoc - currentYLoc);

                int posNegInt = (newYLoc - currentYLoc);
                if (posNegInt > 0)
                {
                    posNegInt = 1;
                }
                else
                {
                    posNegInt = -1;
                }
                for (int i = 0; i <= yMoveDist; i++)
                {
                    locationsTargeted.Add(new Vector2(currentXLoc, currentYLoc + (i * posNegInt)));
                }
                   
            }
            //x movement
            else if (yMoveDist < maxActionPoints)
            {
                if (pXLoc != currentXLoc + 1)
                {
                    if (pXLoc <= 1)
                    {
                        newXLoc = 2;
                        xMoveDist = Mathf.Abs(newXLoc - currentXLoc);
                    }
                    else if (pXLoc == 2)
                    {
                        newXLoc = 3;
                        xMoveDist = Mathf.Abs(newXLoc - currentXLoc);
                    }

                    int posNegInt = (newXLoc - currentXLoc);
                    if (posNegInt > 0)
                    {
                        posNegInt = 1;
                    }
                    else
                    {
                        posNegInt = -1;
                    }
                    for (int i = 0; i <= xMoveDist; i++)
                    {
                        locationsTargeted.Add(new Vector2(currentXLoc + (i * posNegInt), currentYLoc));
                    }
                }

            }
            int totalDist = xMoveDist + yMoveDist;
            if (totalDist >= maxActionPoints)
            {
                //Debug.Log("Dist Over AP: " + totalDist);
                currentActionPoints = 0;
            }
            else
            {
                //Debug.Log("Dist under AP: " + totalDist + " ,AP: " + currentActionPoints);
                currentActionPoints -= totalDist;
            }

            targetXLoc = newXLoc;
            targetYLoc = newYLoc;
            if(targetXLoc != newXLoc || targetYLoc != newYLoc)
            {
                SoundPlayerScript.PlayMoveSound();
            }
        }
        //else if (actionToTake == Action.Move)
        //{

        //}
    }
    #endregion

    #endregion

}
