using UnityEngine;

/// <summary>
/// Elevator Controller - Moves between two Y positions when buttons are pressed.
/// Supports up to two buttons (either one can activate the elevator).
/// </summary>
public class ElevatorController : MonoBehaviour
{
    [Header("Button References")]
    [Tooltip("First button that can activate the elevator")]
    public ButtonController button1;
    
    [Tooltip("Second button that can activate the elevator (optional)")]
    public ButtonController button2;

    [Header("Movement Settings")]
    [Tooltip("The target Y position when activated")]
    public float targetY = 5f;
    
    [Tooltip("Speed of elevator movement")]
    public float speed = 2f;
    
    [Tooltip("If true, elevator stays at target while button is held. If false, elevator toggles on button press.")]
    public bool holdToActivate = true;

    private Vector3 startPosition;
    private Vector3 targetPosition;
    private bool isMovingToTarget = false;

    void Start()
    {
        // Store the starting position
        startPosition = transform.position;
        targetPosition = new Vector3(transform.position.x, targetY, transform.position.z);
    }

    void Update()
    {
        // Check if any button is active
        bool anyButtonActive = IsAnyButtonActive();

        if (holdToActivate)
        {
            // Hold mode: move to target while button is pressed, return when released
            isMovingToTarget = anyButtonActive;
        }
        else
        {
            // Toggle mode would go here if needed
            isMovingToTarget = anyButtonActive;
        }

        // Determine destination
        Vector3 destination = isMovingToTarget ? targetPosition : startPosition;

        // Smoothly move towards destination
        if (Vector3.Distance(transform.position, destination) > 0.01f)
        {
            transform.position = Vector3.MoveTowards(
                transform.position,
                destination,
                speed * Time.deltaTime
            );
        }
        else
        {
            // Snap to destination when close enough
            transform.position = destination;
        }
    }

    private bool IsAnyButtonActive()
    {
        bool button1Active = button1 != null && button1.isActive();
        bool button2Active = button2 != null && button2.isActive();
        
        return button1Active || button2Active;
    }

    // Visual helper in editor to show elevator path
    void OnDrawGizmosSelected()
    {
        Vector3 start = Application.isPlaying ? startPosition : transform.position;
        Vector3 target = new Vector3(start.x, targetY, start.z);

        // Draw start position
        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(start, new Vector3(1f, 0.2f, 0.1f));

        // Draw target position
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(target, new Vector3(1f, 0.2f, 0.1f));

        // Draw line between positions
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(start, target);
    }
}
