using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class FacilityUIPowerUpMaterialList : MonoBehaviour
{
	private FacilityManager FacilityMgr
	{
		get
		{
			if (this.m_FacilityMgr == null)
			{
				this.m_FacilityMgr = GameMain.Instance.FacilityMgr;
			}
			return this.m_FacilityMgr;
		}
	}

	public void Show(int id, Facility facility, Action<int[]> callbackSelect)
	{
		this.m_CallbackSelect = callbackSelect;
		this.SetupFacilityPowerUpMaterialListDropDown(id, facility);
	}

	private void SetupFacilityPowerUpMaterialListDropDown(int id, Facility facility)
	{
		FacilityUIPowerUpMaterialList.<SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey0 <SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey = new FacilityUIPowerUpMaterialList.<SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey0();
		<SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey.$this = this;
		this.m_uGUIListViewer.parentItemArea.gameObject.SetActive(true);
		this.DeleteDropdown();
		Facility.PowerUpRecipe facilityPowerUpRecipe = FacilityDataTable.GetFacilityPowerUpRecipe(id);
		<SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey.materialCategoryIDArray = facilityPowerUpRecipe.materialCategoryIDArray;
		<SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey.recipeData = facility.GetRecipeData(id);
		this.m_uGUIListViewer.Show<Transform>(<SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey.materialCategoryIDArray.Length, delegate(int i, Transform trans)
		{
			int num = <SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey.materialCategoryIDArray[i];
			Dropdown componentInChildren = trans.GetComponentInChildren<Dropdown>();
			Text[] componentsInChildren = trans.GetComponentsInChildren<Text>();
			FacilityUIPowerUpMaterialList.DropdownPowerUpMaterial dropdownPowerUpMaterial = trans.gameObject.AddComponent<FacilityUIPowerUpMaterialList.DropdownPowerUpMaterial>();
			string facilityPowerUpMaterialCategoryName = FacilityDataTable.GetFacilityPowerUpMaterialCategoryName(num);
			componentsInChildren[0].text = facilityPowerUpMaterialCategoryName;
			componentsInChildren[1].text = string.Format("CATEGORY{0}", i + 1);
			componentInChildren.ClearOptions();
			Facility.PowerUpMaterial[] materialArray = <SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey.$this.FacilityMgr.GetFacilityPowerUpItemEnableArray(num);
			if (materialArray.Length > 0)
			{
				for (int j = 0; j < materialArray.Length; j++)
				{
					componentInChildren.options.Add(new Dropdown.OptionData(materialArray[j].name));
					if (<SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey.recipeData.materialIDArray[i] == materialArray[j].id)
					{
						dropdownPowerUpMaterial.materialID = materialArray[j].id;
						componentInChildren.value = j;
					}
				}
			}
			else
			{
				componentInChildren.image.raycastTarget = false;
				dropdownPowerUpMaterial.materialID = 0;
				componentInChildren.options.Add(new Dropdown.OptionData("無し"));
			}
			if (dropdownPowerUpMaterial.materialID == 0)
			{
				if (materialArray.Length > 0)
				{
					dropdownPowerUpMaterial.materialID = materialArray[0].id;
					componentInChildren.value = 0;
				}
				else
				{
					dropdownPowerUpMaterial.materialID = -1;
				}
			}
			componentInChildren.RefreshShownValue();
			componentInChildren.onValueChanged.AddListener(delegate(int value)
			{
				dropdownPowerUpMaterial.materialID = materialArray[value].id;
				<SetupFacilityPowerUpMaterialListDropDown>c__AnonStorey.OnUpdateDropdownParams();
			});
			if (i == 0)
			{
				Transform transform = dropdownPowerUpMaterial.transform.Find("Image Plus Mark");
				if (transform)
				{
					transform.gameObject.SetActive(false);
				}
				else
				{
					Debug.LogError("[FacilityPowerUP]プラスマークのイメージが見つかりません\n「Image Plus Mark」という名前の子オブジェクトを追加してください");
				}
			}
		});
		this.OnUpdateDropdownParams();
	}

	public void HideAllDropdown()
	{
		Transform transform = this.m_uGUIListViewer.transform;
		Dropdown[] componentsInChildren = transform.GetComponentsInChildren<Dropdown>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].Hide();
		}
	}

	public void DeleteDropdown()
	{
		this.HideAllDropdown();
		this.m_uGUIListViewer.ResetList();
	}

	private void OnUpdateDropdownParams()
	{
		Transform transform = this.m_uGUIListViewer.transform;
		FacilityUIPowerUpMaterialList.DropdownPowerUpMaterial[] componentsInChildren = transform.GetComponentsInChildren<FacilityUIPowerUpMaterialList.DropdownPowerUpMaterial>();
		int[] array = new int[componentsInChildren.Length];
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			array[i] = componentsInChildren[i].materialID;
		}
		if (this.m_CallbackSelect != null)
		{
			this.m_CallbackSelect(array);
		}
	}

	private FacilityManager m_FacilityMgr;

	[SerializeField]
	private uGUIListViewer m_uGUIListViewer;

	private Action<int[]> m_CallbackSelect;

	private class DropdownPowerUpMaterial : MonoBehaviour, IPointerClickHandler, ISubmitHandler, IEventSystemHandler
	{
		private void SetupTemplateCanvas()
		{
			Dropdown component = base.GetComponent<Dropdown>();
			Transform transform = component.transform.Find("Dropdown List");
			if (!transform)
			{
				return;
			}
			Canvas component2 = transform.GetComponent<Canvas>();
			Canvas componentInParent = component.GetComponentInParent<Canvas>();
			component2.sortingOrder = componentInParent.sortingOrder + 50;
		}

		private void SetupBlockerCanvas()
		{
			Dropdown component = base.GetComponent<Dropdown>();
			Canvas componentInParent = component.GetComponentInParent<Canvas>();
			Transform transform = componentInParent.transform.Find("Blocker");
			if (!transform)
			{
				return;
			}
			Canvas component2 = transform.GetComponent<Canvas>();
			component2.sortingOrder = componentInParent.sortingOrder + 1;
			CanvasGroup canvasGroup = transform.gameObject.GetComponent<CanvasGroup>();
			if (!canvasGroup)
			{
				canvasGroup = transform.gameObject.AddComponent<CanvasGroup>();
			}
			canvasGroup.ignoreParentGroups = true;
			canvasGroup.interactable = true;
			canvasGroup.blocksRaycasts = true;
		}

		public void OnPointerClick(PointerEventData data)
		{
			this.SetupTemplateCanvas();
			this.SetupBlockerCanvas();
		}

		public void OnSubmit(BaseEventData data)
		{
			this.SetupTemplateCanvas();
			this.SetupBlockerCanvas();
		}

		public int materialID;
	}
}
