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

public class AttachPointSubWindow : BasePhotoSubWindow
{
	public void Awake()
	{
		if (this.popupMaidList.onChangePopUpListValue.Count != 0)
		{
			return;
		}
		this.checkboxTransReset.check = true;
		if (this.popupMaidList.popup_value_list == null || this.popupMaidList.popup_value_list.Count == 0)
		{
			this.popupMaidList.popup_value_list = this.CreatePopupList();
		}
		Action<WFCheckBox> item = delegate(WFCheckBox checkbox)
		{
			foreach (AttachPointSubWindow.AttachPointCheckBoxs clickCheckBoxData in this.checkBoxs)
			{
				if (clickCheckBoxData.checkBox == checkbox)
				{
					this.OnClickAttachCheckBox(clickCheckBoxData);
					return;
				}
			}
		};
		foreach (AttachPointSubWindow.AttachPointCheckBoxs attachPointCheckBoxs in this.checkBoxs)
		{
			attachPointCheckBoxs.checkBox.check = false;
			attachPointCheckBoxs.checkBox.onClick.Add(item);
		}
		this.popupMaidList.onChangePopUpListValue.Add(new Action<KeyValuePair<string, UnityEngine.Object>>(this.OnSelectPopupList));
	}

	public override void Initizalize(BasePhotoWindow parentWindow)
	{
		this.Awake();
		base.Initizalize(parentWindow);
		this.popupMaidList.SetPopupValue(this.popupMaidList.popup_value_list[0].Key);
	}

	public void OnSelectPopupList(KeyValuePair<string, UnityEngine.Object> selectedItem)
	{
		if (this.selectItem == null || this.selectItem.obj == null || selectedItem.Value == null || this.selectItem.type == PhotoTransTargetObject.Type.BG)
		{
			this.stateText = "設定不可";
			if (this.selectItem != null && this.selectItem.obj != null)
			{
				this.selectItem.attachi_point = PhotoTransTargetObject.AttachPoint.Null;
				string stateText = this.selectItem.Attach(this.selectItem.attachi_point, !this.checkboxTransReset.check, null);
				if (this.selectItem.type != PhotoTransTargetObject.Type.BG)
				{
					this.stateText = stateText;
				}
				if (this.onStateTextChangeEvent != null)
				{
					this.onStateTextChangeEvent(this.stateText);
				}
			}
			foreach (AttachPointSubWindow.AttachPointCheckBoxs attachPointCheckBoxs in this.checkBoxs)
			{
				WFCheckBox checkBox = attachPointCheckBoxs.checkBox;
				bool flag = false;
				attachPointCheckBoxs.checkBox.enabled = flag;
				checkBox.check = flag;
			}
			return;
		}
		if (this.selectItem.attachi_point == PhotoTransTargetObject.AttachPoint.Null)
		{
			this.stateText = "設定なし";
			if (this.onStateTextChangeEvent != null)
			{
				this.onStateTextChangeEvent(this.stateText);
			}
		}
		foreach (AttachPointSubWindow.AttachPointCheckBoxs clickCheckBoxData in this.checkBoxs)
		{
			clickCheckBoxData.checkBox.enabled = true;
			clickCheckBoxData.checkBox.check = (clickCheckBoxData.attachType == this.selectItem.attachi_point);
			if (clickCheckBoxData.checkBox.check)
			{
				this.OnClickAttachCheckBox(clickCheckBoxData);
			}
		}
	}

	public void OnSelectItem(PhotoTransTargetObject select)
	{
		this.selectItem = select;
		if (select.attachi_point == PhotoTransTargetObject.AttachPoint.Null)
		{
			this.popupMaidList.SetPopupValue(this.popupMaidList.popup_value_list[0].Key);
		}
		else
		{
			string text = string.Empty;
			List<KeyValuePair<string, UnityEngine.Object>> popup_value_list = this.popupMaidList.popup_value_list;
			for (int i = 0; i < popup_value_list.Count; i++)
			{
				if (popup_value_list[i].Value != null && (popup_value_list[i].Value as Maid).status.guid == select.attach_maid_guid)
				{
					text = popup_value_list[i].Key;
					break;
				}
			}
			if (!string.IsNullOrEmpty(text))
			{
				this.popupMaidList.SetPopupValue(text);
			}
			else
			{
				this.popupMaidList.SetPopupValue(this.popupMaidList.popup_value_list[0].Key);
			}
		}
	}

	public void OnClickAttachCheckBox(AttachPointSubWindow.AttachPointCheckBoxs clickCheckBoxData)
	{
		if (this.selectItem == null || this.popupMaidList.popup_select_value.Value == null)
		{
			return;
		}
		foreach (AttachPointSubWindow.AttachPointCheckBoxs attachPointCheckBoxs in this.checkBoxs)
		{
			if (attachPointCheckBoxs.checkBox != clickCheckBoxData.checkBox)
			{
				attachPointCheckBoxs.checkBox.check = false;
			}
		}
		if (!clickCheckBoxData.checkBox.check)
		{
			this.selectItem.attachi_point = PhotoTransTargetObject.AttachPoint.Null;
			this.stateText = this.selectItem.Attach(this.selectItem.attachi_point, !this.checkboxTransReset.check, null);
			if (this.onStateTextChangeEvent != null)
			{
				this.onStateTextChangeEvent(this.stateText);
			}
			return;
		}
		this.selectItem.attachi_point = clickCheckBoxData.attachType;
		this.stateText = this.selectItem.Attach(this.selectItem.attachi_point, !this.checkboxTransReset.check, this.popupMaidList.popup_select_value.Value as Maid);
		if (this.onStateTextChangeEvent != null)
		{
			this.onStateTextChangeEvent(this.stateText);
		}
	}

	public void OnMaidAddEvent(Maid maid, bool is_deserialize_load)
	{
		if (maid == null || maid.boMAN)
		{
			return;
		}
		if (this.maidList == null)
		{
			PlacementWindow placementWindow = (this.parent_window_.mgr as PhotoWindowManager).GetWindow(PhotoWindowManager.WindowType.Placement) as PlacementWindow;
			this.maidList = placementWindow.maid_list;
		}
		this.activeMaidList.Add(maid);
		bool flag = this.popupMaidList.popup_select_value.Value == null;
		this.popupMaidList.popup_value_list = this.CreatePopupList();
		if (flag)
		{
			this.popupMaidList.SetPopupValue(this.popupMaidList.popup_value_list[0].Key);
		}
	}

	public void OnMaidRemoveEventPrev(Maid maid)
	{
		if (maid == null || maid.boMAN)
		{
			return;
		}
		Maid y = (!(this.popupMaidList.popup_select_value.Value == null)) ? (this.popupMaidList.popup_select_value.Value as Maid) : null;
		List<KeyValuePair<string, UnityEngine.Object>> popup_value_list = this.popupMaidList.popup_value_list;
		int num = -1;
		int num2 = 0;
		while (num2 < popup_value_list.Count && num == -1)
		{
			if (popup_value_list[num2].Value == y)
			{
				num = num2;
			}
			num2++;
		}
		this.activeMaidList.Remove(maid);
		List<KeyValuePair<string, UnityEngine.Object>> list = this.CreatePopupList();
		for (int i = 0; i < list.Count; i++)
		{
			Maid x = (!(list[i].Value == null)) ? (list[i].Value as Maid) : null;
			if (x == maid)
			{
				list.RemoveAt(i);
				break;
			}
		}
		this.popupMaidList.popup_value_list = list;
		if (0 <= num && num < list.Count)
		{
			this.popupMaidList.SetPopupValue(list[num].Key);
		}
		else
		{
			this.popupMaidList.SetPopupValue(list[0].Key);
		}
	}

	public void Serialize(ref Dictionary<string, string> serializeData)
	{
		serializeData.Add("trans_reset", this.checkboxTransReset.check.ToString());
	}

	public void Deserialize(Dictionary<string, string> deserializeData)
	{
		if (deserializeData.ContainsKey("trans_reset"))
		{
			bool check = bool.Parse(deserializeData["trans_reset"]);
			this.checkboxTransReset.check = check;
		}
	}

	private List<KeyValuePair<string, UnityEngine.Object>> CreatePopupList()
	{
		List<KeyValuePair<string, UnityEngine.Object>> list = new List<KeyValuePair<string, UnityEngine.Object>>();
		list.Add(new KeyValuePair<string, UnityEngine.Object>("アタッチポイントの指定なし", null));
		if (this.maidList == null || this.activeMaidList.Count <= 0)
		{
			return list;
		}
		list.Clear();
		HashSet<string> hashSet = new HashSet<string>();
		for (int i = 0; i < this.maidList.Count; i++)
		{
			if (this.activeMaidList.Contains(this.maidList[i]))
			{
				string text = this.maidList[i].status.lastName + " " + this.maidList[i].status.firstName;
				string text2 = text;
				if (hashSet.Contains(text2))
				{
					int num = 2;
					do
					{
						text2 = string.Concat(new object[]
						{
							text,
							"(",
							num,
							")"
						});
						num++;
					}
					while (hashSet.Contains(text2));
				}
				hashSet.Add(text2);
				list.Add(new KeyValuePair<string, UnityEngine.Object>(text2, this.maidList[i]));
			}
		}
		return list;
	}

	public string stateText { get; private set; }

	public int activeMaidListCount
	{
		get
		{
			return this.activeMaidList.Count;
		}
	}

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

	[SerializeField]
	public WFCheckBox checkboxTransReset;

	[SerializeField]
	public WindowPartsPopUpList popupMaidList;

	[SerializeField]
	public AttachPointSubWindow.AttachPointCheckBoxs[] checkBoxs;

	[NonSerialized]
	public Action<string> onStateTextChangeEvent;

	[NonSerialized]
	public HashSet<Maid> activeMaidList = new HashSet<Maid>();

	private PhotoTransTargetObject selectItem;

	private List<Maid> maidList;

	[Serializable]
	public struct AttachPointCheckBoxs
	{
		[SerializeField]
		public PhotoTransTargetObject.AttachPoint attachType;

		[SerializeField]
		public WFCheckBox checkBox;
	}
}
