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

public class PlayerGameplay : MonoBehaviour
{
    public float DefaultPlayerSpeed = 1.0f;
    public float PlayerSpeed;             //Floating point variable to store the player's target movement speed.

    public bool IsDiseased;
    public bool m_IsDead;
    public float DiseasedSpeedModifier = 0.5f;
    public float DiseaseExplosionTime; //time before disease explodes

    public GameObject DiseasedGraphicPrefab; //animation for when you are diseased
    public GameObject CurrentDiseasedGraphic;

    public int m_CurrentScore { get; set; }
    Renderer cachedRenderer;

    private bool RefreshGameplayValues = false;

    private Vector3 m_PreviousPosition = Vector3.zero;

    private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
    // Use this for initialization
    void Start()
    {
        IsDiseased = false;
        PlayerSpeed = DefaultPlayerSpeed;
        cachedRenderer = GetComponent<Renderer>();
        cachedRenderer.material.color = GetComponent<Player>().DefaultColor;
        
    	m_PreviousPosition = transform.position;
        m_IsDead = false;
        rb2d = GetComponent<Rigidbody2D>();
    }

    public bool GetIsDiseased()
    {
        return IsDiseased;
    }

    public void SetPlayerSpeed(float newSpeed)
    {
        PlayerSpeed = newSpeed;
    }

    public float GetPlayerSpeed()
    {
        return PlayerSpeed;
    }

    public void SetIsDiseased(bool setting)
    {
        if(!GetIsDiseased() && setting)
        { 
            if(setting)
            {
                DiseaseExplosionTime = Random.Range(4.0f, 8.0f);
            }

            RefreshGameplayValues = true;
            IsDiseased = setting;
        }
        else if(GetIsDiseased() && !setting)
        {
            RefreshGameplayValues = true;
            IsDiseased = setting;
        }
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.CompareTag("Player"))
        {
            bool isOpponentDiseased = col.gameObject.GetComponent<PlayerGameplay>().GetIsDiseased();
            bool isMyselfDiseased = GetIsDiseased();

            if (isOpponentDiseased && isMyselfDiseased)
            { return; }

            if (isMyselfDiseased && !isOpponentDiseased)
            {
                SetIsDiseased(false);
                col.gameObject.GetComponent<PlayerGameplay>().SetIsDiseased(true);
            }
            if(isOpponentDiseased && !isMyselfDiseased)
            {
                SetIsDiseased(true);
                ApplyDiseaseTransmissonForce(rb2d);
            }
        }
    }

    void ApplyDiseaseTransmissonForce(Rigidbody2D body)
    {
        Vector3 direction = body.transform.position - transform.position;
        body.AddForceAtPosition(direction.normalized, transform.position);
    }
    // Update is called once per frame
    void Update()
    {
        if (RefreshGameplayValues)
        {
            if (IsDiseased)
            {
                PlayerSpeed = PlayerSpeed * DiseasedSpeedModifier;
                CurrentDiseasedGraphic = Instantiate(DiseasedGraphicPrefab);

                CurrentDiseasedGraphic.transform.parent = gameObject.transform;
                //cachedRenderer.material.color = Color.black;
            }
            else
            {
                PlayerSpeed = DefaultPlayerSpeed;
                if (CurrentDiseasedGraphic != null)
                {
                    Object.Destroy(CurrentDiseasedGraphic.gameObject);
                }
                //cachedRenderer.material.color = GetComponent<Player>().DefaultColor;
            }
            RefreshGameplayValues = false;
        }

        UpdateDiseaseTimer();
        UpdateRespawnTimer();
    }

    void UpdateDiseaseTimer()
    {
        if (IsDiseased)
        {
            if (DiseaseExplosionTime > 0)
            {
                DiseaseExplosionTime -= Time.deltaTime;
            }
            else //you exploded
            {
                ApplyDiseaseTransmissonForce(rb2d);
                m_IsDead = true;
                SetIsDiseased(false);
            }
        }
    }

    void UpdateRespawnTimer()
    {

    }
}
