using System;
using UnityEngine;
using wf;

public class ScenePresetWindow : BaseMaidPhotoWindow
{
	public override void Awake()
	{
		base.Awake();
		PhotoScenePreset.Create();
	}

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

	public void Init()
	{
		for (int i = 0; i < PhotoScenePreset.datas.Count; i++)
		{
			this.CreateScenePresetButton(this.contentGrid.gameObject, PhotoScenePreset.datas[i]);
		}
		Utility.ResetNGUI(this.contentGrid);
		Utility.ResetNGUI(NGUITools.FindInParents<UIScrollView>(this.contentGrid.transform));
		this.UpdateChildren();
	}

	public override void OnDeserializeEvent()
	{
		ScenePresetWindow.SelectData = null;
	}

	public GameObject CreateScenePresetButton(GameObject parentObject, PhotoScenePreset.Data data)
	{
		GameObject gameObject = Utility.CreatePrefab(parentObject, "ScenePhotoMode/ScenePresetButton", true);
		gameObject.name = data.id.ToString();
		UI2DSprite component = UTY.GetChildObject(gameObject, "Plate/Plate/BG/ThumImage", false).GetComponent<UI2DSprite>();
		if (!string.IsNullOrEmpty(data.thumbnailName))
		{
			Texture2D texture2D = ImportCM.CreateTexture(data.thumbnailName + ".tex");
			Sprite sprite2D = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
			if (component.sprite2D != null && component.sprite2D.texture != null)
			{
				UnityEngine.Object.DestroyImmediate(component.sprite2D.texture);
			}
			component.sprite2D = sprite2D;
			component.SetDimensions(texture2D.width, texture2D.height);
		}
		UIButton componentInChildren = gameObject.GetComponentInChildren<UIButton>();
		componentInChildren.name = data.id.ToString();
		EventDelegate.Add(componentInChildren.onClick, new EventDelegate.Callback(this.OnClickLoadBtn));
		return gameObject;
	}

	private void OnClickLoadBtn()
	{
		int id = int.Parse(UIButton.current.name);
		GameMain.Instance.SysDlg.Show("現在の配置情報はリセットされます。\n宜しいですか？", SystemDialog.TYPE.OK_CANCEL, delegate
		{
			GameMain.Instance.SysDlg.Close();
			this.Load(id);
		}, null);
	}

	private void Load(int id)
	{
		foreach (PhotoScenePreset.Data data in PhotoScenePreset.datas)
		{
			if (data.id == id)
			{
				byte[] array = null;
				using (AFileBase afileBase = GameUty.FileSystem.FileOpen(data.loadFileName))
				{
					array = afileBase.ReadAll();
				}
				if (array != null)
				{
					ScenePresetWindow.SelectData = data;
					base.mgr.SaveAndLoadManager.ReadSave(array);
				}
				break;
			}
		}
	}

	[HideInInspector]
	public static PhotoScenePreset.Data SelectData;

	[SerializeField]
	private UIGrid contentGrid;
}
