using System;
using RootMotion.FinalIK;
using UnityEngine;

public class LimbControl : MonoBehaviour
{
	public void Initialize(IKManager mgr, LimbControl.Type type)
	{
		this.ik_mgr_ = mgr;
		this.type_ = type;
		Maid maid = this.ik_mgr_.maid;
		Transform root_trans = maid.body0.GetBone("Bip01").transform.root;
		IKManager.BoneType[] array = null;
		if (type == LimbControl.Type.Arm_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.UpperArm_R,
				IKManager.BoneType.Forearm_R,
				IKManager.BoneType.Hand_R
			};
		}
		else if (type == LimbControl.Type.Foot_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Thigh_R,
				IKManager.BoneType.Calf_R,
				IKManager.BoneType.Foot_R
			};
		}
		else if (type == LimbControl.Type.Finger0_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger0_Root_R,
				IKManager.BoneType.Finger0_0_R,
				IKManager.BoneType.Finger0_1_R
			};
		}
		else if (type == LimbControl.Type.Finger1_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger1_Root_R,
				IKManager.BoneType.Finger1_0_R,
				IKManager.BoneType.Finger1_1_R
			};
		}
		else if (type == LimbControl.Type.Finger2_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger2_Root_R,
				IKManager.BoneType.Finger2_0_R,
				IKManager.BoneType.Finger2_1_R
			};
		}
		else if (type == LimbControl.Type.Finger3_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger3_Root_R,
				IKManager.BoneType.Finger3_0_R,
				IKManager.BoneType.Finger3_1_R
			};
		}
		else if (type == LimbControl.Type.Finger4_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger4_Root_R,
				IKManager.BoneType.Finger4_0_R,
				IKManager.BoneType.Finger4_1_R
			};
		}
		else if (type == LimbControl.Type.Toe0_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Toe0_Root_R,
				IKManager.BoneType.Root,
				IKManager.BoneType.Toe0_0_R
			};
		}
		else if (type == LimbControl.Type.Toe1_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Toe1_Root_R,
				IKManager.BoneType.Root,
				IKManager.BoneType.Toe1_0_R
			};
		}
		else if (type == LimbControl.Type.Toe2_R)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Toe2_Root_R,
				IKManager.BoneType.Root,
				IKManager.BoneType.Toe2_0_R
			};
		}
		else if (type == LimbControl.Type.Arm_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.UpperArm_L,
				IKManager.BoneType.Forearm_L,
				IKManager.BoneType.Hand_L
			};
		}
		else if (type == LimbControl.Type.Foot_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Thigh_L,
				IKManager.BoneType.Calf_L,
				IKManager.BoneType.Foot_L
			};
		}
		else if (type == LimbControl.Type.Finger0_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger0_Root_L,
				IKManager.BoneType.Finger0_0_L,
				IKManager.BoneType.Finger0_1_L
			};
		}
		else if (type == LimbControl.Type.Finger1_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger1_Root_L,
				IKManager.BoneType.Finger1_0_L,
				IKManager.BoneType.Finger1_1_L
			};
		}
		else if (type == LimbControl.Type.Finger2_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger2_Root_L,
				IKManager.BoneType.Finger2_0_L,
				IKManager.BoneType.Finger2_1_L
			};
		}
		else if (type == LimbControl.Type.Finger3_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger3_Root_L,
				IKManager.BoneType.Finger3_0_L,
				IKManager.BoneType.Finger3_1_L
			};
		}
		else if (type == LimbControl.Type.Finger4_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Finger4_Root_L,
				IKManager.BoneType.Finger4_0_L,
				IKManager.BoneType.Finger4_1_L
			};
		}
		else if (type == LimbControl.Type.Toe0_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Toe0_Root_L,
				IKManager.BoneType.Root,
				IKManager.BoneType.Toe0_0_L
			};
		}
		else if (type == LimbControl.Type.Toe1_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Toe1_Root_L,
				IKManager.BoneType.Root,
				IKManager.BoneType.Toe1_0_L
			};
		}
		else if (type == LimbControl.Type.Toe2_L)
		{
			array = new IKManager.BoneType[]
			{
				IKManager.BoneType.Toe2_Root_L,
				IKManager.BoneType.Root,
				IKManager.BoneType.Toe2_0_L
			};
		}
		this.bone_start_point_ = this.ik_mgr_.GetBone(array[0]).transform;
		this.bone_joint_point_ = ((array[1] == IKManager.BoneType.Root) ? null : this.ik_mgr_.GetBone(array[1]).transform);
		this.bone_end_point_ = this.ik_mgr_.GetBone(array[2]).transform;
		this.ik_fabrik_ = this.bone_start_point_.gameObject.AddComponent<FABRIK>();
		this.ik_fabrik_.fixTransforms = false;
		IKSolverFABRIK solver = this.ik_fabrik_.solver;
		solver.maxIterations = 1;
		solver.Initiate(root_trans);
		if (this.bone_joint_point_ != null)
		{
			this.joint_drag_point_ = IKManager.CreateIKDragPoint(this.bone_joint_point_.gameObject, maid);
			if (0 <= this.bone_joint_point_.gameObject.name.IndexOf("Finger"))
			{
				if (GameMain.Instance.VRMode)
				{
					this.joint_drag_point_.axis_obj.AllAxisObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
				}
				else
				{
					this.joint_drag_point_.axis_obj.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
				}
			}
			this.joint_drag_point_.drag_start_event = delegate()
			{
				this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
				this.ik_fabrik_.solver.SetChain(new Transform[]
				{
					this.bone_start_point_,
					this.bone_joint_point_
				}, root_trans);
				this.ik_fabrik_.solver.target = this.joint_drag_point_.transform;
			};
			this.joint_drag_point_.drag_end_event = delegate()
			{
				this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
				this.ik_mgr_.HistoryPush();
			};
		}
		this.tip_drag_point_ = IKManager.CreateIKDragPoint(this.bone_end_point_.gameObject, maid);
		if (0 <= this.bone_end_point_.gameObject.name.IndexOf("Finger") || 0 <= this.bone_end_point_.gameObject.name.IndexOf("Toe"))
		{
			if (GameMain.Instance.VRMode)
			{
				this.tip_drag_point_.axis_obj.AllAxisObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
			}
			else
			{
				this.tip_drag_point_.axis_obj.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
			}
		}
		this.tip_drag_point_.drag_start_event = delegate()
		{
			if (this.bone_joint_point_ != null)
			{
				this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
				if (this.joint_lock)
				{
					this.ik_fabrik_.solver.SetChain(new Transform[]
					{
						this.bone_joint_point_,
						this.bone_end_point_
					}, root_trans);
				}
				else
				{
					this.ik_fabrik_.solver.SetChain(new Transform[]
					{
						this.bone_start_point_,
						this.bone_joint_point_,
						this.bone_end_point_
					}, root_trans);
				}
			}
			else
			{
				this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
				this.ik_fabrik_.solver.SetChain(new Transform[]
				{
					this.bone_start_point_,
					this.bone_end_point_
				}, root_trans);
			}
			this.ik_fabrik_.solver.target = this.tip_drag_point_.transform;
		};
		this.tip_drag_point_.drag_end_event = delegate()
		{
			this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
			this.ik_mgr_.HistoryPush();
		};
	}

	public void OnDestroy()
	{
		if (this.joint_drag_point_ != null)
		{
			UnityEngine.Object.Destroy(this.joint_drag_point_.gameObject);
		}
		if (this.tip_drag_point_ != null)
		{
			UnityEngine.Object.Destroy(this.tip_drag_point_.gameObject);
		}
		this.joint_drag_point_ = null;
		this.tip_drag_point_ = null;
	}

	private void Update()
	{
		this.joint_lock = (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl));
	}

	public bool visible
	{
		get
		{
			return this.visible_joint_point || this.visible_tip_point;
		}
		set
		{
			this.visible_tip_point = value;
			this.visible_joint_point = value;
		}
	}

	private bool visible_joint_point
	{
		get
		{
			return this.joint_drag_point_ != null && this.joint_drag_point_.visible;
		}
		set
		{
			if (this.joint_drag_point_ != null)
			{
				this.joint_drag_point_.visible = value;
			}
		}
	}

	private bool visible_tip_point
	{
		get
		{
			return this.tip_drag_point_ != null && this.tip_drag_point_.visible;
		}
		set
		{
			if (this.tip_drag_point_ != null)
			{
				this.tip_drag_point_.visible = value;
			}
		}
	}

	public LimbControl.Type type
	{
		get
		{
			return this.type_;
		}
	}

	public bool joint_lock;

	private IKManager ik_mgr_;

	private Transform bone_start_point_;

	private Transform bone_joint_point_;

	private Transform bone_end_point_;

	private IKDragPoint joint_drag_point_;

	private IKDragPoint tip_drag_point_;

	private LimbControl.Type type_;

	private FABRIK ik_fabrik_;

	public enum Type
	{
		Arm_R,
		Foot_R,
		Finger0_R,
		Finger1_R,
		Finger2_R,
		Finger3_R,
		Finger4_R,
		Toe0_R,
		Toe1_R,
		Toe2_R,
		Arm_L,
		Foot_L,
		Finger0_L,
		Finger1_L,
		Finger2_L,
		Finger3_L,
		Finger4_L,
		Toe0_L,
		Toe1_L,
		Toe2_L
	}
}
