using System;
using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using Kasizuki;
using UnityEngine;

public class PhotoFaceDataShortcutSetter : MonoBehaviour
{
	public float fadeSpeed
	{
		get
		{
			return this.m_FadeSpeed;
		}
		set
		{
			this.m_FadeSpeed = Mathf.Clamp(value, 0.001f, 1f);
		}
	}

	private PhotoFaceDataShortcut targetControllerRight { get; set; }

	private PhotoFaceDataShortcut targetControllerLeft { get; set; }

	private PhotoFaceDataShortcut.ItemData nowSelectItem
	{
		get
		{
			return this.m_NowSelectItem;
		}
		set
		{
			this.m_NowSelectItem = value;
		}
	}

	public void Init(PhotoFaceDataShortcut rightHandUI, PhotoFaceDataShortcut LeftHandUI)
	{
		this.m_FadeWindowShortcutButtonsRight = this.m_ButtonListShortcutRight.GetComponent<NGUIWindow>();
		this.m_FadeWindowShortcutButtonsLeft = this.m_ButtonListShortcutLeft.GetComponent<NGUIWindow>();
		this.m_FadeWindowFaceDataList = this.m_WindowListFaceData.GetComponent<NGUIWindow>();
		this.targetControllerRight = rightHandUI;
		this.targetControllerLeft = LeftHandUI;
		PhotoFaceData.Create();
		List<PhotoFaceData> data2 = PhotoFaceData.data;
		ListViewerWindow windowListFaceData = this.m_WindowListFaceData;
		windowListFaceData.Show<PhotoFaceData, UIWFTabButton>(data2, delegate(int index, PhotoFaceData data, UIWFTabButton item)
		{
			UILabel componentInChildren = item.GetComponentInChildren<UILabel>();
			componentInChildren.text = data.name;
			componentInChildren.GetComponent<Localize>().SetTerm(data.termName);
			EventDelegate.Add(item.onClick, delegate()
			{
				this.OnClickData(data);
			});
		});
		UIWFTabPanel component = windowListFaceData.viewer.parentItemArea.GetComponent<UIWFTabPanel>();
		if (component)
		{
			component.UpdateChildren();
		}
		this.m_ButtonListShortcutRight.Show<Transform>(this.targetControllerRight.itemDataList.Count, delegate(int index, Transform trans)
		{
			PhotoFaceDataShortcut.ItemData data = this.targetControllerRight.itemDataList[index];
			UILabel componentInChildren = trans.GetComponentInChildren<UILabel>();
			UIButton componentInChildren2 = trans.GetComponentInChildren<UIButton>();
			Localize componentInChildren3 = trans.GetComponentInChildren<Localize>();
			componentInChildren.text = data.GetDataName();
			componentInChildren3.SetTerm(data.GetDataTermName());
			EventDelegate.Add(componentInChildren2.onClick, delegate()
			{
				this.OnClickShortcutButton(data);
			});
			PhotoFaceDataShortcutSetter.ItemData itemData = trans.gameObject.AddComponent<PhotoFaceDataShortcutSetter.ItemData>();
			itemData.label = componentInChildren;
			itemData.localize = componentInChildren3;
			this.m_ItemPairDic.Add(data, itemData);
		});
		this.m_ButtonListShortcutLeft.Show<Transform>(this.targetControllerLeft.itemDataList.Count, delegate(int index, Transform trans)
		{
			PhotoFaceDataShortcut.ItemData data = this.targetControllerLeft.itemDataList[index];
			UILabel componentInChildren = trans.GetComponentInChildren<UILabel>();
			UIButton componentInChildren2 = trans.GetComponentInChildren<UIButton>();
			Localize componentInChildren3 = trans.GetComponentInChildren<Localize>();
			componentInChildren.text = data.GetDataName();
			componentInChildren3.SetTerm(data.GetDataTermName());
			EventDelegate.Add(componentInChildren2.onClick, delegate()
			{
				this.OnClickShortcutButton(data);
			});
			PhotoFaceDataShortcutSetter.ItemData itemData = trans.gameObject.AddComponent<PhotoFaceDataShortcutSetter.ItemData>();
			itemData.label = componentInChildren;
			itemData.localize = componentInChildren3;
			this.m_ItemPairDic.Add(data, itemData);
		});
		Transform transform = this.m_ParentAddFaceOptions.transform.Find("Buttons");
		for (int i = 0; i < transform.childCount; i++)
		{
			WFCheckBox component2 = transform.GetChild(i).GetComponent<WFCheckBox>();
			component2.check = false;
			component2.enabled = true;
			this.m_AddFaceOptionDic.Add(component2.GetComponentInChildren<UILabel>().text, component2);
			component2.onClick.Add(new Action<WFCheckBox>(this.OnClickCheckBoxAddOption));
		}
		EventDelegate.Add(this.m_ButtonFaceListOK.onClick, new EventDelegate.Callback(this.OnClickFaceListOK));
		NGUIWindow fadeWindowFaceDataList = this.m_FadeWindowFaceDataList;
		fadeWindowFaceDataList.OnOpen = (Action)Delegate.Combine(fadeWindowFaceDataList.OnOpen, new Action(delegate()
		{
			this.m_WindowListFaceData.Reposition();
		}));
	}

	private void OnClickData(PhotoFaceData data)
	{
		TMorph.AddBlendType blendType = this.ConvertBlendType();
		this.ApplyItemData(this.nowSelectItem, data, blendType);
		PhotoFaceDataShortcutSetter.ItemData itemData;
		if (this.m_ItemPairDic != null && this.m_ItemPairDic.TryGetValue(this.nowSelectItem, out itemData))
		{
			itemData.label.text = this.nowSelectItem.GetDataName();
			itemData.localize.SetTerm(this.nowSelectItem.GetDataTermName());
		}
		if (this.nowSelectItem != null)
		{
			if (this.targetControllerRight.itemDataList.Contains(this.nowSelectItem))
			{
				this.targetControllerRight.Emulate(this.nowSelectItem);
			}
			else if (this.targetControllerLeft.itemDataList.Contains(this.nowSelectItem))
			{
				this.targetControllerLeft.Emulate(this.nowSelectItem);
			}
			else
			{
				Debug.LogWarning("コントローラに存在しない項目を更新しているかも");
			}
		}
	}

	private void OnClickShortcutButton(PhotoFaceDataShortcut.ItemData item)
	{
		this.nowSelectItem = item;
		this.m_FadeWindowShortcutButtonsRight.Close(this.fadeSpeed, null);
		this.m_FadeWindowShortcutButtonsLeft.Close(this.fadeSpeed, null);
		this.WaitTime(this.fadeSpeed, delegate
		{
			this.m_FadeWindowFaceDataList.Open(this.fadeSpeed, null);
		});
		UIWFTabPanel component = this.m_WindowListFaceData.viewer.parentItemArea.GetComponent<UIWFTabPanel>();
		UIWFTabButton selectButtonObject = component.GetSelectButtonObject();
		if (selectButtonObject != null)
		{
			selectButtonObject.SetSelect(false);
		}
		component.ResetSelect();
		component.UpdateChildren();
		this.UpdateCheckBoxAddOption(item);
	}

	private void OnClickFaceListOK()
	{
		this.m_FadeWindowFaceDataList.Close(this.fadeSpeed, null);
		this.WaitTime(this.fadeSpeed, delegate
		{
			this.m_FadeWindowShortcutButtonsRight.Open(this.fadeSpeed, null);
			this.m_FadeWindowShortcutButtonsLeft.Open(this.fadeSpeed, null);
		});
		ControllerShortcutSettingData.config.Write();
	}

	private void ApplyItemData(PhotoFaceDataShortcut.ItemData itemData, PhotoFaceData data, TMorph.AddBlendType blendType)
	{
		if (itemData == null)
		{
			Debug.Log("コントローラの項目の参照がありませんでした");
			return;
		}
		if (data == null)
		{
			Debug.Log("表情データがnullらしい");
		}
		itemData.data = data;
		itemData.blendType = blendType;
		itemData.isOpenMouth = this.IsOpenMouth();
	}

	private TMorph.AddBlendType ConvertBlendType()
	{
		TMorph.AddBlendType addBlendType = TMorph.AddBlendType.None;
		Dictionary<string, TMorph.AddBlendType> dictionary = new Dictionary<string, TMorph.AddBlendType>
		{
			{
				"頬染め1",
				TMorph.AddBlendType.Cheek1
			},
			{
				"頬染め2",
				TMorph.AddBlendType.Cheek2
			},
			{
				"頬染め3",
				TMorph.AddBlendType.Cheek3
			},
			{
				"涙1",
				TMorph.AddBlendType.Tear1
			},
			{
				"涙2",
				TMorph.AddBlendType.Tear2
			},
			{
				"涙3",
				TMorph.AddBlendType.Tear3
			},
			{
				"涙(玉)",
				TMorph.AddBlendType.TearBig
			},
			{
				"涎",
				TMorph.AddBlendType.Yodare
			},
			{
				"赤面",
				TMorph.AddBlendType.Blush
			},
			{
				"ショック",
				TMorph.AddBlendType.Shock
			}
		};
		foreach (KeyValuePair<string, TMorph.AddBlendType> keyValuePair in dictionary)
		{
			if (this.m_AddFaceOptionDic.ContainsKey(keyValuePair.Key) && this.m_AddFaceOptionDic[keyValuePair.Key].check)
			{
				addBlendType |= keyValuePair.Value;
			}
		}
		return addBlendType;
	}

	private bool IsOpenMouth()
	{
		return this.m_AddFaceOptionDic["口を開く"].check;
	}

	private void UpdateCheckBoxAddOption(PhotoFaceDataShortcut.ItemData itemData)
	{
		if (itemData == null || itemData.data == null)
		{
			Debug.Log("表情データが空だったので、追加表情データの表示は最後の状態を引き継ぐ");
			return;
		}
		Dictionary<string, TMorph.AddBlendType> dictionary = new Dictionary<string, TMorph.AddBlendType>
		{
			{
				"頬染め1",
				TMorph.AddBlendType.Cheek1
			},
			{
				"頬染め2",
				TMorph.AddBlendType.Cheek2
			},
			{
				"頬染め3",
				TMorph.AddBlendType.Cheek3
			},
			{
				"涙1",
				TMorph.AddBlendType.Tear1
			},
			{
				"涙2",
				TMorph.AddBlendType.Tear2
			},
			{
				"涙3",
				TMorph.AddBlendType.Tear3
			},
			{
				"涙(玉)",
				TMorph.AddBlendType.TearBig
			},
			{
				"涎",
				TMorph.AddBlendType.Yodare
			},
			{
				"赤面",
				TMorph.AddBlendType.Blush
			},
			{
				"ショック",
				TMorph.AddBlendType.Shock
			}
		};
		foreach (KeyValuePair<string, TMorph.AddBlendType> keyValuePair in dictionary)
		{
			this.m_AddFaceOptionDic[keyValuePair.Key].check = ((itemData.blendType & keyValuePair.Value) != TMorph.AddBlendType.None);
		}
	}

	private void OnClickCheckBoxAddOption(WFCheckBox check_box)
	{
		string[] array = new string[]
		{
			"頬染め1",
			"頬染め2",
			"頬染め3"
		};
		string[] array2 = new string[]
		{
			"涙1",
			"涙2",
			"涙3"
		};
		string[][] array3 = new string[][]
		{
			array,
			array2
		};
		foreach (KeyValuePair<string, WFCheckBox> keyValuePair in this.m_AddFaceOptionDic)
		{
			if (keyValuePair.Value == check_box)
			{
				foreach (string array4 in array3)
				{
					if (0 <= Array.IndexOf<string>(array4, keyValuePair.Key))
					{
						for (int j = 0; j < array4.Length; j++)
						{
							if (!(array4[j] == keyValuePair.Key))
							{
								if (this.m_AddFaceOptionDic[array4[j]].check)
								{
									this.m_AddFaceOptionDic[array4[j]].check = false;
								}
							}
						}
					}
				}
			}
		}
		if (this.nowSelectItem != null && this.nowSelectItem.data != null)
		{
			this.ApplyItemData(this.nowSelectItem, this.nowSelectItem.data, this.ConvertBlendType());
			if (this.nowSelectItem != null)
			{
				if (this.targetControllerRight.itemDataList.Contains(this.nowSelectItem))
				{
					this.targetControllerRight.Emulate(this.nowSelectItem);
				}
				else if (this.targetControllerLeft.itemDataList.Contains(this.nowSelectItem))
				{
					this.targetControllerLeft.Emulate(this.nowSelectItem);
				}
				else
				{
					Debug.LogWarning("コントローラに存在しない項目を更新しているかもしれない");
				}
			}
		}
	}

	private void WaitTime(float time, Action onFinish)
	{
		base.StartCoroutine(this.CoWaitTime(time, onFinish));
	}

	private IEnumerator CoWaitTime(float time, Action onFinish)
	{
		yield return new WaitForSeconds(time);
		if (onFinish != null)
		{
			onFinish();
		}
		yield break;
	}

	[SerializeField]
	[Range(0.001f, 1f)]
	private float m_FadeSpeed = 0.25f;

	[SerializeField]
	private CircleCommandUI m_ButtonListShortcutRight;

	[SerializeField]
	private CircleCommandUI m_ButtonListShortcutLeft;

	[SerializeField]
	private ListViewerWindow m_WindowListFaceData;

	[SerializeField]
	private GameObject m_ParentAddFaceOptions;

	[SerializeField]
	private UIButton m_ButtonFaceListOK;

	private NGUIWindow m_FadeWindowShortcutButtonsRight;

	private NGUIWindow m_FadeWindowShortcutButtonsLeft;

	private NGUIWindow m_FadeWindowFaceDataList;

	private PhotoFaceDataShortcut.ItemData m_NowSelectItem;

	private Dictionary<PhotoFaceDataShortcut.ItemData, PhotoFaceDataShortcutSetter.ItemData> m_ItemPairDic = new Dictionary<PhotoFaceDataShortcut.ItemData, PhotoFaceDataShortcutSetter.ItemData>();

	private Dictionary<string, WFCheckBox> m_AddFaceOptionDic = new Dictionary<string, WFCheckBox>();

	public class ItemData : MonoBehaviour
	{
		public UILabel label
		{
			get
			{
				return this.m_Label;
			}
			set
			{
				this.m_Label = value;
			}
		}

		public Localize localize
		{
			get
			{
				return this.m_Localize;
			}
			set
			{
				this.m_Localize = value;
			}
		}

		private UILabel m_Label;

		private Localize m_Localize;
	}
}
