using UnityEngine;
using System.Collections.Generic;

public class LoadNewScenarioController {

    public ScenarioLibrary scenariosLibrary;
    public List<ScenarioDescriptor> scenarios;

    private List<GameObject> loadedObjects = new List<GameObject>();

    private HUDGUIState hudGUI;

	public void Initialize (HUDGUIState hudGUI) {
        scenariosLibrary = GameObject.FindObjectOfType<ScenarioLibrary>();
        if (scenariosLibrary == null) {
            GameObject tmpGO = (GameObject)Resources.Load("r_ScenarioLibrary");
            Object.Instantiate(tmpGO);
            scenariosLibrary = GameObject.FindObjectOfType<ScenarioLibrary>();
        }
        scenarios = scenariosLibrary.scenarios;

        this.hudGUI = hudGUI;
	}

    public List<GameObject> LoadNewScenario(Scenarios scenario) {
        ClearOldScenario();

        for (int i = 0; i < scenarios.Count; i++) {
            if (scenarios[i].scenario == scenario) {
                foreach (GameObject obj in scenarios[i].associatedObjects) {
                    GameObject tmpGO = (GameObject)Resources.Load("Scenarios/" + obj.name);
                    GameObject loadedObj = (GameObject)Object.Instantiate(tmpGO);
                    loadedObjects.Add(loadedObj);
                }

                hudGUI.SetGuiText(scenariosLibrary.scenarios[i].missionText);
            }
        }

        return loadedObjects;
    }

    public void ClearOldScenario() {
        if (loadedObjects != null && loadedObjects.Count > 0) {
            for (int i = 0; i < loadedObjects.Count; i++) {
                Object.Destroy(loadedObjects[i]);
                loadedObjects.Remove(loadedObjects[i]);
            }

            loadedObjects.Clear();
        }
    }
}
