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

namespace Prisma.Main
{
    public class Elevator2 : MonoBehaviour
    {
        [SerializeField]
        private GameObject[] door;
        [SerializeField]
        private GameObject mng; 

        void Start()
        {
            GameMngr.onNextLvl += OnNextLvl;
        }

        private void OnNextLvl()
        {
        }

        private void OnTriggerEnter2D(Collider2D col)
        {
            if (col.gameObject.name == "Thief" && mng.GetComponent<GameMngr>().RLevel() == 2)
                StartCoroutine(NextLevel(0));
            else if (col.gameObject.name == "Thief" && mng.GetComponent<GameMngr>().RLevel() == 3 && mng.GetComponent<Pad>().RRoof())
                StartCoroutine(NextLevel(1));
        }

        private IEnumerator NextLevel(int i)
        {
            if (i == 0)
            {
                door[i].GetComponent<Animator>().SetTrigger("Close2");
                door[i+1].GetComponent<Animator>().SetTrigger("Open3");
            }
            if (i == 1)
                door[i].GetComponent<Animator>().SetTrigger("Close3");
            door[i].GetComponent<AudioSource>().Play();
            yield return new WaitForSeconds(0.5f);
            this.GetComponent<AudioSource>().Play();
            if (i == 0)
                this.GetComponent<Animator>().SetTrigger("Up1");
            if (i == 1)
                this.GetComponent<Animator>().SetTrigger("Up2");
        }

        public void ElevTop()
        {
            mng.GetComponent<GameMngr>().LevelUp();
        }

    }
}