using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

public class Fusebox : MonoBehaviour
{
    [SerializeField] private GameEvent fusePoweredEvent;
    [SerializeField] private GameEvent unpoweredFusePutInEvent;

    private bool initialItemRemoved = false;

    private XRSocketInteractor socket;

    void Awake()
    {
        socket = GetComponentInChildren<XRSocketInteractor>();
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ItemSelected()
    {
        if (!initialItemRemoved)
        {
            return;
        }
        Fuse f = socket.selectTarget.GetComponent<Fuse>();
        if (f)
        {
            if (f.IsPowered)
            {
                // fire powered event
                fusePoweredEvent.Raise();
                socket.selectTarget.gameObject.layer = LayerMask.NameToLayer("FinishedPuzzles");
                socket.gameObject.layer = LayerMask.NameToLayer("FinishedPuzzles");

                Debug.Log("Powered fuse attached");
            }
            else
            {
                unpoweredFusePutInEvent.Raise();
                Debug.Log("Unpowered fuse attached");
            }
        }
        else
        {
            Debug.LogWarning("Non fuse attached to fuse box");
        }
    }

    public void ItemRemoved()
    {
        initialItemRemoved = true;
    }
}
