using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Script.Gameplay.Interface;
using Script.Gameplay.Connect;

namespace Script.Gameplay.Facility
{
    public class ButtonInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable, ISignalSender
    {
        #region Interactable

        public bool Interactable = true;
        [SerializeField] private float signalDuration = 1.0f; // 信号持续时间（秒）

        public string GetInteractPrompt()
        {
            return "按F按下按钮";
        }

        public void Interact(GameObject interactor)
        {
            if (!Interactable) return;
            StartCoroutine(SendSignalCoroutine());
            Debug.Log("Button pressed");
        }

        public void OnGazeEnter(GameObject editor)
        {
            // 可选：高亮按钮等
        }

        public void OnGazeExit(GameObject editor)
        {
            // 可选：取消高亮
        }

        private IEnumerator SendSignalCoroutine()
        {
            SendSignal(true);
            Interactable = false;
            // 按钮压下的动画或效果可以在这里添加
            
            yield return new WaitForSeconds(signalDuration);
            SendSignal(false);
            Interactable = true;
            // 按钮弹起的动画或效果可以在这里添加
        }

        #endregion

        #region EditableComponent

        [SerializeField] private bool isActive = true;

        public bool IsActive
        {
            get => isActive;
            set
            {
                isActive = value;
                Interactable = isActive;
            }
        }

        public string ComponentName { get; set; } = "Button";
        public LockLevel LockLevel => LockLevel.Red;

        #endregion

        #region Connectable

        public void OnGazeEnter()
        {
        }

        public void OnGazeExit()
        {
        }

        public Vector3 GetPosition()
        {
            return transform.position;
        }

        public GameObject GetGameObject()
        {
            return gameObject;
        }

        public string GetConnectableName()
        {
            return gameObject.name;
        }

        public List<ConnectionLine> ConnectionLines { get; set; } = new List<ConnectionLine>();
        public void SignalActive(bool active, GameObject sender)
        {
            // 按钮通常不接收信号，因此这里可以留空
        }
        
        public void SendSignal(bool active)
        {
            if (ConnectionLines != null)
            {
                foreach (var line in ConnectionLines)
                {
                    line.SignalActive(active, this.gameObject);
                }
            }
        }

        #endregion
    }
}

