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

public class ChinkoCtrl
{
	public ChinkoCtrl(Transform chinkoCenterTrs)
	{
		List<Transform> list = this.worldPointList;
		this.m_chinkoCenterTrs = chinkoCenterTrs;
		list.Add(chinkoCenterTrs);
		this.worldPointList.Add(chinkoCenterTrs.Find("chinko1"));
		this.worldPointList.Add(this.worldPointList[this.worldPointList.Count - 1].Find("chinko2"));
		this.worldPointList.Add(this.worldPointList[this.worldPointList.Count - 1].Find("chinko_nub"));
	}

	public void StartChinkoSlide(Maid targetMaid, ChinkoCtrl.TARGET targetId, Vector3 offset, float apprDist = 0.05f)
	{
		this.m_targetMaidReg = CMT.SearchObjName(targetMaid.body0.m_Bones.transform, this.m_targetBoneName[(int)targetId], true);
		this.m_offset = offset;
		this.m_apprDistance = apprDist;
	}

	public void StopChinkoSlide()
	{
		this.m_targetMaidReg = null;
	}

	public void SelfUpdate()
	{
		if (this.m_targetMaidReg == null)
		{
			return;
		}
		Plane plane = new Plane(-this.m_targetMaidReg.forward, this.m_targetMaidReg.position);
		Plane plane2 = new Plane(-this.m_chinkoCenterTrs.right, this.m_chinkoCenterTrs.position);
		if (plane2.GetSide(this.m_targetMaidReg.position))
		{
			bool flag = false;
			for (int i = 1; i < this.worldPointList.Count; i++)
			{
				Vector3 vector;
				if (MathCM.PlaneSegmentIntersect(plane, this.worldPointList[i - 1].position, this.worldPointList[i].position, out vector))
				{
					Debug.DrawLine(vector, this.m_chinkoCenterTrs.position, Color.magenta);
					Vector3 b = this.m_targetMaidReg.position + this.m_offset - vector;
					this.m_chinkoCenterTrs.position = this.m_chinkoCenterTrs.position + b;
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				Vector3 position = this.worldPointList[this.worldPointList.Count - 2].position;
				Vector3 position2 = this.worldPointList[this.worldPointList.Count - 1].position;
				float num = 0f;
				Vector3 normalized = (position2 - position).normalized;
				if (plane.Raycast(new Ray(position, normalized), out num))
				{
					float num2 = num - Vector3.Distance(position, position2);
					float d = 1f - Mathf.Clamp01(num2 / this.m_apprDistance);
					Vector3 vector = position + normalized * num;
					Vector3 a = this.m_targetMaidReg.position + this.m_offset - vector;
					this.m_chinkoCenterTrs.position = this.m_chinkoCenterTrs.position + a * d;
				}
			}
		}
	}

	private string[] m_targetBoneName = new string[]
	{
		"Mouth",
		"_IK_vagina",
		"_IK_anal"
	};

	private List<Transform> worldPointList = new List<Transform>();

	private Transform m_chinkoCenterTrs;

	private Transform m_targetMaidReg;

	private Vector3 m_offset = Vector3.zero;

	private float m_apprDistance = 0.05f;

	public enum TARGET
	{
		MOUTH,
		VAGINA,
		ANAL,
		MAX
	}
}
