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

// Delegate function prototype for when the collider is colliding with the patch
public delegate void OnPatchEvent(GameObject patchId);

// Delegate function prototype for when the collider exit the patch
public delegate void OnPatchExitEvent();

// Delegate function prototype for when the collider triggers the patch
public delegate void OnPatchTrigger(GameObject obj);

// Delegate function prototype for when the patch is gg
public delegate void OnPatchGgEvent(GameObject obj);

[RequireComponent(typeof(BoxCollider2D))]
public class PatchBehaviour : MonoBehaviour
{
    // The event handler for the on patch function
    public static event OnPatchEvent onPatchHandler;

    // The event handler for the on patch exit function
    public static event OnPatchExitEvent onPatchExitHandler;

    // The event handler for on patch triggered
    public static event OnPatchTrigger onPatchTriggered;

    // The event handler for when patch is gged
    public static event OnPatchGgEvent onPatchGgHandler;

    // The lifespan of the cloth
    public float lifespan;

    // The elapsed time of the cloth
    private float elapsed;
    
    private void Awake()
    {
        elapsed = 0;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag != "Player") return;
        if (onPatchTriggered != null)
        {
            onPatchTriggered(gameObject);
        }
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.gameObject.tag != "Player") return;
        if (onPatchHandler != null)
        {
            onPatchHandler(gameObject);
        }

        // Get the mouse controller
        MouseController mouseController = collision.gameObject.GetComponent<MouseController>();

        if (mouseController != null)
        {
            if (mouseController.IsHanging)
            {
                // Increase the timer
                elapsed += Time.deltaTime;

                // Handles the animation for when the player is hanging onto the cloth
                if (elapsed < lifespan)
                {
                    SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
                    spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, 1.0f - (elapsed / lifespan - 0.1f));
                }

                // Check for gg
                if (elapsed >= lifespan)
                {
                    if (onPatchGgHandler != null)
                    {
                        onPatchGgHandler(gameObject);
                    }

                    gameObject.SetActive(false);
                }
            }
            else
            {
                SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
                spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, 255.0f);
            }
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag != "Player") return;
        if (onPatchExitHandler != null)
        {
            onPatchExitHandler();
        }
        elapsed = 0;
    }
    
    public static void Cleanup()
    {
        onPatchHandler     = null;
        onPatchExitHandler = null;
        onPatchTriggered   = null;
    }
}
