﻿//----------------------------------------------
// Copyright © 2015 HongXun
//----------------------------------------------

#define Debug
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Core.Gui
{
	public class Sct_MainMgr
	{
		private static Sct_MainMgr _sct_MainMgr = null;

		public static Sct_MainMgr Instance {
			get {
				if (_sct_MainMgr == null) {
					_sct_MainMgr = new Sct_MainMgr ();
				}
				return _sct_MainMgr;
			}
		}
		/// <summary>
		/// 設定與取得Root
		/// </summary>
		private UIRoot _mainRoot = null;

		public UIRoot MainRoot {
			set {
				_mainRoot = value;
			}
			get {
				return _mainRoot;
			}
		}

		/// <summary>
		/// 目前所在的場景名稱
		/// </summary>
		private string _nowSceneName = string.Empty;

		public string NowSceneName {
			private set {
				_nowSceneName = value;
			}
			get {
				return _nowSceneName;
			}
		}

		/// <summary>
		/// 目前的UI腳本
		/// </summary>
		private Sct_UIBase _sceneUI = null;

		public Sct_UIBase SceneUi {
			private set {
				_sceneUI = value;
			}
			get {
				return _sceneUI;
			}

		}

		/// <summary>
		/// 目前呼叫的共用場景名稱
		/// </summary>
		private string _nowGlobalSceneName = string.Empty;
		
		public string NowGlobalSceneName {
			private set {
				_nowGlobalSceneName = value;
			}
			get {
				return _nowGlobalSceneName;
			}
		}

		/// <summary>
		/// 目前呼叫的共用UI腳本
		/// </summary>
		private Sct_UIBase _globalSceneUI = null;
		
		public Sct_UIBase GlobalSceneUi {
			private set {
				_globalSceneUI = value;
			}
			get {
				return _globalSceneUI;
			}
			
		}

		private Sct_MainBase _main = null;

		public Sct_MainBase Main {
			get {
				if (_main == null) {
					_main = _mainRoot.GetComponent<Sct_MainBase> ();
				}
				return _main;
			}
		}

		/// <summary> 呼叫場景 </summary>
		/// <param name='sceneName'> 場景名 </param>
		/// <param name='sceneName'> 要傳給下一個場景的參數(ArrayList) </param>
		public void CallScene (string sceneName, ArrayList data = null)
		{
			Action destroyUISctAndPrefab = delegate() {
				if (SceneUi == null) {
					return ;
				}
				
				//呼叫刪除場景
				SceneUi.SceneDestroy ();
			};
			destroyUISctAndPrefab();
			Type sctUI = Main.CallScene(sceneName);
			Component uiComponent = MainRoot.gameObject.AddComponent(sctUI);
			SceneUi = (Sct_UIBase)uiComponent;
			//生成新的場景
			if (SceneUi != null) {
				NowSceneName = sceneName;
				//傳遞資料
				SceneUi.Datalist = data;
				//建立場景
				SceneUi.SceneInit ();
			} else {
				Debug.LogErrorFormat ("Can not Find sceneUI Component[{0}]", sceneName);
			}
		}

		/// <summary> 呼叫共用場景 </summary>
		/// <param name='sceneName'> 共用場景名 </param>
		/// <param name='sceneName'> 要傳給共用場景的參數(ArrayList) </param>
		public void CallGlobalScene(string sceneName, ArrayList data = null)
		{
			Action destroyUISctAndPrefab = delegate() {
				if (GlobalSceneUi == null) {
					return ;
				}
				
				//呼叫刪除場景
				GlobalSceneUi.SceneDestroy ();
			};
			destroyUISctAndPrefab();
			Type sctUI = Main.CallGlobalScene(sceneName);
			Component uiComponent = MainRoot.gameObject.AddComponent(sctUI);
			GlobalSceneUi = (Sct_UIBase)uiComponent;
			if (GlobalSceneUi != null) {
				NowGlobalSceneName = sceneName;
				//傳遞資料
				GlobalSceneUi.Datalist = data;
				//建立場景
				GlobalSceneUi.SceneInit ();
			} else {
				Debug.LogErrorFormat ("Can not Find global sceneUI Component[{0}]", sceneName);
			}
		}
	}
}
