state("Twisted Tower Demo") { }

startup
{
    // Load asl-help binary and instantiate it - will inject code into the asl in the background
    Assembly.Load(File.ReadAllBytes("Components/asl-help")).CreateInstance("Unity");

    // Set the helper to load the scene manager, you probably want this (the helper is set at vars.Helper automagically)
    vars.Helper.LoadSceneManager = true;

    //Setting Game Name and toggling alert to ensure runner is comparing against Game TIme
    vars.Helper.GameName = "Twisted Tower Demo";
    vars.Helper.AlertLoadless();

    vars.SceneLoading = "";
    vars.TextDisplaySceneLoading = "";

    //creates text components for variable information
	vars.SetTextComponent = (Action<string, string>)((id, text) =>
	{
	        var textSettings = timer.Layout.Components.Where(x => x.GetType().Name == "TextComponent").Select(x => x.GetType().GetProperty("Settings").GetValue(x, null));
	        var textSetting = textSettings.FirstOrDefault(x => (x.GetType().GetProperty("Text1").GetValue(x, null) as string) == id);
	        if (textSetting == null)
	        {
	        var textComponentAssembly = Assembly.LoadFrom("Components\\LiveSplit.Text.dll");
	        var textComponent = Activator.CreateInstance(textComponentAssembly.GetType("LiveSplit.UI.Components.TextComponent"), timer);
	        timer.Layout.LayoutComponents.Add(new LiveSplit.UI.Components.LayoutComponent("LiveSplit.Text.dll", textComponent as LiveSplit.UI.Components.IComponent));
	
	        textSetting = textComponent.GetType().GetProperty("Settings", BindingFlags.Instance | BindingFlags.Public).GetValue(textComponent, null);
	        textSetting.GetType().GetProperty("Text1").SetValue(textSetting, id);
	        }
	
	        if (textSetting != null)
	        textSetting.GetType().GetProperty("Text2").SetValue(textSetting, text);
    });

    //Parent setting
	settings.Add("Variable Information", true, "Variable Information");
	//Child settings that will sit beneath Parent setting
    settings.Add("Unity Scene Loading", true, "Unity Scene Loading", "Variable Information");
    settings.Add("Loading Scene Name", true, "Loading Scene Name", "Variable Information");
    settings.Add("Active Scene Name", true, "Active Scene Name", "Variable Information");
}

init

{
    //helps clear some errors when scene is null
    current.Scene = "";
    current.activeScene = "";
    current.loadingScene = "";

    // This is where we will load custom properties from the code, EMPTY FOR NOW
    vars.Helper.TryLoad = (Func<dynamic, bool>)(mono =>
    {
        return true;
    });

    //Enable if having scene print issues - a custom function defined in init, the `scene` is the scene's address (e.g. vars.Helper.Scenes.Active.Address)
    vars.ReadSceneName = (Func<IntPtr, string>)(scene => {
    string name = vars.Helper.ReadString(256, ReadStringType.UTF8, scene + 0x38);
    return name == "" ? null : name;
    });
}

update
{
    // Get the current active scene's name and set it to `current.activeScene` - sometimes, it is null, so fallback to old value
    current.activeScene = vars.Helper.Scenes.Active.Name ?? current.activeScene;

    // Usually the scene that's loading, a bit jank in this version of asl-help
    current.loadingScene = vars.Helper.Scenes.Loaded[0].Name ?? current.loadingScene;

    // Log changes to the active scene
    if (old.activeScene != current.activeScene) {
        vars.Log("activeScene: " + old.activeScene + " -> " + current.activeScene);
    }
    if (old.loadingScene != current.loadingScene) {
        vars.Log("loadingScene: " + old.loadingScene + " -> " + current.loadingScene);
    }

    //Setting up for load removal & text display of load removal stuff
    if(old.loadingScene != current.loadingScene)
        {
            vars.SceneLoading = "Loading";
        }
    if(old.activeScene != current.activeScene)
        {
            vars.SceneLoading = "Not Loading";
        }

    //Prints whether a scene is loading or not
    if(settings["Unity Scene Loading"]){vars.SetTextComponent("Scene Loading?",vars.SceneLoading.ToString());}
    //Prints the name of the currently loading scene
    if(settings["Loading Scene Name"]){vars.SetTextComponent("LScene Name: ",current.loadingScene.ToString());}
    //Prints the name of the current active scene
    if(settings["Active Scene Name"]){vars.SetTextComponent("AScene Name: ",current.activeScene.ToString());}
}

onStart
{
    vars.Log("activeScene: " + current.activeScene);
    vars.Log("loadingScene: " + current.loadingScene);
}

start
{
    return 
    current.activeScene == "Exterior" && vars.SceneLoading == "Not Loading" || old.activeScene != "Exterior" && current.activeScene == "Exterior";
}

split
{
    return old.activeScene != current.activeScene;
}

isLoading
{
    return vars.SceneLoading == "Loading";
}