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

public enum ClothType
{
    GreenCloth = 0,
    RedCloth = 1,
    BlueCloth,
    YellowCloth
}

public class GrabbableCloth : MonoBehaviour
{
    private GameObject m_Player;
    public const float m_MaxTime = Mathf.PI*2;
    public ClothType m_ClothType;

    public float m_VerticalSpeed = 1.5f;
    public float m_HorizontalSpeed = 4;

    public float m_VerticalSpeedRandom = 0.2f;
    public float m_HorizontalSpeedRandom = 0.4f;

    public float m_TimePassed;

    public float m_MinRotValue;
    public float m_MaxRotValue;

    public SpriteRenderer m_Sprite;
    public Collider2D m_Collider;

    public float m_CheckPlayerDistanceInterval = 3;
    public float m_CheckPlayerDistanceTimer;
    public float m_DeleteIfDistanceFromPlayer = 30;
    public bool m_DisableCloth;

    public ParticleSystem m_GrabbedPS;
    public ParticleSystem m_DeathPS;


    void Start()
    {
        if (m_Player == null)
            m_Player = GameObject.FindGameObjectWithTag("Player");
        
        m_VerticalSpeed += Random.Range(-m_VerticalSpeedRandom, m_VerticalSpeedRandom); 
        m_HorizontalSpeed += Random.Range(-m_HorizontalSpeedRandom, m_HorizontalSpeedRandom);
        m_MinRotValue = Random.Range(0f, 7.5f);
        m_MaxRotValue = Random.Range(50f, 65f);

        m_ClothType = UnityEngine.Object.FindObjectOfType<GameController>().ReturnRandomPresentColor();
        
        SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer>();
        if (renderer) renderer.color = ColourManager.instance.GetClothColorFromEnum(m_ClothType);
    }
    
    void Update()
    {
        if (m_DisableCloth)
            return;
        if (m_Player != null)
        {
            m_CheckPlayerDistanceTimer += Time.deltaTime;
            if (m_CheckPlayerDistanceTimer > m_CheckPlayerDistanceInterval)
            {

                m_CheckPlayerDistanceTimer = 0;
                if (Vector2.SqrMagnitude(m_Player.transform.position - transform.position) > m_DeleteIfDistanceFromPlayer* m_DeleteIfDistanceFromPlayer)
                    DestroyCloth();
                
            }
        }
    }

    private void FixedUpdate()
    {
        if (m_DisableCloth)
            return;
        m_TimePassed += Time.fixedDeltaTime;
        if (m_TimePassed > 6.28319f)
            m_TimePassed = 0;


        Vector3 newPos = new Vector3(transform.position.x + GetHorizontalSpeed(), transform.position.y + GetVerticalSpeed(), 0);
        transform.position = newPos;
    }

    public float GetVerticalSpeed()
    {
        float newSpeed = 0;
        float sinValue = Mathf.Abs(Mathf.Sin(m_TimePassed));
        newSpeed = -sinValue * m_VerticalSpeed * Time.fixedDeltaTime;

        if (sinValue > 0.99f)
        {
            m_MinRotValue = Random.Range(0, 7.5f);
        }

        if (sinValue - Mathf.Epsilon < 0 + Mathf.Epsilon)
        {
            m_MaxRotValue = Random.Range(50, 65);
        }

        float rotValue = Mathf.Lerp(m_MinRotValue, m_MaxRotValue, sinValue);

        
        transform.rotation = Quaternion.Euler(rotValue, 0, 0);
        return newSpeed;
    }

    public float GetHorizontalSpeed()
    {

        float newSpeed = 0;
        newSpeed = Mathf.Cos(m_TimePassed) * m_HorizontalSpeed * Time.fixedDeltaTime;

        return newSpeed;
    }

    public void Grabbed()
    {
        m_GrabbedPS.gameObject.SetActive(true);
        m_GrabbedPS.Emit(30);
    }

    [ContextMenu("dest")]
    public void DestroyCloth()
    {
        StartCoroutine(DestroyingCloth());
    }

    IEnumerator DestroyingCloth()
    {
        float timer = 0;

        m_DeathPS.gameObject.SetActive(true);
        while (timer < 1)
        {
            float alphaValue = Mathf.Lerp(1, 0, timer);
            timer += Time.deltaTime;
            Color color = m_Sprite.color;
            color.a = alphaValue;
            m_Sprite.color = color;
            yield return null;
        }

        Destroy(gameObject);
    }

}
