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

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

        public bool Interactable = true;
        private bool isOpened = false;

        public string GetInteractPrompt()
        {
            return "按F进行交互";
        }

        public void Interact(GameObject interactor)
        {
            if (!Interactable) return;
            if (isOpened)
            {
                CloseDoor();
                isOpened = false;
            }
            else
            {
                OpenDoor();
                isOpened = true;
            }
        }

        public void OnGazeEnter(GameObject editor)
        {
            //
        }

        public void OnGazeExit(GameObject editor)
        {
            //
        }

        private void OpenDoor()
        {
            // 逆时针旋转90度
            transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + new Vector3(0, -90, 0));
            Debug.Log("Open door");
        }

        private void CloseDoor()
        {
            // 顺时针旋转90度
            transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + new Vector3(0, 90, 0));
            Debug.Log("Close door");
        }

        #endregion

        #region EditableComponent

        [SerializeField] private bool isActive = true;

        public bool IsActive
        {
            get => isActive;
            set
            {
                isActive = value;
                //具体被编辑的逻辑
                Interactable = isActive;
            }
        }

        public string ComponentName { get; set; } = "DoorSwitch";
        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)
        {
            Interact(sender);
        }

        #endregion
    }
}