using System;
using System.Collections;
using System.Collections.Specialized;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CreativeRoomUIPlacementMaid : MonoBehaviour
{
	private Maid targetMaid
	{
		get
		{
			if (this.m_TargetMaid == null)
			{
				this.m_TargetMaid = this.GetMaid(true);
				if (this.m_TargetMaid == null)
				{
					this.m_TargetMaid = this.GetMaid(false);
				}
				if (this.m_TargetMaid)
				{
					this.m_TargetMaid.body0.SetBoneHitHeightY(-1000f);
				}
			}
			return this.m_TargetMaid;
		}
	}

	private Maid GetMaid(bool searchLeader = true)
	{
		if (this.m_CharaMgr == null)
		{
			return null;
		}
		Maid maid = null;
		foreach (Maid maid2 in this.m_CharaMgr.GetStockMaidList())
		{
			if (maid2 != null)
			{
				if (!searchLeader || maid2.status.leader)
				{
					maid = maid2;
					break;
				}
			}
		}
		if (maid == null)
		{
			int maidCount = this.m_CharaMgr.GetMaidCount();
			for (int i = 0; i < maidCount; i++)
			{
				Maid maid3 = this.m_CharaMgr.GetMaid(i);
				if (maid3 != null)
				{
					if (!searchLeader || maid3.status.leader)
					{
						maid = maid3;
						break;
					}
				}
			}
		}
		return maid;
	}

	public float snapValuePosition
	{
		set
		{
			if (this.m_GizmoObject == null)
			{
				return;
			}
			this.m_GizmoObject.snapValuePosition = value;
		}
	}

	private void Awake()
	{
		this.m_CharaMgr = GameMain.Instance.CharacterMgr;
		GameObject gameObject;
		if (!(gameObject = GameObject.Find("[CreativeRoomUI Placement Maid] GizmoControlObject")))
		{
			gameObject = new GameObject("[CreativeRoomUI Placement Maid] GizmoControlObject");
			GizmoObject gizmoObject = gameObject.AddComponent<GizmoObject>();
			gizmoObject.activeAxis = true;
			gizmoObject.activeRot = false;
			gizmoObject.snapValuePosition = 0.5f;
			gizmoObject.snapValueAngles = 15f;
			gizmoObject.snapValueScale = 0.1f;
			gameObject.SetActive(false);
		}
		this.m_GizmoObject = gameObject.GetComponent<GizmoObject>();
		GizmoObject gizmoObject2 = this.m_GizmoObject;
		gizmoObject2.onChangePosition = (Action<Vector3>)Delegate.Combine(gizmoObject2.onChangePosition, new Action<Vector3>(this.OnUpdateGizmoPosition));
		GizmoObject gizmoObject3 = this.m_GizmoObject;
		gizmoObject3.onChangeRotation = (Action<Quaternion>)Delegate.Combine(gizmoObject3.onChangeRotation, new Action<Quaternion>(this.OnUpdateGizmoRotation));
		GizmoObject gizmoObject4 = this.m_GizmoObject;
		gizmoObject4.onChangeScale = (Action<Vector3>)Delegate.Combine(gizmoObject4.onChangeScale, new Action<Vector3>(this.OnUpdateGizmoScale));
		this.SetUpUI();
	}

	private void SetUpUI()
	{
		this.m_CachedTransformDataDic = new StringDictionary();
		this.m_InputFieldPosX.contentType = InputField.ContentType.DecimalNumber;
		this.m_InputFieldPosY.contentType = InputField.ContentType.DecimalNumber;
		this.m_InputFieldPosZ.contentType = InputField.ContentType.DecimalNumber;
		this.m_InputFieldRotX.contentType = InputField.ContentType.DecimalNumber;
		this.m_InputFieldRotY.contentType = InputField.ContentType.DecimalNumber;
		this.m_InputFieldRotZ.contentType = InputField.ContentType.DecimalNumber;
		this.m_InputFieldScale.contentType = InputField.ContentType.DecimalNumber;
		this.m_InputFieldPosX.text = this.m_GizmoObject.position.x.ToString("f3");
		this.m_InputFieldPosY.text = this.m_GizmoObject.position.y.ToString("f3");
		this.m_InputFieldPosZ.text = this.m_GizmoObject.position.z.ToString("f3");
		this.m_InputFieldRotX.text = this.m_GizmoObject.eulerAngles.x.ToString("f3");
		this.m_InputFieldRotY.text = this.m_GizmoObject.eulerAngles.y.ToString("f3");
		this.m_InputFieldRotZ.text = this.m_GizmoObject.eulerAngles.z.ToString("f3");
		this.m_InputFieldScale.text = this.m_GizmoObject.scale.x.ToString("f3");
		this.m_SliderScale.value = this.m_GizmoObject.scale.x;
		Action<InputField, Action<float>, Action> action = delegate(InputField input, Action<float> act_change, Action act_end)
		{
			input.onValueChanged.AddListener(delegate(string str)
			{
				float obj;
				if (float.TryParse(str, out obj))
				{
					act_change(obj);
				}
			});
			input.onEndEdit.AddListener(delegate(string str)
			{
				act_end();
			});
		};
		action(this.m_InputFieldPosX, delegate(float value)
		{
			Vector3 position = this.m_GizmoObject.position;
			position.x = value;
			this.m_GizmoObject.position = position;
		}, delegate
		{
			this.m_InputFieldPosX.text = this.m_GizmoObject.position.x.ToString("f3");
		});
		action(this.m_InputFieldPosY, delegate(float value)
		{
			Vector3 position = this.m_GizmoObject.position;
			position.y = value;
			this.m_GizmoObject.position = position;
		}, delegate
		{
			this.m_InputFieldPosY.text = this.m_GizmoObject.position.y.ToString("f3");
		});
		action(this.m_InputFieldPosZ, delegate(float value)
		{
			Vector3 position = this.m_GizmoObject.position;
			position.z = value;
			this.m_GizmoObject.position = position;
		}, delegate
		{
			this.m_InputFieldPosZ.text = this.m_GizmoObject.position.z.ToString("f3");
		});
		action(this.m_InputFieldRotX, delegate(float value)
		{
			Vector3 eulerAngles = this.m_GizmoObject.eulerAngles;
			eulerAngles.x = value;
			this.m_GizmoObject.eulerAngles = eulerAngles;
		}, delegate
		{
			this.m_InputFieldRotX.text = this.m_GizmoObject.eulerAngles.x.ToString("f3");
		});
		action(this.m_InputFieldRotY, delegate(float value)
		{
			Vector3 eulerAngles = this.m_GizmoObject.eulerAngles;
			eulerAngles.y = value;
			this.m_GizmoObject.eulerAngles = eulerAngles;
		}, delegate
		{
			this.m_InputFieldRotY.text = this.m_GizmoObject.eulerAngles.y.ToString("f3");
		});
		action(this.m_InputFieldRotZ, delegate(float value)
		{
			Vector3 eulerAngles = this.m_GizmoObject.eulerAngles;
			eulerAngles.z = value;
			this.m_GizmoObject.eulerAngles = eulerAngles;
		}, delegate
		{
			this.m_InputFieldRotZ.text = this.m_GizmoObject.eulerAngles.z.ToString("f3");
		});
		action(this.m_InputFieldScale, delegate(float value)
		{
			Vector3 scale = this.m_GizmoObject.scale;
			value = Mathf.Clamp(value, this.m_SliderScale.minValue, this.m_SliderScale.maxValue);
			if (Mathf.Approximately(scale.x, value))
			{
				return;
			}
			scale.x = (scale.y = (scale.z = value));
			this.m_GizmoObject.scale = scale;
			this.m_SliderScale.value = value;
		}, delegate
		{
			this.m_InputFieldScale.text = this.m_GizmoObject.scale.x.ToString("f3");
		});
		this.m_SliderScale.onValueChanged.AddListener(delegate(float value)
		{
			if (Mathf.Approximately(this.m_GizmoObject.cachedSnapScale.x, value))
			{
				return;
			}
			this.m_GizmoObject.nonCallbackScale = Vector3.one * value;
			float x = this.m_GizmoObject.cachedSnapScale.x;
			this.m_SliderScale.value = x;
			if (!this.m_InputFieldScale.isFocused)
			{
				this.m_InputFieldScale.text = x.ToString("f3");
			}
		});
	}

	private void Update()
	{
		if (this.targetMaid == null || !this.targetMaid.Visible)
		{
			this.m_GizmoObject.gameObject.SetActive(false);
			return;
		}
		if (EventSystem.current != null)
		{
			if (NInput.GetMouseButtonDown(0))
			{
				GameObject currentSelectedGameObject = EventSystem.current.currentSelectedGameObject;
				if (EventSystem.current.IsPointerOverGameObject() && currentSelectedGameObject && currentSelectedGameObject.GetComponent<RectTransform>())
				{
					this.m_GizmoObject.gameObject.SetActive(false);
				}
			}
			else if (NInput.GetMouseButtonUp(0))
			{
				this.m_GizmoObject.gameObject.SetActive(true);
			}
		}
	}

	private void OnUpdateGizmoPosition(Vector3 position)
	{
		if (this.targetMaid != null)
		{
			this.targetMaid.transform.position = position;
			if (!this.m_InputFieldPosX.isFocused)
			{
				this.m_InputFieldPosX.text = position.x.ToString("f3");
			}
			if (!this.m_InputFieldPosY.isFocused)
			{
				this.m_InputFieldPosY.text = position.y.ToString("f3");
			}
			if (!this.m_InputFieldPosZ.isFocused)
			{
				this.m_InputFieldPosZ.text = position.z.ToString("f3");
			}
		}
	}

	private void OnUpdateGizmoRotation(Quaternion rotation)
	{
		if (this.targetMaid != null)
		{
			this.targetMaid.transform.rotation = rotation;
			Vector3 eulerAngles = rotation.eulerAngles;
			if (!this.m_InputFieldRotX.isFocused)
			{
				this.m_InputFieldRotX.text = eulerAngles.x.ToString("f3");
			}
			if (!this.m_InputFieldRotY.isFocused)
			{
				this.m_InputFieldRotY.text = eulerAngles.y.ToString("f3");
			}
			if (!this.m_InputFieldRotZ.isFocused)
			{
				this.m_InputFieldRotZ.text = eulerAngles.z.ToString("f3");
			}
		}
	}

	private void OnUpdateGizmoScale(Vector3 scale)
	{
		if (this.targetMaid != null)
		{
			this.targetMaid.transform.localScale = scale;
			if (!this.m_InputFieldScale.isFocused)
			{
				this.m_InputFieldScale.text = scale.x.ToString("f3");
			}
			this.m_SliderScale.value = scale.x;
		}
	}

	public void ButtonEvent_GizmoEnableMove()
	{
		GizmoObject gizmoObject = this.m_GizmoObject;
		gizmoObject.activeAxis = true;
		gizmoObject.activeRot = false;
	}

	public void ButtonEvent_GizmoEnableRot()
	{
		GizmoObject gizmoObject = this.m_GizmoObject;
		gizmoObject.activeAxis = false;
		gizmoObject.activeRot = true;
	}

	public void ButtonEvent_GizmoDisable()
	{
		GizmoObject gizmoObject = this.m_GizmoObject;
		gizmoObject.activeAxis = false;
		gizmoObject.activeRot = false;
	}

	public void ButtonEvent_CopyPosition()
	{
		Vector3 position = this.m_GizmoObject.position;
		string text = string.Format("{0},{1},{2}", position.x, position.y, position.z);
		this.m_CachedTransformDataDic["position"] = text.ToString();
	}

	public void ButtonEvent_PastePosition()
	{
		string text = this.m_CachedTransformDataDic["position"];
		if (string.IsNullOrEmpty(text))
		{
			Debug.Log("座標データはまだない");
			return;
		}
		string[] array = text.Split(new char[]
		{
			','
		});
		if (array.Length != 3)
		{
			Debug.LogError("position の文字数が" + array.Length + "でした");
			return;
		}
		this.m_GizmoObject.position = new Vector3(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]));
	}

	public void ButtonEvent_ResetPosition()
	{
		this.m_GizmoObject.position = Vector3.zero;
	}

	public void ButtonEvent_CopyRotation()
	{
		Quaternion rotation = this.m_GizmoObject.rotation;
		string text = string.Format("{0},{1},{2},{3}", new object[]
		{
			rotation.x,
			rotation.y,
			rotation.z,
			rotation.w
		});
		this.m_CachedTransformDataDic["rotation"] = text.ToString();
	}

	public void ButtonEvent_PasteRotation()
	{
		string text = this.m_CachedTransformDataDic["rotation"];
		if (string.IsNullOrEmpty(text))
		{
			Debug.Log("回転データはまだない");
			return;
		}
		string[] array = text.Split(new char[]
		{
			','
		});
		if (array.Length != 4)
		{
			Debug.LogError("rotation の文字数が" + array.Length + "でした");
			return;
		}
		this.m_GizmoObject.rotation = new Quaternion(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]), float.Parse(array[3]));
	}

	public void ButtonEvent_ResetRotation()
	{
		this.m_GizmoObject.rotation = Quaternion.identity;
	}

	public void ButtonEvent_CopyScale()
	{
		Vector3 scale = this.m_GizmoObject.scale;
		string text = string.Format("{0},{1},{2}", scale.x, scale.y, scale.z);
		this.m_CachedTransformDataDic["scale"] = text.ToString();
	}

	public void ButtonEvent_PasteScale()
	{
		string text = this.m_CachedTransformDataDic["scale"];
		if (string.IsNullOrEmpty(text))
		{
			Debug.Log("スケールデータはまだない");
			return;
		}
		string[] array = text.Split(new char[]
		{
			','
		});
		if (array.Length != 3)
		{
			Debug.LogError("scale の文字数が" + array.Length + "でした");
			return;
		}
		this.m_GizmoObject.scale = new Vector3(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]));
	}

	public void ButtonEvent_ResetScale()
	{
		this.m_GizmoObject.scale = Vector3.one;
	}

	public void ButtonEvent_CameraFocus()
	{
		Maid targetMaid = this.targetMaid;
		if (!targetMaid)
		{
			return;
		}
		if (!targetMaid.Visible)
		{
			return;
		}
		Transform bone = targetMaid.body0.GetBone("Bip01 Head");
		WfCameraMoveSupport wfCameraMoveSupport = base.GetComponent<WfCameraMoveSupport>();
		if (wfCameraMoveSupport == null)
		{
			wfCameraMoveSupport = base.gameObject.AddComponent<WfCameraMoveSupport>();
		}
		Vector3 position = bone.position;
		float distance = Mathf.Sqrt(5f) * 2f;
		Vector2 aroundAngle = new Vector2(-135f, 45f);
		wfCameraMoveSupport.StartCameraPosition(position, distance, aroundAngle);
	}

	public void ButtonEvent_VisibleMaid(bool isVisible)
	{
		if (this.m_CharaMgr.IsBusy())
		{
			return;
		}
		if (this.targetMaid == null)
		{
			return;
		}
		if (this.targetMaid.ActiveSlotNo == -1)
		{
			this.m_CharaMgr.SetActiveMaid(this.targetMaid, 0);
			this.targetMaid.AllProcPropSeqStart();
		}
		this.targetMaid.Visible = isVisible;
		if (isVisible && this.targetMaid.IsBusy)
		{
			uGUIUtility.SetActiveEventSystem(false);
			GameMain.Instance.MainCamera.FadeOut(0f, false, null, true, default(Color));
			base.StartCoroutine(this.CorMaidLoad(this.targetMaid, new Action(this.OnMaidLoadEnd)));
		}
	}

	public void ButtonEvent_ToggleSnap(bool isEnable)
	{
		if (this.m_GizmoObject)
		{
			if (isEnable)
			{
				this.m_GizmoObject.controlType = GizmoObject.ControlType.Snap;
			}
			else
			{
				this.m_GizmoObject.controlType = GizmoObject.ControlType.Default;
			}
		}
	}

	private IEnumerator CorMaidLoad(Maid targetMaid, Action actLoadEnd)
	{
		targetMaid.Visible = true;
		while (targetMaid.IsBusy)
		{
			yield return null;
		}
		actLoadEnd();
		yield break;
	}

	private void OnMaidLoadEnd()
	{
		GameMain.Instance.MainCamera.FadeIn(0.3f, false, null, true, true, default(Color));
		this.targetMaid.CrossFade("maid_stand01.anm", false, true, false, 0.5f, 1f);
		this.targetMaid.FaceAnime("通常", 0f, 0);
		this.targetMaid.EyeToCamera(Maid.EyeMoveType.目と顔を向ける, 0f);
		uGUIUtility.SetActiveEventSystem(true);
	}

	private void OnDisable()
	{
		if (this.m_GizmoObject)
		{
			this.m_GizmoObject.gameObject.SetActive(false);
		}
	}

	private void OnEnable()
	{
		if (this.m_GizmoObject && this.targetMaid != null && this.targetMaid.Visible)
		{
			this.m_GizmoObject.gameObject.SetActive(true);
		}
	}

	private void OnDestroy()
	{
		if (this.m_GizmoObject)
		{
			UnityEngine.Object.Destroy(this.m_GizmoObject);
		}
		if (this.targetMaid)
		{
			this.targetMaid.transform.position = Vector3.zero;
			this.targetMaid.transform.rotation = Quaternion.identity;
			this.targetMaid.transform.localScale = Vector3.one;
			this.targetMaid.ResetAll();
			this.targetMaid.Visible = false;
		}
	}

	[Header("座標入力欄")]
	[SerializeField]
	private InputField m_InputFieldPosX;

	[SerializeField]
	private InputField m_InputFieldPosY;

	[SerializeField]
	private InputField m_InputFieldPosZ;

	[Header("回転入力欄")]
	[SerializeField]
	private InputField m_InputFieldRotX;

	[SerializeField]
	private InputField m_InputFieldRotY;

	[SerializeField]
	private InputField m_InputFieldRotZ;

	[Header("スケール入力欄")]
	[SerializeField]
	private InputField m_InputFieldScale;

	[SerializeField]
	private Slider m_SliderScale;

	private CharacterMgr m_CharaMgr;

	private GizmoObject m_GizmoObject;

	private Maid m_TargetMaid;

	private StringDictionary m_CachedTransformDataDic;
}
