using UnityEngine;
#if !UNITY_5_0_0
using UnityEngine.SceneManagement;
#endif
using System.Collections;
using System.Collections.Generic;


// Include in every scene - starts shit up!
public class SystemMain : Singleton<SystemMain>
{
	// Definition
	//////////////////////////////////////////////////////////////////////
		
	
	// Editable Variables
	//////////////////////////////////////////////////////////////////////	
			
	[SerializeField] AudioCue m_soundAmbient = null;
	[SerializeField] AudioCue m_music = null;

	[SerializeField] List<Component> m_systems = null;
				
	// Use this for initialization
	void Awake () 
	{	
		
		SetSingleton();		
		// Ensure there's only 1 SystemMain
		if ( Instance != null && Instance != this )
		{
			Destroy(gameObject);
			return;	
		}	
		DontDestroyOnLoad(this);	
				
		// Game Start!!
		OnGameStart();
		
		OnLevelLoad( );	
		
	}
	
	void Start()
	{
		//InputManager.Setup();
	}
	
	// Called every time it's a new level
	void OnLevelWasLoaded( int level )
	{				
		if ( Instance != null && Instance != this )
		{
			return;
		}
		OnLevelLoad();

		
	}
	
	// Update is called once per frame
	void Update () 
	{		
	}
		
	void OnGameStart()		
	{				
		//Screen.showCursor = false;
		
		foreach( Component obj in m_systems )
		{
			Transform trans = (GameObject.Instantiate(obj.gameObject) as GameObject).transform;
			trans.name = obj.name;
			trans.parent = transform;
		}	
		SystemAudio.PlayMusic(m_music);
		m_soundAmbient.Play();

	}
		
	void OnLevelLoad( )
	{		
		StartCoroutine(CoroutineOnLevelLoad());
	}

	IEnumerator CoroutineOnLevelLoad()
	{
		yield return new WaitForEndOfFrame();
	}
	
		
}
