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

public class AlignmentPresetWindow : BaseMaidPhotoWindow
{
	public override void Awake()
	{
		base.Awake();
		PhotoAlignmentPreset.Create();
		this.noCharaActive = base.GetComponentInChildren<PhotoNoCharaActive>();
	}

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

	public void Init()
	{
		for (int i = 0; i < PhotoAlignmentPreset.datas.Count; i++)
		{
			this.CreateAlignmentPresetButton(this.contentGrid.gameObject, PhotoAlignmentPreset.datas[i]);
		}
		Utility.ResetNGUI(this.contentGrid);
		Utility.ResetNGUI(NGUITools.FindInParents<UIScrollView>(this.contentGrid.transform));
		Utility.ResetNGUI(this.maidListGrid);
		Utility.ResetNGUI(NGUITools.FindInParents<UIScrollView>(this.maidListGrid.transform));
		this.UpdateMaidList();
		this.noCharaActive.ChangeMode(true, true);
		UIButton[] componentsInChildren = this.contentGrid.GetComponentsInChildren<UIButton>();
		foreach (UIButton uibutton in componentsInChildren)
		{
			uibutton.isEnabled = false;
		}
	}

	public override void OnMaidAddEvent(Maid maid, bool is_deserialize_load)
	{
		if (maid == null || maid.boMAN)
		{
			return;
		}
		this.activeMaidList.Add(maid);
		this.UpdateMaidList();
		this.noCharaActive.ChangeMode(false, true);
	}

	public void OnSelectMaid()
	{
		GameObject gameObject = UIButton.current.transform.parent.gameObject;
		MaidPlate component = gameObject.GetComponent<MaidPlate>();
		bool selected = UIWFSelectButton.selected;
		if (selected)
		{
			for (int i = 0; i < this.selectMaidList.Length; i++)
			{
				if (this.selectMaidList[i] == null)
				{
					this.selectMaidList[i] = component;
					this.selectMaidList[i].SetSelectNoVisible(i + 1, true);
					break;
				}
			}
		}
		else
		{
			for (int j = 0; j < this.selectMaidList.Length; j++)
			{
				if (this.selectMaidList[j] == component)
				{
					this.selectMaidList[j].SetSelectNoVisible(j + 1, false);
					this.selectMaidList[j] = null;
					break;
				}
			}
		}
		bool flag = false;
		int num = 0;
		while (num < this.selectMaidList.Length && !flag)
		{
			if (this.selectMaidList[num] != null)
			{
				flag = true;
			}
			num++;
		}
		UIButton[] componentsInChildren = this.contentGrid.GetComponentsInChildren<UIButton>();
		foreach (UIButton uibutton in componentsInChildren)
		{
			uibutton.isEnabled = flag;
		}
	}

	public override void OnMaidRemoveEvent(Maid maid)
	{
		if (maid == null || maid.boMAN)
		{
			return;
		}
		this.activeMaidList.Remove(maid);
		this.UpdateMaidList();
		if (this.activeMaidList.Count <= 0)
		{
			this.noCharaActive.ChangeMode(true, true);
		}
	}

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

	public void UpdateMaidList()
	{
		Dictionary<Maid, int> dictionary = new Dictionary<Maid, int>();
		for (int i = 0; i < this.selectMaidList.Length; i++)
		{
			if (this.selectMaidList[i] != null)
			{
				this.selectMaidList[i].SetSelectNoVisibleAllOff();
				bool flag = this.activeMaidList.Contains(this.selectMaidList[i].maid);
				if (flag)
				{
					dictionary.Add(this.selectMaidList[i].maid, i);
				}
			}
			this.selectMaidList[i] = null;
		}
		MaidPlate[] array = new MaidPlate[this.selectMaidList.Length];
		List<Transform> childList = this.maidListGrid.GetChildList();
		for (int j = 0; j < childList.Count; j++)
		{
			UnityEngine.Object.DestroyImmediate(childList[j].gameObject);
		}
		this.maidListGrid.transform.DetachChildren();
		List<Maid> sortedActiveMaidList = this.GetSortedActiveMaidList();
		for (int k = 0; k < sortedActiveMaidList.Count; k++)
		{
			GameObject gameObject = Utility.CreatePrefab(this.maidListGrid.gameObject, "ScenePhotoMode/SimpleMaidPlate", true);
			gameObject.GetComponent<MaidPlate>().SetMaidData(sortedActiveMaidList[k]);
			UIWFSelectButton component = UTY.GetChildObject(gameObject, "Button", false).GetComponent<UIWFSelectButton>();
			UIWFTabButton component2 = UTY.GetChildObject(gameObject, "Button", false).GetComponent<UIWFTabButton>();
			UnityEngine.Object.Destroy(component2);
			component.enabled = true;
			EventDelegate.Add(component.onSelect, new EventDelegate.Callback(this.OnSelectMaid));
			if (dictionary.ContainsKey(sortedActiveMaidList[k]))
			{
				array[dictionary[sortedActiveMaidList[k]]] = gameObject.GetComponent<MaidPlate>();
			}
		}
		this.switchPanel.SetNumberOfSelected(5);
		this.switchPanel.UpdateChildren();
		Utility.ResetNGUI(this.maidListGrid);
		Utility.ResetNGUI(NGUITools.FindInParents<UIScrollView>(this.maidListGrid.transform));
		this.UpdateChildren();
		for (int l = 0; l < array.Length; l++)
		{
			if (!(array[l] == null))
			{
				UIWFSelectButton component3 = UTY.GetChildObject(array[l].gameObject, "Button", false).GetComponent<UIWFSelectButton>();
				this.switchPanel.Select(component3);
			}
		}
		bool isEnabled = false;
		this.selectMaidList = array;
		for (int m = 0; m < this.selectMaidList.Length; m++)
		{
			if (this.selectMaidList[m] != null)
			{
				this.selectMaidList[m].SetSelectNoVisibleAllOff();
				this.selectMaidList[m].SetSelectNoVisible(m + 1, true);
				isEnabled = true;
			}
		}
		UIButton[] componentsInChildren = this.contentGrid.GetComponentsInChildren<UIButton>();
		foreach (UIButton uibutton in componentsInChildren)
		{
			uibutton.isEnabled = isEnabled;
		}
	}

	public GameObject CreateAlignmentPresetButton(GameObject parentObject, PhotoAlignmentPreset.Data data)
	{
		GameObject gameObject = Utility.CreatePrefab(parentObject, "ScenePhotoMode/AlignmentPresetButton", 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()
	{
		GameObject gameObject = UIButton.current.transform.parent.parent.gameObject;
		int id = int.Parse(gameObject.name);
		PhotoAlignmentPreset.Data data = PhotoAlignmentPreset.GetData(id);
		GameMain.Instance.SysDlg.Show("選択した配置に変更します。\n宜しいですか？", SystemDialog.TYPE.OK_CANCEL, delegate
		{
			GameMain.Instance.SysDlg.Close();
			this.ExecAlignmentPreset(data);
		}, null);
	}

	private void ExecAlignmentPreset(PhotoAlignmentPreset.Data data)
	{
		for (int i = 0; i < this.selectMaidList.Length; i++)
		{
			if (this.selectMaidList[i] != null)
			{
				this.selectMaidList[i].maid.transform.localPosition = data.positions[i];
			}
		}
	}

	private List<Maid> GetSortedActiveMaidList()
	{
		if (this.activeMaidList.Count <= 0)
		{
			return new List<Maid>();
		}
		if (this.placementWindow == null)
		{
			this.placementWindow = (base.mgr.GetWindow(PhotoWindowManager.WindowType.Placement) as PlacementWindow);
		}
		List<Maid> list = new List<Maid>();
		List<Maid> maid_list = this.placementWindow.maid_list;
		for (int i = 0; i < maid_list.Count; i++)
		{
			if (this.activeMaidList.Contains(maid_list[i]))
			{
				list.Add(maid_list[i]);
			}
		}
		return list;
	}

	[HideInInspector]
	public static PhotoAlignmentPreset.Data SelectData;

	[SerializeField]
	private UIGrid maidListGrid;

	[SerializeField]
	private UIWFSwitchPanel switchPanel;

	[SerializeField]
	private UIGrid contentGrid;

	private MaidPlate[] selectMaidList = new MaidPlate[5];

	private PhotoNoCharaActive noCharaActive;

	private HashSet<Maid> activeMaidList = new HashSet<Maid>();

	private PlacementWindow placementWindow;
}
