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

public class UserEditCtrl : MonoBehaviour
{
	public void Init(UserEditMgr userEditMgr, UserEditAPI userEditAPI, GameObject goUserEditPanel)
	{
		this.m_mgr = userEditMgr;
		this.m_goPanel = goUserEditPanel;
		this.m_userEditAPI = userEditAPI;
	}

	private void InitViewer()
	{
		this.m_name = UTY.GetChildObject(this.m_goPanel, "Name/InputField", false).GetComponent<UIInput>();
		this.m_nameBoxCollider = UTY.GetChildObject(this.m_goPanel, "Name/InputField", false).GetComponent<BoxCollider>();
		this.m_abdomenSlider = UTY.GetChildObject(this.m_goPanel, "Abdomen/Slider", false).GetComponent<UISlider>();
		this.m_abdomenValue = UTY.GetChildObject(this.m_goPanel, "Abdomen/Value/Number", false).GetComponent<UILabel>();
		this.m_colorRedSlider = UTY.GetChildObject(this.m_goPanel, "Color/R/Slider", false).GetComponent<UISlider>();
		this.m_colorGreenSlider = UTY.GetChildObject(this.m_goPanel, "Color/G/Slider", false).GetComponent<UISlider>();
		this.m_colorBlueSlider = UTY.GetChildObject(this.m_goPanel, "Color/B/Slider", false).GetComponent<UISlider>();
		this.m_goHeadPresetUnitParent = UTY.GetChildObject(this.m_goPanel, "Head/HeadPresetUnitParent", false);
		UIButton component = UTY.GetChildObject(this.m_goPanel, "Ok", false).GetComponent<UIButton>();
		EventDelegate.Add(component.onClick, new EventDelegate.Callback(this.m_mgr.CloseUserEditPanel));
		bool enabled = this.m_userEditAPI.FirstMan();
		this.m_nameBoxCollider.enabled = enabled;
		this.m_name.enabled = enabled;
		this.InitData();
	}

	private void InitData()
	{
		this.m_name.value = this.m_userEditAPI.GetName();
		this.m_abdomenSlider.value = this.m_userEditAPI.GetFat();
		this.m_abdomenValue.text = this.ToLabelValue(this.m_abdomenSlider.value, 100);
		this.m_colorRedSlider.value = this.m_userEditAPI.GetColor().r;
		this.m_colorGreenSlider.value = this.m_userEditAPI.GetColor().g;
		this.m_colorBlueSlider.value = this.m_userEditAPI.GetColor().b;
	}

	private string ToLabelValue(float input, int coefficient)
	{
		float num = (float)Math.Round((double)input, 2, MidpointRounding.AwayFromZero);
		float num2 = num * (float)coefficient;
		return ((int)num2).ToString();
	}

	public void CreateViewer(Dictionary<string, UserEditCtrl.HeadButton> dicHeadPreset)
	{
		if (!this.m_bInit)
		{
			this.InitViewer();
			this.m_bInit = true;
		}
		this.m_dicHeadPreset = dicHeadPreset;
		if (dicHeadPreset == null || dicHeadPreset.Count == 0)
		{
			return;
		}
		this.PrepareToCreateViewer(this.m_goHeadPresetUnitParent);
		this.ValidateHeadPreset(dicHeadPreset);
		this.m_goHeadPresetUnit = this.GetHeadPresetUnit();
		foreach (KeyValuePair<string, UserEditCtrl.HeadButton> keyValuePair in dicHeadPreset)
		{
			UserEditCtrl.HeadButton value = keyValuePair.Value;
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_goHeadPresetUnit);
			this.SetTransformInfo(gameObject, this.m_goHeadPresetUnitParent);
			gameObject.name = value.name;
			UITexture component = gameObject.GetComponent<UITexture>();
			component.mainTexture = value.headIcon;
			UIButton component2 = gameObject.GetComponent<UIButton>();
			EventDelegate.Add(component2.onClick, new EventDelegate.Callback(this.m_mgr.ClickPreset));
		}
		BaseCreateViewerCtrl.Reposition(this.m_goHeadPresetUnitParent);
	}

	private void PrepareToCreateViewer(GameObject goParent)
	{
		IEnumerator enumerator = goParent.transform.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				Transform transform = (Transform)obj;
				UnityEngine.Object.Destroy(transform.gameObject);
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		goParent.transform.DetachChildren();
	}

	private void ValidateHeadPreset(Dictionary<string, UserEditCtrl.HeadButton> dicHeadPreset)
	{
		if (dicHeadPreset.Count > 10)
		{
			Debug.LogError(string.Format("頭のプリセット数が予定されている最大数を超えています。最大数={0}", 10));
		}
	}

	private GameObject GetHeadPresetUnit()
	{
		this.m_goHeadPresetUnit = (this.m_goHeadPresetUnit ?? (Resources.Load("SceneUserEdit/Prefab/HeadPresetUnit") as GameObject));
		return this.m_goHeadPresetUnit;
	}

	private void SetTransformInfo(GameObject copyPrefabs, GameObject goParent)
	{
		copyPrefabs.transform.parent = goParent.transform;
		copyPrefabs.transform.localScale = Vector3.one;
		copyPrefabs.transform.localPosition = Vector3.zero;
		copyPrefabs.transform.rotation = Quaternion.identity;
	}

	public void SetPreset(string btnName)
	{
		UserEditCtrl.HeadButton headButton = new UserEditCtrl.HeadButton();
		if (this.m_dicHeadPreset.TryGetValue(btnName, out headButton))
		{
			if (!this.m_userEditAPI.IsSelected(MPN.head, headButton.item))
			{
				this.m_userEditAPI.ProcMenu(headButton.item);
			}
		}
		else
		{
			Debug.LogError(string.Format("不適切なプリセット(頭)が選択されました。選択されたプリセット名={0}", btnName));
		}
	}

	public void UpdateAbdomenValue()
	{
		if (this.m_bInit)
		{
			float num = (float)Math.Round((double)this.m_abdomenSlider.value, 2, MidpointRounding.AwayFromZero);
			this.m_abdomenValue.text = this.ToLabelValue(num, 100);
			this.m_userEditAPI.SetFat(num);
		}
	}

	public void UpdateColorValue()
	{
		if (this.m_bInit)
		{
			this.m_userEditAPI.SetColor(new Color(this.m_colorRedSlider.value, this.m_colorGreenSlider.value, this.m_colorBlueSlider.value, 1f));
		}
	}

	public void SetName(string name)
	{
		this.m_userEditAPI.SetName(name);
	}

	public void SaveAndLoadScene()
	{
		this.m_userEditAPI.SetName(this.m_name.value);
	}

	private UserEditMgr m_mgr;

	private UserEditAPI m_userEditAPI;

	private Dictionary<string, UserEditCtrl.HeadButton> m_dicHeadPreset;

	private GameObject m_goPanel;

	private GameObject m_goHeadPresetUnit;

	private GameObject m_goHeadPresetUnitParent;

	private BoxCollider m_nameBoxCollider;

	private UIInput m_name;

	private UISlider m_abdomenSlider;

	private UISlider m_colorRedSlider;

	private UISlider m_colorGreenSlider;

	private UISlider m_colorBlueSlider;

	private UILabel m_abdomenValue;

	private bool m_bInit;

	private const string HEAD_PRESET_UNIT_PARENT_PATH = "Head/HeadPresetUnitParent";

	private const string HEAD_PRESET_UNIT_PATH = "SceneUserEdit/Prefab/HeadPresetUnit";

	private const string SCENE_NAME_FROM_USER_EDIT = "SceneEdit";

	private const int MAX_HEAD_PRESET_COUNT = 10;

	private const int ABDOMEN_COEFFICIENT = 100;

	private const int COLOR_COEFFICIENT = 255;

	private const int SLIDER_NUMBER_OF_DECIMAL_PLACES = 2;

	public class HeadButton
	{
		public string name;

		public Texture2D headIcon;

		public SceneEdit.SMenuItem item;
	}
}
