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

public class GravityTransformControl : MonoBehaviour
{
	public bool isEnabled
	{
		get
		{
			return this.isEnabled_;
		}
		set
		{
			this.isEnabled_ = false;
			if (value && this.isValid)
			{
				this.isEnabled_ = true;
			}
		}
	}

	public bool isValid
	{
		get
		{
			return (this.dynamickirtBones != null && this.dynamickirtBones.Count != 0) || (this.dynamicBones != null && this.dynamicBones.Count != 0);
		}
	}

	public bool visibleTransTargetObject
	{
		get
		{
			return this.transTargetObject != null && this.transTargetObject.axis_obj.Visible;
		}
		set
		{
			if (this.transTargetObject != null)
			{
				this.transTargetObject.axis_obj.Visible = value;
			}
		}
	}

	private void Awake()
	{
		if (this.maidBody != null)
		{
			return;
		}
		Transform parent = base.transform.parent;
		while (parent != null)
		{
			this.maidBody = parent.GetComponent<TBody>();
			if (this.maidBody != null)
			{
				break;
			}
			parent = parent.parent;
		}
	}

	public void SetTargetSlods(TBody.SlotID[] slotIds)
	{
		this.Awake();
		this.targetSloatIds = slotIds;
		this.dynamickirtBones.Clear();
		this.dynamicBones.Clear();
		foreach (TBody.SlotID index in slotIds)
		{
			if (this.maidBody.goSlot[(int)index] != null && !(this.maidBody.goSlot[(int)index].obj == null))
			{
				DynamicSkirtBone component = this.maidBody.goSlot[(int)index].obj.GetComponent<DynamicSkirtBone>();
				if (component != null)
				{
					this.dynamickirtBones.Add(new KeyValuePair<DynamicSkirtBone, Vector3>(component, component.m_vGravity));
				}
				DynamicBone component2 = this.maidBody.goSlot[(int)index].obj.GetComponent<DynamicBone>();
				if (component2 != null)
				{
					this.dynamicBones.Add(new KeyValuePair<DynamicBone, Vector3>(component2, component2.m_Force));
				}
			}
		}
	}

	public bool OnChangeMekure()
	{
		bool flag = false;
		foreach (KeyValuePair<DynamicSkirtBone, Vector3> keyValuePair in this.dynamickirtBones)
		{
			if (keyValuePair.Key == null)
			{
				flag = true;
				break;
			}
		}
		if (!flag)
		{
			foreach (KeyValuePair<DynamicBone, Vector3> keyValuePair2 in this.dynamicBones)
			{
				if (keyValuePair2.Key == null)
				{
					flag = true;
					break;
				}
			}
		}
		if (flag)
		{
			this.SetTargetSlods(this.targetSloatIds);
		}
		return true;
	}

	public void Update()
	{
		Vector3 b = Vector3.zero;
		Vector3 vector = base.transform.localPosition;
		vector = new Vector3(Mathf.Max(Mathf.Min(1f, vector.x), -1f), Mathf.Max(Mathf.Min(1f, vector.y), -1f), Mathf.Max(Mathf.Min(1f, vector.z), -1f));
		base.transform.localPosition = vector;
		if (!this.isEnabled)
		{
			vector = Vector3.zero;
		}
		if (!wf.Math.Approximately(vector, Vector3.zero))
		{
			b = vector * this.forceRate;
		}
		foreach (KeyValuePair<DynamicSkirtBone, Vector3> keyValuePair in this.dynamickirtBones)
		{
			DynamicSkirtBone key = keyValuePair.Key;
			Vector3 vector2 = keyValuePair.Value + b;
			if (!wf.Math.Approximately(key.m_vGravity, vector2))
			{
				key.m_vGravity = vector2;
				key.UpdateParameters();
			}
		}
		foreach (KeyValuePair<DynamicBone, Vector3> keyValuePair2 in this.dynamicBones)
		{
			DynamicBone key2 = keyValuePair2.Key;
			Vector3 vector3 = keyValuePair2.Value + b;
			if (!wf.Math.Approximately(key2.m_Force, vector3))
			{
				key2.m_Force = vector3;
				key2.UpdateParameters();
			}
		}
	}

	public float forceRate = 1f;

	public PhotoTransTargetObject transTargetObject;

	private TBody maidBody;

	private TBody.SlotID[] targetSloatIds;

	private List<KeyValuePair<DynamicSkirtBone, Vector3>> dynamickirtBones = new List<KeyValuePair<DynamicSkirtBone, Vector3>>();

	private List<KeyValuePair<DynamicBone, Vector3>> dynamicBones = new List<KeyValuePair<DynamicBone, Vector3>>();

	private bool isEnabled_;
}