using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnterLight : MonoBehaviour
{
    public LevelManager levelManager;
    LampSwitch collidingLamp = null;
    bool isFireKeptDown = false;
    Animator childAnimator = null;
    // Start is called before the first frame update
    void Start()
    {
        levelManager = GameObject.FindObjectOfType<LevelManager>();
        childAnimator = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxis("Submit") > 0 && !isFireKeptDown)
        {
            isFireKeptDown = true;
            if (collidingLamp != null) {
                childAnimator.SetTrigger("Interruttore");
                collidingLamp.activate();
            }
        }
        else if (Input.GetAxis("Submit") == 0)
        {
            isFireKeptDown = false;
        }
    }

    private void OnTriggerEnter(Collider other)
    {   
        if (other.tag == "FearTrigger") {
            Lampione lamp = other.GetComponent<Lampione>();
            if (lamp != null){
                levelManager.enterLightArea(!lamp.alreadyUsed);
                lamp.alreadyUsed = true;
            } 
        }
        if (other.tag == "QTETriger") {
            QTE qte = other.GetComponent<QTE>();
            if (qte != null && !qte.completed) {
                levelManager.beginQTE(this, qte);
            }
        }
        if (other.tag == "LampSwitch")
        {
            childAnimator.SetTrigger("Interruttore");
            LampSwitch lampswitch = other.GetComponent<LampSwitch>();
            collidingLamp = lampswitch;
        }

    }

    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "FearTrigger")
        {
            levelManager.exitLightArea();
        }
        if (other.tag == "LampSwitch")
        {
            collidingLamp = null;
        }
    }
}
