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

public class GravityControlWindow : BaseMaidPhotoWindow
{
	public override void Awake()
	{
		base.Awake();
		this.selectGravityData = null;
		this.editSubWindow = this.subWindows[0];
		this.noCharaActive = base.GetComponentInChildren<PhotoNoCharaActive>();
		foreach (GravityControlWindow.GravityData gravityData in this.gravityDatas)
		{
			NDebug.Assert(!string.IsNullOrEmpty(gravityData.categoryName), "GravityControlWindow 初期化エラーcategoryNameが不正です");
			NDebug.Assert(0 < gravityData.slotIds.Length, "GravityControlWindow 初期化エラーslotIdsが不正です");
			EventDelegate.Add(gravityData.button.onClick, delegate()
			{
				for (int j = 0; j < this.gravityDatas.Length; j++)
				{
					if (this.gravityDatas[j].button == UIButton.current)
					{
						if (this.gravityDatas[j] == this.selectGravityData)
						{
							this.CloseEditSubWindow();
						}
						else
						{
							this.OnSelectCategoryButton(this.gravityDatas[j]);
						}
						break;
					}
				}
			});
		}
		this.checkBoxEnabledGravityControl.onClick.Add(delegate(WFCheckBox check_box)
		{
			if (base.mgr.select_maid == null || base.mgr.select_maid.boMAN || this.selectGravityData == null)
			{
				return;
			}
			GravityTransformControl gravityTransformControl = this.GetGravityTransformControl(base.mgr.select_maid, this.selectGravityData.categoryName);
			if (gravityTransformControl == null)
			{
				return;
			}
			gravityTransformControl.isEnabled = check_box.check;
		});
		this.UpdateChildren();
	}

	public override void Start()
	{
		base.Start();
		this.noCharaActive.ChangeMode(true, true);
		this.editSubWindow.SetButtonEnabled(BasePhotoSubWindow.BtnType.End, true, false);
		this.editSubWindow.GetButtonOnClickEventList(BasePhotoSubWindow.BtnType.End).Add(new Action(this.CloseEditSubWindow));
		this.editSubWindow.visible = false;
	}

	public override void OnMaidAddEvent(Maid maid, bool is_deserialize_load)
	{
		if (maid == null || maid.boMAN)
		{
			return;
		}
		Dictionary<string, GravityTransformControl> dictionary = new Dictionary<string, GravityTransformControl>();
		foreach (GravityControlWindow.GravityData gravityData in this.gravityDatas)
		{
			string text = "GravityDatas_" + maid.status.guid + "_" + gravityData.categoryName;
			Transform bone = maid.body0.GetBone("Bip01");
			Transform transform = bone.Find(text);
			if (transform == null)
			{
				GameObject gameObject = new GameObject();
				gameObject.name = text;
				gameObject.transform.SetParent(bone, false);
				gameObject.transform.SetParent(maid.transform, true);
				gameObject.transform.localScale = Vector3.one;
				gameObject.transform.rotation = Quaternion.identity;
				GameObject gameObject2 = new GameObject();
				gameObject2.transform.SetParent(gameObject.transform, false);
				gameObject2.name = text;
				transform = gameObject2.transform;
			}
			else
			{
				transform = transform.transform.GetChild(0);
			}
			GravityTransformControl gravityTransformControl = transform.gameObject.AddComponent<GravityTransformControl>();
			gravityTransformControl.transTargetObject = new PhotoTransTargetObject(transform.gameObject, text, PhotoTransTargetObject.Type.Prefab, Vector2.one);
			gravityTransformControl.SetTargetSlods(gravityData.slotIds);
			gravityTransformControl.forceRate = gravityData.forceRate;
			dictionary.Add(gravityData.categoryName, gravityTransformControl);
			Dictionary<string, string> maidStoreData = base.GetMaidStoreData(maid);
			string categoryName = gravityData.categoryName;
			if (gravityTransformControl.isValid)
			{
				if (maidStoreData.ContainsKey(categoryName))
				{
					gravityTransformControl.isEnabled = bool.Parse(maidStoreData[categoryName]);
					gravityTransformControl.transform.localPosition = Parse.Vector3(maidStoreData[categoryName + "pos"]);
				}
			}
			else if (maidStoreData != null && maidStoreData.ContainsKey(categoryName))
			{
				maidStoreData.Remove(categoryName);
				maidStoreData.Remove(categoryName + "pos");
			}
		}
		this.activeMaidControllerList.Add(maid.status.guid, dictionary);
		this.noCharaActive.ChangeMode(false, true);
	}

	public override void OnMaidRemoveEvent(Maid maid)
	{
		if (maid == null || maid.boMAN)
		{
			return;
		}
		foreach (GravityControlWindow.GravityData gravityData in this.gravityDatas)
		{
			GravityTransformControl gravityTransformControl = this.GetGravityTransformControl(maid, gravityData.categoryName);
			GameObject gameObject = gravityTransformControl.transform.parent.gameObject;
			if (gravityTransformControl.transTargetObject != null)
			{
				gravityTransformControl.transTargetObject.Delete();
			}
			UnityEngine.Object.DestroyImmediate(gameObject);
		}
		if (this.activeMaidControllerList.ContainsKey(maid.status.guid))
		{
			this.activeMaidControllerList.Remove(maid.status.guid);
		}
		if (this.activeMaidControllerList.Count <= 0)
		{
			this.noCharaActive.ChangeMode(true, true);
		}
	}

	public override void OnMaidChangeEvent(Maid maid)
	{
		base.OnMaidChangeEvent(maid);
		foreach (GravityControlWindow.GravityData gravityData in this.gravityDatas)
		{
			GravityTransformControl gravityTransformControl = (!(maid == null) && !maid.boMAN) ? this.GetGravityTransformControl(maid, gravityData.categoryName) : null;
			bool flag = !(gravityTransformControl == null) && gravityTransformControl.isValid;
			if (!flag)
			{
				gravityData.button.SetSelect(false);
			}
			gravityData.button.isEnabled = flag;
		}
		if (this.selectGravityData != null)
		{
			this.OnSelectCategoryButton(this.selectGravityData);
		}
	}

	public void OnChangeMekure()
	{
		if (base.mgr.select_maid == null || base.mgr.select_maid.boMAN)
		{
			return;
		}
		foreach (GravityControlWindow.GravityData gravityData in this.gravityDatas)
		{
			GravityTransformControl gravityTransformControl = this.GetGravityTransformControl(base.mgr.select_maid, gravityData.categoryName);
			if (!(gravityTransformControl == null))
			{
				gravityTransformControl.OnChangeMekure();
			}
		}
	}

	public override void OnSerializeEvent()
	{
		List<string> maidStoreGuidList = base.mgr.GetMaidStoreGuidList();
		foreach (string text in maidStoreGuidList)
		{
			Dictionary<string, string> maidStoreData = base.GetMaidStoreData(text);
			if (maidStoreData != null)
			{
				foreach (GravityControlWindow.GravityData gravityData in this.gravityDatas)
				{
					string categoryName = gravityData.categoryName;
					GravityTransformControl gravityTransformControl = this.GetGravityTransformControl(text, gravityData.categoryName);
					if (!(gravityTransformControl == null))
					{
						if (gravityTransformControl.isValid)
						{
							maidStoreData[categoryName] = gravityTransformControl.isEnabled.ToString();
							maidStoreData[categoryName + "pos"] = gravityTransformControl.transform.localPosition.ToString("G9");
						}
						else
						{
							maidStoreData[categoryName] = false.ToString();
							maidStoreData[categoryName + "pos"] = Vector3.zero.ToString("G9");
						}
					}
				}
			}
		}
	}

	public override void OnDeserializeEvent()
	{
	}

	private void OnSelectCategoryButton(GravityControlWindow.GravityData selectedData)
	{
		this.CloseEditSubWindow();
		if (base.mgr.select_maid == null || base.mgr.select_maid.boMAN || selectedData == null || !selectedData.button.isEnabled)
		{
			return;
		}
		GravityTransformControl gravityTransformControl = this.GetGravityTransformControl(base.mgr.select_maid, selectedData.categoryName);
		if (gravityTransformControl == null)
		{
			return;
		}
		this.transInput.SetTarget(gravityTransformControl.transform, Vector3.zero);
		this.checkBoxEnabledGravityControl.check = gravityTransformControl.isEnabled;
		gravityTransformControl.visibleTransTargetObject = true;
		this.editSubWindow.visible = true;
		selectedData.isSelectedButton = true;
		this.selectGravityData = selectedData;
	}

	private void CloseEditSubWindow()
	{
		if (this.selectGravityData != null)
		{
			this.selectGravityData.isSelectedButton = false;
			this.selectGravityData.button.SetSelect(false);
			this.editSubWindow.visible = false;
		}
		foreach (KeyValuePair<string, Dictionary<string, GravityTransformControl>> keyValuePair in this.activeMaidControllerList)
		{
			foreach (KeyValuePair<string, GravityTransformControl> keyValuePair2 in keyValuePair.Value)
			{
				keyValuePair2.Value.visibleTransTargetObject = false;
			}
		}
		this.selectGravityData = null;
	}

	private GravityTransformControl GetGravityTransformControl(Maid maid, string gravityCategoryName)
	{
		if (maid == null)
		{
			return null;
		}
		return this.GetGravityTransformControl(maid.status.guid, gravityCategoryName);
	}

	private GravityTransformControl GetGravityTransformControl(string maidGuid, string gravityCategoryName)
	{
		GravityTransformControl result = null;
		if (this.activeMaidControllerList.ContainsKey(maidGuid))
		{
			result = ((!this.activeMaidControllerList[maidGuid].ContainsKey(gravityCategoryName)) ? null : this.activeMaidControllerList[maidGuid][gravityCategoryName]);
		}
		return result;
	}

	[SerializeField]
	private GravityControlWindow.GravityData[] gravityDatas;

	[SerializeField]
	private WFCheckBox checkBoxEnabledGravityControl;

	[SerializeField]
	private PhotTransInput transInput;

	private GravityControlWindow.GravityData selectGravityData;

	private BasePhotoSubWindow editSubWindow;

	private PhotoNoCharaActive noCharaActive;

	private Dictionary<string, Dictionary<string, GravityTransformControl>> activeMaidControllerList = new Dictionary<string, Dictionary<string, GravityTransformControl>>();

	[Serializable]
	private class GravityData
	{
		public TBody.SlotID[] slotIds
		{
			get
			{
				if (this.slotIds_ == null && this.slotIdNames != null)
				{
					this.slotIds_ = new TBody.SlotID[this.slotIdNames.Length];
					for (int i = 0; i < this.slotIdNames.Length; i++)
					{
						this.slotIds_[i] = (TBody.SlotID)Enum.Parse(typeof(TBody.SlotID), this.slotIdNames[i]);
					}
				}
				return this.slotIds_;
			}
		}

		public bool isSelectedButton
		{
			get
			{
				return this.button.isSelected;
			}
			set
			{
				if (value)
				{
					BoxCollider component = this.button.GetComponent<BoxCollider>();
					Vector3 size = component.size;
					this.button.SetSelect(true);
					component.size = size;
				}
				else
				{
					this.button.SetSelect(false);
				}
			}
		}

		[SerializeField]
		public string categoryName;

		[SerializeField]
		public float forceRate;

		[SerializeField]
		public UIWFTabButton button;

		[SerializeField]
		private string[] slotIdNames;

		private TBody.SlotID[] slotIds_;
	}
}
