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

//PlayerControls related code

public class Player : MonoBehaviour
{
	public InputDevice Device { get; set; }

    private Animator animator;
    private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.

    public PersistantPlayerInfo m_PersistentInfo;
    public Color DefaultColor { get; set; }
    public int m_PlayerIndex { get; set; }
    public string m_PlayerName { get; set; }
    public bool IsMoving;

    public int PlayerDirection;
    public Ability m_Ability;

    public float Speed = 5.0f;

    // Status effect, hacked
    public float DashMultiplier = 2.0f;
    public bool Stunned = false;
    public bool Confused = false;
    public bool Immune = false;

    //Sprites
    public Sprite StatusStunned;
    public Sprite StatusConfused;
    public Sprite StatusImmune;

    public Sprite AbilityStun;
    public Sprite AbilityConfuse;

    public GameObject StatusIconObj;
    public GameObject AbilityIconObj;

    public List<Status> m_AffectingStatus = new List<Status>();

    void Start()
	{
        rb2d = GetComponent<Rigidbody2D>();
        animator = this.GetComponent<Animator>();

        CreateAbility("Stun");
        m_AffectingStatus.Clear();

        //First, Find the Parent Object 
        Transform parent = transform.parent;
        //StatusIconObj = parent.Find("StatusIcon").gameObject;
        //AbilityIconObj = parent.Find("AbilityEffect").gameObject;
    }

    void CreateAbility(string ability1)
    {
        switch(ability1)
        {
            case "Stun":
                m_Ability = this.gameObject.AddComponent<StunAbility>();
                break;

            case "Confuse":
                m_Ability = this.gameObject.AddComponent<ConfuseAbility>();
                break;

            case "Immunity":
                m_Ability = this.gameObject.AddComponent<ImmunityAbility>();
                break;

            case "Dash":
                m_Ability = this.gameObject.AddComponent<DashAbility>();
                break;
        }
        
    }

    void Update()
    {
        if (Device.Command.WasPressed)
        {
            GameManager.Instance.TogglePause();
        }

        //Quick hack for dashing
        if (Device.Action1.WasPressed)
        {
            // Hit Ability
            if (m_Ability)
                m_Ability.Use(this);

            rb2d.AddRelativeForce(Vector3.forward * 5.0f, ForceMode2D.Impulse);
        }

    }

    void ResetStatusEffects()
    {
        DashMultiplier = 1.0f;
        Stunned = false;
        Confused = false;
        Immune = false;
    }

    void UpdateStatusEffects()
    {
        for (int i = m_AffectingStatus.Count-1; i >= 0 ; --i)
        {
            Status current = m_AffectingStatus[i];

            if (current.TimeLeft <= 0)
            {
                m_AffectingStatus.Remove(current);
                StatusIconObj.GetComponent<SpriteRenderer>().sprite = null;
            }
            else
            {
                current.Update(this);
            }

        }
    }

    void FixedUpdate()
    {
        ResetStatusEffects();
        UpdateStatusEffects();

        if (Device == null || gameObject.GetComponent<PlayerGameplay>().m_IsDead)
        {
            animator.SetFloat("Speed", 0.0f);
        }
        else
        {
            if(Stunned)
            { 
                StatusIconObj.GetComponent<SpriteRenderer>().sprite = StatusStunned;
            }
            else if (Confused)
            {
                StatusIconObj.GetComponent<SpriteRenderer>().sprite = StatusConfused;
            }
            else if (Immune)
            {
                StatusIconObj.GetComponent<SpriteRenderer>().sprite = StatusImmune;
            }
            else
            {
                StatusIconObj.GetComponent<SpriteRenderer>().sprite = null;
            }
            if (Stunned)
                return;

            //float speed = GetComponent<PlayerGameplay>().GetPlayerSpeed();
            float speed = Speed * DashMultiplier;
            // Move the player based on the D pad
            float H = Device.Direction.X * speed * Time.deltaTime;
            float V = Device.Direction.Y * speed * Time.deltaTime;
            Vector2 movement = new Vector2(H , V);

            if (Confused)
                movement = -movement;

            // rb2d.AddForce(movement, ForceMode2D.Impulse);
            rb2d.MovePosition(rb2d.position + movement);
            //transform.Rotate( Vector3.down, 500.0f * Time.deltaTime * Device.Direction.X, Space.World );
            //transform.Rotate( Vector3.right, 500.0f * Time.deltaTime * Device.Direction.Y, Space.World );
            

            PlayerDirection = (int)(Mathf.Floor(Mathf.Atan2(movement.y, movement.x)*180.0f/Mathf.PI +720.0f +22.5f) % 360 /360 * 8);
			animator.SetInteger("Direction", PlayerDirection);

			if(Mathf.Abs(movement.x) +Mathf.Abs(movement.y) >0.0f)
			{
				animator.SetFloat("Speed", 1.0f);
				IsMoving = true;
			}
			else
			{
				animator.SetFloat("Speed", 0.0f);
				IsMoving = false;
			}
        }
   } 
}

