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

public class CreateBGObjectSubWindow : BasePhotoSubWindow
{
	public void Awake()
	{
		PhotoBGObjectData.Create();
		if (this.addButtonList.popup_value_list != null)
		{
			return;
		}
		Dictionary<string, List<KeyValuePair<string, object>>> dictionary = new Dictionary<string, List<KeyValuePair<string, object>>>();
		foreach (KeyValuePair<string, List<PhotoBGObjectData>> keyValuePair in PhotoBGObjectData.category_list)
		{
			if (!dictionary.ContainsKey(keyValuePair.Key))
			{
				dictionary.Add(keyValuePair.Key, new List<KeyValuePair<string, object>>());
			}
			for (int i = 0; i < keyValuePair.Value.Count; i++)
			{
				dictionary[keyValuePair.Key].Add(new KeyValuePair<string, object>(keyValuePair.Value[i].name, keyValuePair.Value[i]));
			}
		}
		this.addButtonList.SetData(dictionary, false);
		this.addButtonList.onClickEventList.Add(new Action<object>(this.OnClickItem));
		this.addButtonList.popup_value_list = PhotoBGObjectData.popup_category_list;
	}

	public override void Initizalize(BasePhotoWindow parentWindow)
	{
		this.Awake();
		base.Initizalize(parentWindow);
		parentWindow.UpdateChildren();
		this.addButtonList.SetPopupValue(PhotoBGObjectData.popup_category_list[0].Key);
	}

	public void OnDestroy()
	{
		if (GameMain.Instance == null || GameMain.Instance.BgMgr == null || GameMain.Instance.BgMgr.bg_parent_object == null)
		{
			return;
		}
		foreach (CreateBGObjectSubWindow.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();
	}

	private void OnClickItem(object select_data_obj)
	{
		if (select_data_obj == null)
		{
			return;
		}
		PhotoBGObjectData add_bg_data = select_data_obj as PhotoBGObjectData;
		this.AddObject(add_bg_data, string.Empty);
	}

	public void AddMyObject(string filePath)
	{
		PhotoBGObjectData photoBGObjectData = PhotoBGObjectData.AddMyObject(filePath);
		this.addButtonList.AddData("マイオブジェクト", new KeyValuePair<string, object>(photoBGObjectData.name, photoBGObjectData));
	}

	public void UpdateMyObject(string filePath)
	{
		foreach (CreateBGObjectSubWindow.BgObject bgObject in this.create_obj_list_)
		{
			if (!string.IsNullOrEmpty(bgObject.data.direct_file))
			{
				if (filePath == bgObject.data.direct_file)
				{
					PhotoCustomObjectPlane component = bgObject.game_object.GetComponent<PhotoCustomObjectPlane>();
					component.Desilialize(File.ReadAllBytes(filePath));
				}
			}
		}
	}

	private CreateBGObjectSubWindow.BgObject AddObject(PhotoBGObjectData add_bg_data, string create_time = "")
	{
		if (add_bg_data == null)
		{
			return null;
		}
		CreateBGObjectSubWindow.BgObject bgObject = new CreateBGObjectSubWindow.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);
		return bgObject;
	}

	public void RemoveObject(GameObject removeObject)
	{
		if (removeObject == null)
		{
			return;
		}
		CreateBGObjectSubWindow.BgObject bgObject = null;
		foreach (CreateBGObjectSubWindow.BgObject bgObject2 in this.create_obj_list_)
		{
			if (bgObject2.game_object == removeObject)
			{
				bgObject = bgObject2;
				break;
			}
		}
		if (bgObject != null)
		{
			this.RemoveObject(bgObject);
		}
	}

	private void RemoveObject(CreateBGObjectSubWindow.BgObject removeObject)
	{
		if (removeObject == null)
		{
			return;
		}
		ObjectManagerWindow objectManagerWindow = this.mgr.GetWindow(PhotoWindowManager.WindowType.ObjectManager) as ObjectManagerWindow;
		objectManagerWindow.RemoveTransTargetObject(removeObject.game_object);
		UnityEngine.Object.DestroyImmediate(removeObject.game_object);
		this.create_obj_list_.Remove(removeObject);
	}

	public void OnSerializeEvent(ref Dictionary<string, Dictionary<string, string>> storeData)
	{
		for (int i = 0; i < this.create_obj_list_.Count; i++)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			CreateBGObjectSubWindow.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();
			storeData.Add(bgObject.create_time, dictionary);
		}
	}

	public void OnDeserializeEvent(ref Dictionary<string, Dictionary<string, string>> storeData)
	{
		this.OnDestroy();
		List<string> list = new List<string>();
		foreach (KeyValuePair<string, Dictionary<string, string>> keyValuePair in storeData)
		{
			Dictionary<string, string> value = keyValuePair.Value;
			long id = long.Parse(value["id"]);
			string key = keyValuePair.Key;
			CreateBGObjectSubWindow.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 i = 0; i < list.Count; i++)
		{
			storeData.Remove(list[i]);
		}
		Utility.ResetNGUI(this.addButtonList.ScrollView);
	}

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

	public PopupAndButtonList addButtonList;

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

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

		public int CompareTo(CreateBGObjectSubWindow.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;
	}
}
