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

public class ComputerScript : MonoBehaviour {

    public enum ComputerColor
    {
        orange, green, blue
    };
    public ComputerColor computerColor;
    [SerializeField]private GameObject doorToOpen;
    [SerializeField] private GameObject keyPrompt;
    private bool colliding = false;
    Animator anim;
    ScientistScript colScript;

    private void Start()
    {
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        if (colliding)
        { 
        keyPrompt.SetActive(true);

        if ((int)colScript.scientistColor == (int)computerColor)
        {
            keyPrompt.GetComponent<Animator>().SetBool("Enabled", true);
            if (Input.GetKeyDown(KeyCode.E))
            {
                GetComponent<AudioSource>().Play();
                doorToOpen.GetComponent<DoorScript>().OpenDoor();
                anim.SetBool("Enabled", false);
                keyPrompt.SetActive(false);
            }
        }
        else
        {
            keyPrompt.GetComponent<Animator>().SetBool("Enabled", false);
        }
        }
    }

    private void OnTriggerStay2D(Collider2D collision)
    {

        colScript = collision.gameObject.GetComponent<ScientistScript>();
        if (collision.gameObject.CompareTag("UnPossessed"))
        {
            return;
        }
        if (collision.isTrigger)
        {
            return;
        }
        if(colScript == null)
        {
            return;
        }
        if(anim.GetBool("Enabled") == false)
        {
            return;
        }

        colliding = true;

        
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        keyPrompt.SetActive(false);
        colliding = false;
    }
}
