using UnityEngine;

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

    [Header("Movement Settings")]
    [Tooltip("The target X position when activated")]
    public float targetX = 5f;
    
    [Tooltip("Speed of platform movement")]
    public float speed = 2f;
    
    [Tooltip("If true, platform stays at target while button is held. If false, platform 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(targetX, transform.position.y, 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 platform path
    void OnDrawGizmosSelected()
    {
        Vector3 start = Application.isPlaying ? startPosition : transform.position;
        Vector3 target = new Vector3(targetX, start.y, start.z);

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

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

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