using UnityEngine;
using UnityEngine.InputSystem;

public class MenuButton : MonoBehaviour
{
    public bool isButtonPressed = false;

    public MenuKey key;

    public int row;

    private SpriteRenderer spriteRenderer;

    private Collider2D boxCollider;

    private static Color transparent = new Color(1f, 1f, 1f, 0f);

    private static Color opaque = new Color(1f, 1f, 1f, 1f);

    private const float holdThreshold = 0.15f;

    private bool pointerDownOnButton = false;

    private bool holdInteraction = false;

    private bool pointerInsideDuringHold = false;

    private float pointerDownTime = 0f;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        boxCollider = GetComponent<Collider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Mouse.current == null)
        {
            return;
        }

        // Check for mouse button press to request key translation and handle hold interactions
        if (Mouse.current.leftButton.wasPressedThisFrame)
        {
            if (IsPointerInsideButton())
            {
                isButtonPressed = true;
                key.TranslateTo(row);
                pointerDownOnButton = true;
                holdInteraction = false;
                pointerInsideDuringHold = true;
                pointerDownTime = Time.unscaledTime;
            }
            else
            {
                isButtonPressed = false;
                pointerDownOnButton = false;
                holdInteraction = false;
            }
        }

        if (pointerDownOnButton && Mouse.current.leftButton.isPressed)
        {
            bool pointerInside = IsPointerInsideButton();

            if (!holdInteraction && (Time.unscaledTime - pointerDownTime) >= holdThreshold)
            {
                holdInteraction = true;
                key.OpenKey(false);
                pointerInsideDuringHold = pointerInside;

                if (!pointerInside)
                {
                    key.CloseKey();
                }
            }

            if (holdInteraction)
            {
                if (pointerInside && !pointerInsideDuringHold)
                {
                    key.OpenKey(false);
                }
                else if (!pointerInside && pointerInsideDuringHold)
                {
                    key.CloseKey();
                }

                pointerInsideDuringHold = pointerInside;
            }
        }

        if (pointerDownOnButton && Mouse.current.leftButton.wasReleasedThisFrame)
        {
            bool pointerInside = IsPointerInsideButton();

            if (holdInteraction)
            {
                if (pointerInside)
                {
                    key.InvokeCallback();
                }

                key.ReleaseKey();
            }
            else
            {
                key.OpenKey(true);
            }

            pointerDownOnButton = false;
            holdInteraction = false;
            pointerInsideDuringHold = false;
        }

        if (isButtonPressed)
        {
            spriteRenderer.color = opaque;
        }
        else
        {
            spriteRenderer.color = transparent;
        }
    }

    private bool IsPointerInsideButton()
    {
        if (Camera.main == null)
        {
            return false;
        }

        Vector2 mousePosition = Mouse.current.position.ReadValue();
        Vector2 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
        return boxCollider.OverlapPoint(worldMousePosition);
    }
}
