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

public abstract class BasePhotoWindowManager : MonoBehaviour
{
	public static void SetCopyData(string title, string value)
	{
		if (!BasePhotoWindowManager.clipboard_.ContainsKey(title))
		{
			BasePhotoWindowManager.clipboard_.Add(title, value);
		}
		else
		{
			BasePhotoWindowManager.clipboard_[title] = value;
		}
	}

	public static string GetCopyData(string title)
	{
		if (!BasePhotoWindowManager.clipboard_.ContainsKey(title))
		{
			return string.Empty;
		}
		return BasePhotoWindowManager.clipboard_[title];
	}

	public void Reset()
	{
		int panelDepth = this.PanelDepth;
		Transform transform = base.gameObject.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			BasePhotoWindow component = transform.GetChild(i).gameObject.GetComponent<BasePhotoWindow>();
			if (!(component == null))
			{
				component.depth = panelDepth++;
				component.SetWindowManager(this);
			}
		}
	}

	public virtual void Awake()
	{
		BasePhotoWindowManager.clipboard_ = new Dictionary<string, string>();
		this.Reset();
		int panelDepth = this.PanelDepth;
		this.front_depth_ = -1;
		Transform transform = base.gameObject.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			BasePhotoWindow component = transform.GetChild(i).gameObject.GetComponent<BasePhotoWindow>();
			if (!(component == null) && component.enabled)
			{
				component.gameObject.SetActive(true);
				this.OnAddWindow(component);
				this.front_depth_ = Math.Max(this.front_depth_, component.depth);
				this.window_dic_.Add(new KeyValuePair<BasePhotoWindow, int>(component, component.depth));
				component.SetWindowManager(this);
			}
		}
	}

	public virtual void Start()
	{
		this.Deserialize();
	}

	protected virtual bool OnAddWindow(BasePhotoWindow add_win)
	{
		return true;
	}

	public BasePhotoWindow GetWindow(string uniqueWindowName)
	{
		foreach (KeyValuePair<BasePhotoWindow, int> keyValuePair in this.window_dic_)
		{
			if (keyValuePair.Key.uniqueName == uniqueWindowName)
			{
				return keyValuePair.Key;
			}
		}
		return null;
	}

	public virtual void ResetPosition()
	{
		foreach (KeyValuePair<BasePhotoWindow, int> keyValuePair in this.window_dic_)
		{
			keyValuePair.Key.ResetPosition();
		}
	}

	public void ActiveWindow(BasePhotoWindow window)
	{
		if (window.depth != this.front_depth_)
		{
			window.depth = ++this.front_depth_;
		}
		if (40 <= this.front_depth_)
		{
			SortedList<int, BasePhotoWindow> sortedList = new SortedList<int, BasePhotoWindow>();
			foreach (KeyValuePair<BasePhotoWindow, int> keyValuePair in this.window_dic_)
			{
				sortedList.Add(keyValuePair.Key.depth, keyValuePair.Key);
			}
			this.front_depth_ = -1;
			foreach (KeyValuePair<int, BasePhotoWindow> keyValuePair2 in sortedList)
			{
				keyValuePair2.Value.depth = ++this.front_depth_;
			}
		}
	}

	public void OnDestroy()
	{
		this.Serialize();
	}

	public Dictionary<string, Dictionary<string, string>> GetWoldStoreData(BasePhotoWindow photo_window)
	{
		return this.GetWoldStoreData(photo_window.name);
	}

	public Dictionary<string, Dictionary<string, string>> GetWoldStoreData(string name)
	{
		if (!this.world_store_data_.ContainsKey(name))
		{
			this.world_store_data_.Add(name, new Dictionary<string, Dictionary<string, string>>());
		}
		return this.world_store_data_[name];
	}

	public void Serialize()
	{
		string name = base.gameObject.name;
		Dictionary<string, Dictionary<string, string>> dictionary = new Dictionary<string, Dictionary<string, string>>();
		foreach (KeyValuePair<BasePhotoWindow, int> keyValuePair in this.window_dic_)
		{
			KeyValuePair<string, Dictionary<string, string>> keyValuePair2 = keyValuePair.Key.Serialize();
			dictionary.Add(keyValuePair2.Key, keyValuePair2.Value);
		}
		if (this.save_data_.ContainsKey(name))
		{
			this.save_data_.Remove(name);
		}
		this.save_data_.Add(name, dictionary);
		string save_fullpath_uidata = this.save_fullpath_uidata;
		Directory.CreateDirectory(save_fullpath_uidata.Replace(Path.GetFileName(save_fullpath_uidata), string.Empty));
		using (MemoryStream memoryStream = new MemoryStream())
		{
			using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
			{
				binaryWriter.Write(this.save_header_uidata);
				binaryWriter.Write(1170);
				binaryWriter.Write(this.save_data_.Count);
				foreach (KeyValuePair<string, Dictionary<string, Dictionary<string, string>>> keyValuePair3 in this.save_data_)
				{
					binaryWriter.Write(keyValuePair3.Key);
					binaryWriter.Write(keyValuePair3.Value.Count);
					foreach (KeyValuePair<string, Dictionary<string, string>> keyValuePair4 in keyValuePair3.Value)
					{
						binaryWriter.Write(keyValuePair4.Key);
						binaryWriter.Write(keyValuePair4.Value.Count);
						foreach (KeyValuePair<string, string> keyValuePair5 in keyValuePair4.Value)
						{
							binaryWriter.Write(keyValuePair5.Key);
							binaryWriter.Write(keyValuePair5.Value);
						}
					}
				}
			}
			File.WriteAllBytes(save_fullpath_uidata, memoryStream.ToArray());
		}
	}

	public void Deserialize()
	{
		this.save_data_ = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
		string save_fullpath_uidata = this.save_fullpath_uidata;
		Directory.CreateDirectory(save_fullpath_uidata.Replace(Path.GetFileName(save_fullpath_uidata), string.Empty));
		if (!File.Exists(save_fullpath_uidata))
		{
			return;
		}
		using (FileStream fileStream = new FileStream(save_fullpath_uidata, FileMode.Open))
		{
			if (fileStream == null)
			{
				return;
			}
			byte[] buffer = new byte[fileStream.Length];
			fileStream.Read(buffer, 0, (int)fileStream.Length);
			using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer)))
			{
				if (binaryReader.ReadString() != this.save_header_uidata)
				{
					NDebug.Assert(this.save_header_uidata + "ファイルのヘッダーが不正です。", false);
					return;
				}
				int num = binaryReader.ReadInt32();
				int num2 = binaryReader.ReadInt32();
				for (int i = 0; i < num2; i++)
				{
					string key = binaryReader.ReadString();
					int num3 = binaryReader.ReadInt32();
					Dictionary<string, Dictionary<string, string>> dictionary = new Dictionary<string, Dictionary<string, string>>();
					for (int j = 0; j < num3; j++)
					{
						string key2 = binaryReader.ReadString();
						int num4 = binaryReader.ReadInt32();
						Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
						for (int k = 0; k < num4; k++)
						{
							string key3 = binaryReader.ReadString();
							string value = binaryReader.ReadString();
							dictionary2.Add(key3, value);
						}
						dictionary.Add(key2, dictionary2);
					}
					this.save_data_.Add(key, dictionary);
				}
			}
		}
		string name = base.gameObject.name;
		if (this.save_data_.ContainsKey(name))
		{
			Dictionary<string, Dictionary<string, string>> dictionary3 = this.save_data_[name];
			if (dictionary3 != null && 0 < dictionary3.Count)
			{
				foreach (KeyValuePair<BasePhotoWindow, int> keyValuePair in this.window_dic_)
				{
					keyValuePair.Key.Deserialize(dictionary3);
				}
			}
		}
	}

	protected abstract string save_fullpath_uidata { get; }

	protected abstract string save_header_uidata { get; }

	public int PanelDepth;

	protected List<KeyValuePair<BasePhotoWindow, int>> window_dic_ = new List<KeyValuePair<BasePhotoWindow, int>>();

	protected Dictionary<string, Dictionary<string, Dictionary<string, string>>> world_store_data_ = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();

	private static Dictionary<string, string> clipboard_ = new Dictionary<string, string>();

	private int front_depth_;

	private Dictionary<string, Dictionary<string, Dictionary<string, string>>> save_data_;
}
