using Godot;

namespace Newlon;

//
// Class that gives access to level data, pools and etc.
//

public partial class LevelController : Node
{
    public static LevelController Instance { get; private set; }

    private bool _isLevelRunning = false;

    public override void _EnterTree()
    {
        Instance = this;
    }
    
    public override void _ExitTree()
    {
        Instance = null;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="levelTileset">Scene that will be loaded</param>
    /// <param name="levelScript">Optional script that will be attached to loaded scene</param>
    public void StartLevel(PackedScene levelTileset, Script levelScript = null)
    {
        if (_isLevelRunning)
            return;

        GetTree().ChangeSceneToPacked(levelTileset);

        if (levelScript != null)
            GetTree().CurrentScene.SetScript(levelScript);
        
        _isLevelRunning = true;
    }

    public void EndLevel()
    {
        if (_isLevelRunning == false)
            return;

        _isLevelRunning = false;
    }
}
