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);

    // 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.leftButton.wasPressedThisFrame)
        {
            Vector2 mousePosition = Mouse.current.position.ReadValue();
            Vector2 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

            if (boxCollider.OverlapPoint(worldMousePosition))
            {
                isButtonPressed = true;
                key.TranslateTo(row);
            } else {
                isButtonPressed = false;
            }
        }

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