using UnityEngine;

namespace Level
{
    public class Level : MonoBehaviour
    {
        public const string ResourcesDirectory = "Levels/";
        public string LevelName { get; private set; } = "";

        public Transform StartingPoint { get; private set; } = null;

        private GameObject prefab = null;
        private GameObject instance = null;
        private bool needsRestart = true;

        public void SetLevel(string levelName)
        {
            LevelName = levelName;

            prefab = Resources.Load<GameObject>($"{ ResourcesDirectory }{ levelName }");
            Disable();
        }

        public void Enable()
        {
            gameObject.SetActive(true);
            needsRestart = true;
        }

        public void Disable()
        {
            if (!needsRestart)
                return;

            if (instance != null)
                Destroy(instance);

            instance = Instantiate(prefab, transform.position, Quaternion.identity, transform);
            StartingPoint = instance.transform.Find("Starting Point");
            gameObject.SetActive(false);
            needsRestart = false;
        }
    }
}
