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

namespace SceneEditWindow
{
	[AddComponentMenu("SceneEditWindow/CustomPartsWindow")]
	public class CustomPartsWindow : BasePhotoWindow
	{
		public override string windowName
		{
			get
			{
				return "CustomPartsWindow";
			}
		}

		public override void Start()
		{
			base.Start();
			this.subWindowVisibleCheckBox.check = false;
			this.subWindowVisibleCheckBox.onClick.Add(new Action<WFCheckBox>(this.OnClickOpenSubWindow));
			this.checkBoxCustomEnabled.onClick.Add(new Action<WFCheckBox>(this.OnClickCustomEnabled));
			this.subWindowVisibleCheckBox.visible = false;
			this.enabledMpns = new HashSet<MPN>();
			foreach (string value in this.customEnabledMpnNames)
			{
				MPN item = MPN.null_mpn;
				try
				{
					item = (MPN)Enum.Parse(typeof(MPN), value);
				}
				catch (Exception e)
				{
					NDebug.AssertParseError("MPN", e);
				}
				this.enabledMpns.Add(item);
			}
			this.partsIndexTabPanel.UpdateChildren();
			foreach (UIWFTabButton uiwftabButton in this.partsIndexTabPanel.button_list)
			{
				EventDelegate.Add(uiwftabButton.onSelect, new EventDelegate.Callback(this.OnSelectIndexButton));
			}
			this.visibleTabPanel.UpdateChildren();
			foreach (UIWFTabButton uiwftabButton2 in this.visibleTabPanel.button_list)
			{
				EventDelegate.Add(uiwftabButton2.onSelect, new EventDelegate.Callback(this.OnSelectVisibleButton));
			}
			this.visibleTabPanel.Select(this.visibleTabPanel.button_list[0]);
			this.scaleSlider.onChangeValue.Clear();
			this.scaleSlider.onChangeValue.Add(new Action<float>(this.onChangeScaleSlider));
			this.UpdateChildren();
		}

		public void Initizalize(Maid maid)
		{
			if (maid == null || maid.body0 == null || maid.body0.m_Bones == null)
			{
				NDebug.Assert("メイドのボディ読み込みが終了していないのにInitizalizeが呼ばれています", false);
				return;
			}
			this.subWindowVisibleCheckBox.check = false;
			this.maid = maid;
			this.animation = maid.body0.m_Bones.GetComponent<Animation>();
			this.visible = false;
		}

		private void OnDestroy()
		{
			GameObject worldTransformAxisParent = PhotoWindowManager.GetWorldTransformAxisParent();
			if (worldTransformAxisParent != null)
			{
				UnityEngine.Object.DestroyImmediate(worldTransformAxisParent);
			}
		}

		public void DestroySelectCustomParts()
		{
			if (this.selectCustomParts == null)
			{
				return;
			}
			foreach (CustomParts customParts in this.selectCustomParts)
			{
				customParts.Destroy();
			}
			this.selectCustomParts = null;
		}

		public bool SetItem(SceneEdit.SMenuItem menuItem)
		{
			this.reseaveLoadMenuItem = null;
			if (menuItem == null || menuItem.m_boDelOnly || !this.enabledMpns.Contains(menuItem.m_mpn))
			{
				return false;
			}
			this.reseaveLoadMenuItem = menuItem;
			return true;
		}

		public void ResetPoint(MPN mpn)
		{
			List<KeyValuePair<TBody.SlotID, string>> attachPointListFromMPN = this.maid.body0.GetAttachPointListFromMPN(mpn);
			for (int i = 0; i < attachPointListFromMPN.Count; i++)
			{
				CustomParts customParts = new CustomParts(this.maid, attachPointListFromMPN[i].Key, attachPointListFromMPN[i].Value);
				customParts.enabled = false;
			}
		}

		public void ResetAllPoint()
		{
			foreach (MPN mpn in this.enabledMpns)
			{
				this.ResetPoint(mpn);
			}
		}

		public new void Update()
		{
			if (this.reseaveLoadMenuItem != null && !GameMain.Instance.CharacterMgr.IsBusy())
			{
				this.CreateItemData(this.reseaveLoadMenuItem);
				this.reseaveLoadMenuItem = null;
			}
			if (this.selectCustomParts == null || this.selectCustomParts.Length == 0 || !this.editMode)
			{
				return;
			}
			foreach (CustomParts customParts in this.selectCustomParts)
			{
				if (customParts.enabled)
				{
					Transform transform = customParts.indirectObject.transform;
					customParts.SetTransformData(transform, true);
				}
			}
		}

		public void UpdateUI()
		{
			if (this.selectCustomParts != null)
			{
				foreach (CustomParts customParts in this.selectCustomParts)
				{
					customParts.tranformAxis.Visible = false;
					customParts.gizmoRender.Visible = false;
				}
			}
			if (!this.editMode)
			{
				return;
			}
			CustomParts customParts2 = this.selectCustomParts[this.selectCustomPartsIndex];
			this.checkBoxCustomEnabled.check = customParts2.enabled;
			Vector3 position;
			Quaternion rotation;
			Vector3 localScale;
			customParts2.GetTransformData(out position, out rotation, out localScale, true);
			customParts2.indirectObject.transform.position = position;
			customParts2.indirectObject.transform.rotation = rotation;
			customParts2.indirectObject.transform.localScale = localScale;
			customParts2.tranformAxis.Visible = (customParts2.enabled && this.axisVisibleButton.isSelected);
			customParts2.gizmoRender.Visible = (customParts2.enabled && this.rotateVisibleButton.isSelected);
			this.scaleSlider.value = customParts2.scale.x;
			this.editToolObject.SetActive(customParts2.enabled);
		}

		public void ViewMode(bool enabled)
		{
			if (this.selectCustomParts == null || this.selectCustomParts.Length == 0 || !this.editMode)
			{
				return;
			}
			if (this.selectCustomParts != null)
			{
				foreach (CustomParts customParts in this.selectCustomParts)
				{
					customParts.tranformAxis.Visible = false;
					customParts.gizmoRender.Visible = false;
				}
			}
			if (enabled)
			{
				return;
			}
			this.UpdateUI();
		}

		public void OnExecItemPrev(SceneEdit.SMenuItem menuItem)
		{
			this.isBackupDatas = null;
			if (this.reseaveLoadMenuItem != null && !GameMain.Instance.CharacterMgr.IsBusy())
			{
				this.CreateItemData(this.reseaveLoadMenuItem);
				this.reseaveLoadMenuItem = null;
			}
			this.DestroySelectCustomParts();
			if (menuItem.m_mpn == MPN.hairf || menuItem.m_mpn == MPN.hairt)
			{
				MPN f_mpn = (menuItem.m_mpn != MPN.hairf) ? MPN.acckamisub : MPN.acckami;
				List<KeyValuePair<TBody.SlotID, string>> attachPointListFromMPN = this.maid.body0.GetAttachPointListFromMPN(f_mpn);
				for (int i = 0; i < attachPointListFromMPN.Count; i++)
				{
					new BaseCustomParts(this.maid, attachPointListFromMPN[i].Key, attachPointListFromMPN[i].Value)
					{
						enabled = false
					}.Reset();
				}
				this.visible = false;
			}
			else if (this.enabledMpns.Contains(menuItem.m_mpn))
			{
				MaidProp prop = this.maid.GetProp(menuItem.m_mpn);
				int nFileNameRID = prop.nFileNameRID;
				bool flag = menuItem.m_nMenuFileRID == nFileNameRID;
				if (menuItem.m_nMenuFileRID != nFileNameRID && !menuItem.m_boDelOnly)
				{
					List<SceneEdit.SMenuItem> list = null;
					if (menuItem.m_bGroupLeader)
					{
						list = menuItem.m_listMember;
					}
					else if (menuItem.m_leaderMenu != null)
					{
						list = menuItem.m_leaderMenu.m_listMember;
					}
					if (list != null)
					{
						foreach (SceneEdit.SMenuItem smenuItem in list)
						{
							if (smenuItem.m_nMenuFileRID == nFileNameRID)
							{
								flag = true;
								break;
							}
						}
					}
				}
				List<CustomParts> list2 = new List<CustomParts>();
				List<KeyValuePair<TBody.SlotID, string>> attachPointListFromMPN2 = this.maid.body0.GetAttachPointListFromMPN(menuItem.m_mpn);
				for (int j = 0; j < attachPointListFromMPN2.Count; j++)
				{
					list2.Add(new CustomParts(this.maid, attachPointListFromMPN2[j].Key, attachPointListFromMPN2[j].Value));
				}
				if (flag)
				{
					this.isBackupDatas = new bool[list2.Count];
					for (int k = 0; k < list2.Count; k++)
					{
						CustomParts customParts = list2[k];
						this.isBackupDatas[k] = customParts.enabled;
						if (customParts.enabled)
						{
							customParts.SaveBackup();
						}
					}
				}
				foreach (CustomParts customParts2 in list2)
				{
					customParts2.Reset();
					customParts2.enabled = false;
				}
			}
			else
			{
				this.visible = false;
			}
		}

		public void CreateItemData(SceneEdit.SMenuItem menu)
		{
			this.DestroySelectCustomParts();
			this.selectCustomPartsIndex = 0;
			List<KeyValuePair<TBody.SlotID, string>> attachPointListFromMPN = this.maid.body0.GetAttachPointListFromMPN(menu.m_mpn);
			if (attachPointListFromMPN.Count != 0)
			{
				this.selectCustomParts = new CustomParts[attachPointListFromMPN.Count];
			}
			for (int i = 0; i < attachPointListFromMPN.Count; i++)
			{
				CustomParts customParts = new CustomParts(this.maid, attachPointListFromMPN[i].Key, attachPointListFromMPN[i].Value);
				customParts.indirectObject = GameObject.Find(menu.m_nMenuFileRID.ToString() + "_" + i.ToString());
				if (customParts.indirectObject == null)
				{
					customParts.indirectObject = new GameObject();
					customParts.indirectObject.name = menu.m_nMenuFileRID.ToString();
				}
				if (this.isBackupDatas != null && this.isBackupDatas.Length == attachPointListFromMPN.Count && this.isBackupDatas[i])
				{
					customParts.enabled = true;
					customParts.LoadBackup();
				}
				Vector3 position;
				Quaternion rotation;
				Vector3 localScale;
				customParts.GetTransformData(out position, out rotation, out localScale, true);
				customParts.indirectObject.transform.position = position;
				customParts.indirectObject.transform.rotation = rotation;
				customParts.indirectObject.transform.localScale = localScale;
				customParts.menuItem = menu;
				customParts.tranformAxis = PhotoWindowManager.CreateWorldTransformAxis(customParts.indirectObject, false, true);
				customParts.tranformAxis.Visible = false;
				customParts.tranformAxis.ScaleMinMax = new Vector2(this.scaleSlider.MinNum, this.scaleSlider.MaxNum);
				customParts.gizmoRender = PhotoWindowManager.CreateWorldTransformRotate(customParts.indirectObject, false);
				customParts.gizmoRender.offsetScale = 0.4f;
				customParts.gizmoRender.Visible = false;
				this.checkBoxCustomEnabled.check = customParts.enabled;
				this.selectCustomParts[i] = customParts;
			}
			this.isBackupDatas = null;
		}

		public void OnSelectIndexButton()
		{
			if (!UIWFSelectButton.current.isSelected)
			{
				return;
			}
			this.selectCustomPartsIndex = 0;
			UILabel componentInChildren = UIWFSelectButton.current.transform.parent.GetComponentInChildren<UILabel>(true);
			if (componentInChildren == null)
			{
				this.selectCustomPartsIndex = 0;
			}
			else
			{
				this.selectCustomPartsIndex = int.Parse(componentInChildren.text) - 1;
			}
			this.UpdateUI();
		}

		public void OnSelectVisibleButton()
		{
			this.UpdateUI();
		}

		public void onChangeScaleSlider(float value)
		{
			if (!this.editMode || this.selectCustomParts == null || this.selectCustomParts.Length == 0)
			{
				return;
			}
			CustomParts customParts = this.selectCustomParts[this.selectCustomPartsIndex];
			customParts.indirectObject.transform.localScale = new Vector3(value, value, value);
		}

		protected void OnClickOpenSubWindow(WFCheckBox checkBox)
		{
			if (GameMain.Instance.CharacterMgr.IsBusy() || this.selectCustomParts == null || this.selectCustomParts.Length <= 0)
			{
				this.editMode = false;
				return;
			}
			this.editMode = checkBox.check;
			this.partsIndexTabPanel.Select(this.partsIndexTabPanel.button_list[this.selectCustomPartsIndex]);
			for (int i = 0; i < this.partsIndexTabPanel.transform.childCount; i++)
			{
				this.partsIndexTabPanel.transform.GetChild(i).gameObject.SetActive(i < this.selectCustomParts.Length);
			}
			this.OnClickCustomEnabled(this.checkBoxCustomEnabled);
		}

		protected void OnClickCustomEnabled(WFCheckBox checkBox)
		{
			if (!this.editMode)
			{
				return;
			}
			CustomParts customParts = this.selectCustomParts[this.selectCustomPartsIndex];
			customParts.enabled = checkBox.check;
			this.UpdateUI();
		}

		public bool playMaidAnimation
		{
			get
			{
				return !(this.animation == null) && !this.maid.GetLockHeadAndEye();
			}
			set
			{
				if (this.playMaidAnimation == value || this.animation == null)
				{
					return;
				}
				AnimationState animationState = this.animation[this.maid.body0.LastAnimeFN.ToLower()];
				if (animationState == null)
				{
					Debug.LogError(this.maid.body0.LastAnimeFN.ToLower() + "のAnimationStateが見つかりませんでした");
					return;
				}
				animationState.enabled = value;
				this.maid.LockHeadAndEye(!value);
				if (GameMain.Instance.VRMode)
				{
					if (GameMain.Instance.OvrMgr.ovr_obj.left_controller != null && GameMain.Instance.OvrMgr.ovr_obj.left_controller.grip_collider != null)
					{
						GameMain.Instance.OvrMgr.ovr_obj.left_controller.grip_collider.ResetGrip();
					}
					if (GameMain.Instance.OvrMgr.ovr_obj.right_controller != null && GameMain.Instance.OvrMgr.ovr_obj.right_controller.grip_collider != null)
					{
						GameMain.Instance.OvrMgr.ovr_obj.right_controller.grip_collider.ResetGrip();
					}
					MaidColliderCollect component = this.maid.GetComponent<MaidColliderCollect>();
					if (component != null)
					{
						List<CapsuleCollider> collider = component.GetCollider(MaidColliderCollect.ColliderType.Grab);
						foreach (CapsuleCollider capsuleCollider in collider)
						{
							capsuleCollider.gameObject.SetActive(value);
						}
					}
				}
			}
		}

		public override bool visible
		{
			get
			{
				return base.visible;
			}
			set
			{
				base.visible = value;
				this.subWindowVisibleCheckBox.check = value;
				this.OnClickOpenSubWindow(this.subWindowVisibleCheckBox);
			}
		}

		public bool editMode
		{
			get
			{
				return this.subWindowVisibleCheckBox.check;
			}
			set
			{
				if (this.selectCustomParts == null)
				{
					value = false;
				}
				this.subWindowVisibleCheckBox.check = value;
				this.playMaidAnimation = !value;
				if (this.selectCustomParts != null && this.selectCustomParts.Length != 0)
				{
					this.UpdateUI();
				}
			}
		}

		[SerializeField]
		private WFCheckBox subWindowVisibleCheckBox;

		[SerializeField]
		private WFCheckBox checkBoxCustomEnabled;

		[SerializeField]
		private GameObject editToolObject;

		[SerializeField]
		private UIWFTabPanel partsIndexTabPanel;

		[SerializeField]
		private UIWFTabPanel visibleTabPanel;

		[SerializeField]
		private UIWFTabButton axisVisibleButton;

		[SerializeField]
		private UIWFTabButton rotateVisibleButton;

		[SerializeField]
		private PhotoSliderAndInput scaleSlider;

		[SerializeField]
		private string[] customEnabledMpnNames;

		private Maid maid;

		private Animation animation;

		private SceneEdit.SMenuItem reseaveLoadMenuItem;

		private bool[] isBackupDatas;

		private CustomParts[] selectCustomParts;

		private int selectCustomPartsIndex;

		private HashSet<MPN> enabledMpns;
	}
}
