using System;
using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class PresetCtrl : MonoBehaviour
{
	public void Init(GameObject goPresetPanel, Maid maid)
	{
		this.m_goPresetPanel = goPresetPanel;
		this.m_maid = maid;
		this.m_goPresetParent = UTY.GetChildObject(this.m_goPresetPanel, "PresetViewer/Contents/PresetUnitParent", false);
		GameObject childObject = UTY.GetChildObject(this.m_goPresetPanel, "PresetViewer", false);
		this.m_presetScrollView = UTY.GetChildObject(childObject, "Contents", false).GetComponent<UIScrollView>();
		this.m_presetScrollBar = UTY.GetChildObject(childObject, "Scroll Bar", false).GetComponent<UIScrollBar>();
		this.m_listBtnState = new Dictionary<PresetMgr.Filter, PresetCtrl.FilterButton>();
		bool flag = false;
		IEnumerator enumerator = Enum.GetValues(typeof(PresetMgr.Filter)).GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				PresetMgr.Filter filter = (PresetMgr.Filter)obj;
				if (filter != PresetMgr.Filter.None)
				{
					UIButton component = UTY.GetChildObject(this.m_goPresetPanel, "FilterButtons/" + filter, false).GetComponent<UIButton>();
					if (!flag)
					{
						this.activeColor = new Color(component.defaultColor.r, component.defaultColor.g, component.defaultColor.b, 1f);
						this.inActiveColor = component.defaultColor;
						flag = true;
					}
					GameObject childObject2 = UTY.GetChildObject(this.m_goPresetPanel, "FilterButtons/" + filter + "/SelectCursor", false);
					childObject2.SetActive(false);
					PresetCtrl.FilterButton filterButton = new PresetCtrl.FilterButton();
					filterButton.m_name = filter;
					filterButton.m_btnButton = component;
					filterButton.m_goSelectCursor = childObject2;
					this.m_listBtnState.Add(filterButton.m_name, filterButton);
				}
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
	}

	public void CreatePresetList(List<CharacterMgr.Preset> listPreset)
	{
		if (listPreset == null)
		{
			return;
		}
		if (this.m_presetPrefab == null)
		{
			this.m_presetPrefab = Resources.Load("SceneEdit/Preset/Prefabs/PresetUnit");
			if (this.m_presetPrefab == null)
			{
				Debug.LogError(string.Format("ロードされるプレハブが見つかりませんでした。パス={0}", "SceneEdit/Preset/Prefabs/PresetUnit"));
				return;
			}
		}
		this.ClearExistPreset(this.m_goPresetParent);
		this.m_dicPresetButton = new Dictionary<string, PresetCtrl.PresetButton>();
		foreach (CharacterMgr.Preset preset in listPreset)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(this.m_presetPrefab) as GameObject;
			gameObject.transform.parent = this.m_goPresetParent.transform;
			gameObject.transform.localScale = Vector3.one;
			gameObject.transform.localPosition = Vector3.zero;
			gameObject.transform.rotation = Quaternion.identity;
			UITexture component = gameObject.GetComponent<UITexture>();
			component.mainTexture = preset.texThum;
			GameObject childObject = UTY.GetChildObject(gameObject, "TypeIcon", false);
			UISprite component2 = childObject.GetComponent<UISprite>();
			CharacterMgr.PresetType ePreType = preset.ePreType;
			if (ePreType != CharacterMgr.PresetType.All)
			{
				if (ePreType != CharacterMgr.PresetType.Wear)
				{
					if (ePreType == CharacterMgr.PresetType.Body)
					{
						component2.GetComponent<Localize>().SetTerm("SceneEdit/プリセット/スプライト/種別プレート体");
					}
				}
				else
				{
					component2.GetComponent<Localize>().SetTerm("SceneEdit/プリセット/スプライト/種別プレート服");
				}
			}
			else
			{
				component2.GetComponent<Localize>().SetTerm("SceneEdit/プリセット/スプライト/種別プレート体と服");
			}
			gameObject.name = preset.strFileName;
			UIButton component3 = gameObject.GetComponent<UIButton>();
			EventDelegate.Add(component3.onClick, new EventDelegate.Callback(BaseMgr<PresetMgr>.Instance.ClickPreset));
			PresetCtrl.PresetButton presetButton = new PresetCtrl.PresetButton();
			presetButton.id = gameObject.name;
			presetButton.presetButton = gameObject;
			presetButton.preset = preset;
			presetButton.typeIcon = childObject;
			this.m_dicPresetButton.Add(presetButton.id, presetButton);
		}
		this.AdjustTargetPosition(this.m_goPresetParent, this.m_presetScrollView);
	}

	private void AdjustTargetPosition(GameObject go, UIScrollView scrollView)
	{
		go.GetComponent<UITable>().Reposition();
		scrollView.ResetPosition();
	}

	private void ClearExistPreset(GameObject parent)
	{
		IEnumerator enumerator = parent.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();
			}
		}
		parent.transform.DetachChildren();
	}

	public void FilterPreset(PresetMgr.Filter clickBtnName, PresetMgr.Filter currentActiveFilterBtnName)
	{
		this.FilterPreset(clickBtnName, currentActiveFilterBtnName, true);
	}

	public void FilterPreset(PresetMgr.Filter clickBtnName, PresetMgr.Filter currentActiveFilterBtnName, bool setBtnActive)
	{
		if (setBtnActive)
		{
			this.SetFilterButtonActive(clickBtnName, currentActiveFilterBtnName);
		}
		foreach (KeyValuePair<string, PresetCtrl.PresetButton> keyValuePair in this.m_dicPresetButton)
		{
			CharacterMgr.Preset preset = keyValuePair.Value.preset;
			if (clickBtnName == PresetMgr.Filter.NotFilter)
			{
				keyValuePair.Value.presetButton.SetActive(true);
				keyValuePair.Value.typeIcon.SetActive(true);
			}
			else if (preset.ePreType.ToString() == clickBtnName.ToString())
			{
				keyValuePair.Value.presetButton.SetActive(true);
				keyValuePair.Value.typeIcon.SetActive(false);
			}
			else
			{
				keyValuePair.Value.presetButton.SetActive(false);
			}
		}
		this.AdjustTargetPosition(this.m_goPresetParent, this.m_presetScrollView);
	}

	public float GetScrollBarValue()
	{
		return this.m_presetScrollBar.value;
	}

	public void SetScrollBar(float scrollBarValue)
	{
		this.m_presetScrollBar.value = scrollBarValue;
	}

	public void InitFilterButtonActive(PresetMgr.Filter btnName)
	{
		if (btnName == PresetMgr.Filter.NotFilter)
		{
			btnName = PresetMgr.Filter.None;
		}
		this.SetFilterButtonActive(PresetMgr.Filter.NotFilter, btnName);
	}

	private void SetFilterButtonActive(PresetMgr.Filter clickBtnName, PresetMgr.Filter prevClickBtnName)
	{
		if (this.m_listBtnState.ContainsKey(clickBtnName))
		{
			PresetCtrl.FilterButton filterButton = this.m_listBtnState[clickBtnName];
			filterButton.m_btnButton.defaultColor = this.activeColor;
			filterButton.m_goSelectCursor.SetActive(true);
		}
		if (prevClickBtnName != PresetMgr.Filter.None)
		{
			PresetCtrl.FilterButton filterButton2 = this.m_listBtnState[prevClickBtnName];
			filterButton2.m_btnButton.defaultColor = this.inActiveColor;
			filterButton2.m_goSelectCursor.SetActive(false);
		}
	}

	public void PresetSet()
	{
		if (this.m_maid.IsBusy)
		{
			return;
		}
		string name = UIButton.current.name;
		PresetCtrl.PresetButton presetButton;
		if (this.m_dicPresetButton.TryGetValue(name, out presetButton))
		{
			CharacterMgr.Preset preset = presetButton.preset;
			GameMain.Instance.CharacterMgr.PresetSet(this.m_maid, preset);
		}
		else
		{
			Debug.LogError(string.Format("不適切なプリセットが選択されました。プリセット名={0}", name));
		}
	}

	public void DeletePreset(string clickBtnName)
	{
		this.m_presetName = clickBtnName;
		GameMain.Instance.SysDlg.ShowFromLanguageTerm("Dialog/プリセットを削除して宜しいですか。", SystemDialog.TYPE.OK_CANCEL, new SystemDialog.OnClick(this.DeletePreset), null);
	}

	private void DeletePreset()
	{
		GameMain.Instance.SysDlg.Close();
		PresetCtrl.PresetButton presetButton;
		if (this.m_dicPresetButton.TryGetValue(this.m_presetName, out presetButton))
		{
			float scrollBarValue = BaseMgr<PresetMgr>.Instance.GetScrollBarValue();
			CharacterMgr.Preset preset = presetButton.preset;
			GameMain.Instance.CharacterMgr.PresetDelete(preset);
			BaseMgr<PresetMgr>.Instance.UpdatePresetList();
			if (PresetMgr.m_currentActiveFilterBtnName != PresetMgr.Filter.None)
			{
				BaseMgr<PresetMgr>.Instance.UpdateFilterPreset(PresetMgr.m_currentActiveFilterBtnName);
				this.SetScrollBar(scrollBarValue);
			}
			Debug.Log(string.Format("プリセット「{0}」を削除しました。", this.m_presetName));
		}
	}

	private Maid m_maid;

	private GameObject m_goPresetPanel;

	private GameObject m_goPresetParent;

	private UIScrollView m_presetScrollView;

	private UIScrollBar m_presetScrollBar;

	private Dictionary<string, PresetCtrl.PresetButton> m_dicPresetButton;

	private Dictionary<PresetMgr.Filter, PresetCtrl.FilterButton> m_listBtnState;

	private UnityEngine.Object m_presetPrefab;

	private Color activeColor;

	private Color inActiveColor;

	private string m_presetName;

	private const string PRESET_UNIT_PARENT_PATH = "PresetViewer/Contents/PresetUnitParent";

	private const string PRESET_UNIT_PREFAB_PATH = "SceneEdit/Preset/Prefabs/PresetUnit";

	private const string TYPE_ICON_ALL = "cm3d2_edit_priset_kindicon_clothes_body";

	private const string TYPE_ICON_WEAR = "cm3d2_edit_priset_kindicon_clothes";

	private const string TYPE_ICON_BODY = "cm3d2_edit_priset_kindicon_body";

	public class FilterButton
	{
		public PresetMgr.Filter m_name { get; set; }

		public UIButton m_btnButton { get; set; }

		public GameObject m_goSelectCursor { get; set; }
	}

	public class PresetButton
	{
		public string id { get; set; }

		public GameObject presetButton { get; set; }

		public CharacterMgr.Preset preset { get; set; }

		public GameObject typeIcon { get; set; }
	}
}
