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

public class CharacterScript : MonoBehaviour {

    [SerializeField] protected string characterName = "NAME";

    [SerializeField] protected int currentActionPoints = 3;
    [SerializeField] protected int lastActionPoints = 3; //used to kep track of points between phases (move to attack)
    protected const int maxActionPoints = 3;
    [SerializeField] protected bool isInfected = false;
    [SerializeField] protected int currentHealth = 10;
    protected const int maxHealth = 10;
    protected const float critalhealthPercent = 0.25f;
    protected const float weakhealthPercent = 0.5f;

    [SerializeField] protected int damage = 5;
    [SerializeField] protected int currentDefendPoints = 0;
    [SerializeField] protected int currentDodgePoints = 0;

    [SerializeField] protected int currentXLoc = 0;
    [SerializeField] protected int currentYLoc = 0;

    [SerializeField] protected int targetXLoc = 0;
    [SerializeField] protected int targetYLoc = 0;

    [SerializeField] protected GameObject targeter;

    [SerializeField] public List<Vector2> locationsTargeted = new List<Vector2>();

    [SerializeField] protected Vector2 directionFacing = Vector2.right;

    [SerializeField] protected Text healthText;

    public enum Action { Move, Attack, Defend, Dodge, Infect, PickAction, Wait };
    [SerializeField]protected Action currentAction = Action.Wait;

    public enum Affiliation { Friendly, Neutral, Enemy };
    [SerializeField]protected Affiliation currentAffiliation = Affiliation.Neutral;

    // Use this for initialization
    virtual protected void Start () {
		
	}

    // Update is called once per frame
    virtual protected void Update () {
		
	}

    #region Handlers
    virtual public void HandleActions()
    {
        if (currentAction == Action.Wait)
        {
            //ResetActionPoints();
        }
        else if (currentAction == Action.Move)
        {
            MovePlayer();
        }
        else if (currentAction == Action.Attack)
        {
            Attack();
        }
        else if (currentAction == Action.Defend)
        {
            Defend();
        }
    }
    #endregion

    #region Setters
    virtual public void SetHealthText()
    {
        healthText.text = "HP " + currentHealth.ToString();
    }

    virtual public void SetIsInfected()
    {
        isInfected = true;
    }
    virtual public  void ResetActionPoints()
    {
        currentActionPoints = maxActionPoints;
        lastActionPoints = maxActionPoints;
    }

    virtual public void SetActionPointsToLast()
    {
        currentActionPoints = lastActionPoints;
    }

    virtual public void SetCurrentLocation(Vector2 loc)
    {
        currentXLoc = (int)loc.x;
        currentYLoc = (int)loc.y;
    }

    virtual public void StartGame()
    {
        ResetActionPoints();
        currentAction = Action.Wait;
        currentDefendPoints = 0;
        currentDodgePoints = 0;
        targetXLoc = currentXLoc;
        targetYLoc = currentYLoc;
    }

    virtual public void StartTurn()
    {
        ResetActionPoints();
        currentAction = Action.Move;
        currentDefendPoints = 0;
        currentDodgePoints = 0;
        targetXLoc = currentXLoc;
        targetYLoc = currentYLoc;
    }

    virtual public void NextAction()
    {
        //Debug.Log(characterName + "Last Action: " + currentAction);
        if (currentAction == Action.Wait)
        {
            //ResetActionPoints();
            currentAction = Action.Move;
        }
        else if (currentAction == Action.Move)
        {
            currentAction = Action.Attack;
        }
        else if (currentAction == Action.Attack)
        {
            currentAction = Action.Wait;
        }
        else if (currentAction == Action.Defend)
        {
            currentAction = Action.Infect;
        }
        else if (currentAction == Action.Dodge)
        {
            currentAction = Action.Infect;
        }
        else if (currentAction == Action.Infect)
        {
            currentAction = Action.Wait;
        }
        //Debug.Log(characterName + " Next Action: " + currentAction);
    }


    virtual public void SetCurrentAction(Action newAct)
    {
        currentAction = newAct;
    }

    #endregion

    #region Health
    public void TakeDamage(int dam)
    {
        dam -= currentDefendPoints;
        float rand = Random.Range(0, 100);
        Debug.Log("R% " + rand.ToString());
        if (dam < 0)
        {
            dam = 0;
        }
        else if (currentDodgePoints == 1 && rand <= 25)
        {
            dam = 0;
            Debug.Log("L1 Dodge");
        }
        else if (currentDodgePoints == 2 && rand <= 33)
        {
            dam = 0;
            Debug.Log("L2 Dodge");
        }
        else if (currentDodgePoints == 3 && rand <= 50)
        {
            dam = 0;
            Debug.Log("L3 Dodge");
        }
        currentHealth -= dam;
    }

    public float GetHealthPercentage()
    {
        return (float)currentHealth / (float)maxHealth;
    }
    #endregion

    #region Getters
    virtual public string GetCharacterName()
    {
        return characterName;
    }
    virtual public Action GetCurrentAction()
    {
        return currentAction;
    }

    virtual public Affiliation GetCurrentAffiliation()
    {
        return currentAffiliation;
    }

    virtual public GameObject GetTargeterObject()
    {
        return targeter;
    }

    virtual public int GetCurrentActionPoints()
    {
        return currentActionPoints;
    }

    virtual public int GetMaxActionPoints()
    {
        return maxActionPoints;
    }

    virtual public int GetCurrentHealth()
    {
        return currentHealth;
    }

    virtual public int GetMaxHealth()
    {
        return maxHealth;
    }

    virtual public int GetDefendPoints()
    {
        return currentDefendPoints;
    }

    virtual public int GetDodgePoints()
    {
        return currentDodgePoints;
    }
    virtual public bool GetIsInfected()
    {
        return isInfected;
    }

    virtual public Vector2 GetCurrentLocation()
    {
        return new Vector2(currentXLoc, currentYLoc);
    }

    virtual public Vector2 GetTargetLocation()
    {
        return new Vector2(targetXLoc, targetYLoc);
    }

    virtual public List<Vector2> GetLocationsTargeted()
    {
        return locationsTargeted;
    }
    #endregion


    #region Phase Functions

    #region Infect
    virtual protected void Infect()
    {
       
    }

    virtual protected void InfectTargeter()
    {
       
    }
    #endregion

    #region Dodge
    virtual protected void Dodge()
    {

    }

    virtual protected void ChargeDodgePoints()
    {

    }
    #endregion

    #region Defend
    virtual protected void Defend()
    {
        
    }

    virtual protected void ChargeDefencePoints()
    {
       
    }
    #endregion

    #region Attack
    virtual protected void Attack()
    {
        
    }

    virtual protected void AttackTargeter()
    {
        
    }
    #endregion

    #region Movement
    virtual protected void MovePlayer()
    {

    }

    virtual protected void FindMovementTargetSquare()
    {
       
    }
    #endregion

    #endregion
}
