using System;
using System.Collections.Generic;
using UnityEngine;
using wf;

public class BGObjectWindow : BaseMaidPhotoWindow
{
	public override void Awake()
	{
		base.Awake();
		PhotoBGObjectData.Create();
		this.scroll_view_ = NGUITools.FindInParents<UIScrollView>(this.Grid.transform);
		this.add_scroll_view_ = NGUITools.FindInParents<UIScrollView>(this.AddGrid.transform);
		for (int i = 0; i < PhotoBGObjectData.data.Count; i++)
		{
			PhotoBGObjectData photoBGObjectData = PhotoBGObjectData.data[i];
			GameObject gameObject = Utility.CreatePrefab(this.Grid.gameObject, "ScenePhotoMode/WindowListItemNormalButton", true);
			gameObject.name = photoBGObjectData.id.ToString();
			gameObject.GetComponentInChildren<UILabel>().text = photoBGObjectData.name;
			EventDelegate.Add(gameObject.GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnClickAdd));
		}
		NGUITools.FindInParents<UIScrollView>(this.Grid.transform).ResetPosition();
		this.Grid.Reposition();
		this.UpdateChildren();
	}

	public override void Start()
	{
		base.Start();
	}

	public void OnDestroy()
	{
		if (GameMain.Instance == null || GameMain.Instance.BgMgr == null || GameMain.Instance.BgMgr.bg_parent_object == null)
		{
			return;
		}
		foreach (BGObjectWindow.BgObject bgObject in this.create_obj_list_)
		{
			if (this.mgr != null && this.mgr.GetWindow(PhotoWindowManager.WindowType.ObjectManager) != null)
			{
				ObjectManagerWindow objectManagerWindow = this.mgr.GetWindow(PhotoWindowManager.WindowType.ObjectManager) as ObjectManagerWindow;
				objectManagerWindow.RemoveTransTargetObject(bgObject.game_object);
			}
			UnityEngine.Object.DestroyImmediate(bgObject.game_object);
		}
		this.create_obj_list_.Clear();
	}

	public void OnClickAdd()
	{
		this.AddObject(PhotoBGObjectData.Get(long.Parse(UIButton.current.name)), string.Empty);
	}

	public void OnClickRemove()
	{
		string name = UIButton.current.name;
		for (int i = 0; i < this.create_obj_list_.Count; i++)
		{
			if (this.create_obj_list_[i].create_time == name)
			{
				ObjectManagerWindow objectManagerWindow = this.mgr.GetWindow(PhotoWindowManager.WindowType.ObjectManager) as ObjectManagerWindow;
				objectManagerWindow.RemoveTransTargetObject(this.create_obj_list_[i].game_object);
				UnityEngine.Object.DestroyImmediate(this.create_obj_list_[i].game_object);
				this.create_obj_list_.RemoveAt(i);
				UnityEngine.Object.DestroyImmediate(UIButton.current.gameObject);
				this.AddGrid.Reposition();
				this.add_scroll_view_.UpdateScrollbars();
				break;
			}
		}
	}

	public override void OnSerializeEvent()
	{
		base.OnSerializeEvent();
		Dictionary<string, Dictionary<string, string>> woldStoreData = base.GetWoldStoreData();
		woldStoreData.Clear();
		for (int i = 0; i < this.create_obj_list_.Count; i++)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			BGObjectWindow.BgObject bgObject = this.create_obj_list_[i];
			Transform transform = bgObject.game_object.transform;
			dictionary["id"] = bgObject.data.id.ToString();
			dictionary["position"] = transform.position.ToString("G9");
			dictionary["rotation"] = transform.rotation.ToString("G9");
			dictionary["scale"] = transform.localScale.ToString("G9");
			dictionary["visible"] = transform.gameObject.activeSelf.ToString();
			woldStoreData.Add(bgObject.create_time, dictionary);
		}
	}

	public override void OnDeserializeEvent()
	{
		UIButton current = UIButton.current;
		List<Transform> childList = this.AddGrid.GetChildList();
		for (int i = 0; i < childList.Count; i++)
		{
			UIButton.current = childList[i].GetComponentInChildren<UIButton>();
			this.OnClickRemove();
		}
		UIButton.current = current;
		this.OnDestroy();
		this.create_obj_list_.Clear();
		List<string> list = new List<string>();
		Dictionary<string, Dictionary<string, string>> woldStoreData = base.GetWoldStoreData();
		foreach (KeyValuePair<string, Dictionary<string, string>> keyValuePair in woldStoreData)
		{
			Dictionary<string, string> value = keyValuePair.Value;
			long id = long.Parse(value["id"]);
			string key = keyValuePair.Key;
			BGObjectWindow.BgObject bgObject = this.AddObject(PhotoBGObjectData.Get(id), key);
			if (bgObject != null && bgObject.game_object != null)
			{
				Transform transform = bgObject.game_object.transform;
				transform.position = Parse.Vector3(value["position"]);
				transform.rotation = Parse.Quaternion(value["rotation"]);
				transform.localScale = Parse.Vector3(value["scale"]);
				bgObject.game_object.SetActive(bool.Parse(value["visible"]));
			}
			else
			{
				list.Add(keyValuePair.Key);
			}
		}
		for (int j = 0; j < list.Count; j++)
		{
			woldStoreData.Remove(list[j]);
		}
		this.Grid.Reposition();
		this.AddGrid.Reposition();
		this.scroll_view_.ResetPosition();
		this.add_scroll_view_.ResetPosition();
	}

	private BGObjectWindow.BgObject AddObject(PhotoBGObjectData add_bg_data, string create_time = "")
	{
		if (add_bg_data == null)
		{
			return null;
		}
		BGObjectWindow.BgObject bgObject = new BGObjectWindow.BgObject();
		bgObject.data = add_bg_data;
		if (!string.IsNullOrEmpty(create_time))
		{
			bgObject.create_time = create_time;
		}
		for (int i = 0; i < this.create_obj_list_.Count; i++)
		{
			if (this.create_obj_list_[i].create_time == bgObject.create_time)
			{
				return null;
			}
		}
		bgObject.game_object = bgObject.data.Instantiate(bgObject.create_time);
		if (bgObject.game_object == null)
		{
			return null;
		}
		this.create_obj_list_.Add(bgObject);
		this.create_obj_list_.Sort();
		ObjectManagerWindow objectManagerWindow = this.mgr.GetWindow(PhotoWindowManager.WindowType.ObjectManager) as ObjectManagerWindow;
		objectManagerWindow.AddTransTargetObject(bgObject.game_object, bgObject.data.name, PhotoTransTargetObject.Type.Prefab);
		GameObject gameObject = Utility.CreatePrefab(this.AddGrid.gameObject, "ScenePhotoMode/WindowListItemNormalButton", true);
		gameObject.name = bgObject.create_time.ToString();
		gameObject.GetComponentInChildren<UILabel>().text = bgObject.data.name;
		EventDelegate.Add(gameObject.GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnClickRemove));
		this.AddGrid.Reposition();
		this.add_scroll_view_.UpdateScrollbars();
		return bgObject;
	}

	public new PhotoWindowManager mgr
	{
		get
		{
			return base.mgr;
		}
	}

	public UIGrid Grid;

	public UIGrid AddGrid;

	private UIScrollView scroll_view_;

	private UIScrollView add_scroll_view_;

	private List<BGObjectWindow.BgObject> create_obj_list_ = new List<BGObjectWindow.BgObject>();

	private class BgObject : IComparable<BGObjectWindow.BgObject>
	{
		public BgObject()
		{
			this.create_time = DateTime.Now.ToString("yyyyMMddHHmmssfff");
		}

		public int CompareTo(BGObjectWindow.BgObject obj)
		{
			if (obj == null)
			{
				return 1;
			}
			return long.Parse(this.create_time).CompareTo(long.Parse(obj.create_time));
		}

		public string create_time;

		public GameObject game_object;

		public PhotoBGObjectData data;
	}
}
