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

public class PortalController : MonoBehaviour
{
    /// <summary>
    /// The level to load upon enter
    /// </summary>
    public int NextLevel;

    public Players PlayerType;

    public static bool HumanInPortal;

    public static bool GhostInPortal;

    private LevelController levelController;

    void Start()
    {
        levelController = LevelController.Instance;

#if DEBUG
        GhostInPortal = false;
#endif
    }

    // OnTriggerEnter2D is called when the Collider2D other enters the trigger (2D physics only)
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "PlayerHuman" && PlayerType == Players.Human)
        {
            HumanInPortal = true;
        }

        if (collision.tag == "PlayerGhost" && PlayerType == Players.Ghost)
        {
            GhostInPortal = true;
        }

        if (HumanInPortal && GhostInPortal)
        {
            HumanInPortal = false;
            GhostInPortal = false;
            levelController.NextLevel();
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "PlayerHuman" && PlayerType == Players.Human)
        {
            HumanInPortal = false;
        }

        if (collision.tag == "PlayerGhost" && PlayerType == Players.Ghost)
        {
            GhostInPortal = false;
        }
    }

    public enum Players
    {
        Human,
        Ghost
    }
}
