using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MainBusinessWindow : MonoBehaviour
{
	private uGUICanvasFade UGUICanvas
	{
		get
		{
			if (this.m_uGUICanvasFade == null)
			{
				this.m_uGUICanvasFade = base.GetComponent<uGUICanvasFade>();
			}
			return this.m_uGUICanvasFade;
		}
	}

	private void Start()
	{
		if (base.gameObject.activeSelf)
		{
			base.gameObject.SetActive(false);
		}
		if (0f < this.UGUICanvas.alpha)
		{
			this.UGUICanvas.alpha = 0f;
		}
		this.m_UIListBanner.tempItem.AddComponent<MainBusinessWindow.BannerEnterHandler>();
	}

	public void OpenBusinessWindow(Action callback)
	{
		this.m_CallbackCloseWindow = callback;
		this.m_CallbackCloseWindowAtArg = null;
		this.OpenBusinessWindow();
	}

	public void OpenBusinessWindow(Action<bool, string> callback)
	{
		this.m_CallbackCloseWindow = null;
		this.m_CallbackCloseWindowAtArg = callback;
		this.OpenBusinessWindow();
	}

	public void OpenBusinessWindow()
	{
		MainBusinessWindow.<OpenBusinessWindow>c__AnonStorey0 <OpenBusinessWindow>c__AnonStorey = new MainBusinessWindow.<OpenBusinessWindow>c__AnonStorey0();
		<OpenBusinessWindow>c__AnonStorey.$this = this;
		this.UGUICanvas.interactable = false;
		this.UGUICanvas.blocksRaycasts = true;
		this.UGUICanvas.FadeIn(0.5f, delegate
		{
			<OpenBusinessWindow>c__AnonStorey.$this.UGUICanvas.interactable = true;
		});
		this.m_LastSelectToggle = null;
		<OpenBusinessWindow>c__AnonStorey.nowBusinessTypeID = GameMain.Instance.FacilityMgr.NextDayBusinessTypeID;
		if (!FacilityDataTable.IsExistFacilityBusinessTypeData(<OpenBusinessWindow>c__AnonStorey.nowBusinessTypeID))
		{
			<OpenBusinessWindow>c__AnonStorey.nowBusinessTypeID = GameMain.Instance.FacilityMgr.NowBusinessTypeID;
		}
		this.m_NowSelectingBusinessTypeID = <OpenBusinessWindow>c__AnonStorey.nowBusinessTypeID;
		this.m_BeforeSelectingBusinessTypeID = <OpenBusinessWindow>c__AnonStorey.nowBusinessTypeID;
		<OpenBusinessWindow>c__AnonStorey.businessTypeDataArray = FacilityDataTable.GetFacilityBusinessTypeDataArray(true);
		this.m_UIListBanner.Show<Toggle>(<OpenBusinessWindow>c__AnonStorey.businessTypeDataArray.Length, delegate(int index, Toggle toggle)
		{
			FacilityDataTable.BusinessTypeData data = <OpenBusinessWindow>c__AnonStorey.businessTypeDataArray[index];
			if (data.ID == <OpenBusinessWindow>c__AnonStorey.nowBusinessTypeID)
			{
				toggle.isOn = true;
			}
			if (toggle.image)
			{
				Sprite spriteInAtlas = <OpenBusinessWindow>c__AnonStorey.$this.GetSpriteInAtlas(<OpenBusinessWindow>c__AnonStorey.$this.m_UIAtlasBusinessBannger, "main_banner_dammy");
				if (spriteInAtlas != null)
				{
					toggle.image.sprite = spriteInAtlas;
				}
			}
			toggle.onValueChanged.AddListener(delegate(bool value)
			{
				if (!value)
				{
					return;
				}
				<OpenBusinessWindow>c__AnonStorey.OnClickBusinessBanner(toggle, data.ID);
			});
			MainBusinessWindow.BannerEnterHandler component = toggle.GetComponent<MainBusinessWindow.BannerEnterHandler>();
			component.onPointerEnter = delegate()
			{
				<OpenBusinessWindow>c__AnonStorey.UpdateBusinessTypeDescription(data.ID);
				<OpenBusinessWindow>c__AnonStorey.UpdateBusinessTypeConditions(data.ID);
			};
			component.onPointerExit = delegate()
			{
				<OpenBusinessWindow>c__AnonStorey.UpdateBusinessTypeDescription(<OpenBusinessWindow>c__AnonStorey.m_NowSelectingBusinessTypeID);
				<OpenBusinessWindow>c__AnonStorey.UpdateBusinessTypeConditions(<OpenBusinessWindow>c__AnonStorey.m_NowSelectingBusinessTypeID);
			};
		});
		this.UpdateBusinessTypeDescription(this.m_NowSelectingBusinessTypeID);
		this.UpdateBusinessTypeConditions(this.m_NowSelectingBusinessTypeID);
	}

	private void OnClickBusinessBanner(Toggle toggle, int businessTypeID)
	{
		bool flag = this.m_LastSelectToggle == toggle;
		bool flag2 = this.m_NowSelectingBusinessTypeID != businessTypeID;
		bool flag3 = this.IsSatisfyTheConditions(businessTypeID);
		if (!flag || !flag3)
		{
			this.UpdateBusinessTypeDescription(businessTypeID);
			this.UpdateBusinessTypeConditions(businessTypeID);
		}
		if (flag3)
		{
			this.m_NowSelectingBusinessTypeID = businessTypeID;
		}
		this.m_LastSelectToggle = toggle;
	}

	private void UpdateBusinessTypeDescription(int businessTypeID)
	{
		FacilityDataTable.BusinessTypeData facilityBusinessTypeData = FacilityDataTable.GetFacilityBusinessTypeData(businessTypeID);
		this.m_UITextDescription.text = facilityBusinessTypeData.description;
	}

	private void UpdateBusinessTypeConditions(int businessTypeID)
	{
		FacilityDataTable.BusinessTypeData facilityBusinessTypeData = FacilityDataTable.GetFacilityBusinessTypeData(businessTypeID);
		StringBuilder stringBuilder = new StringBuilder();
		string[] conditions = facilityBusinessTypeData.conditions;
		for (int i = 0; i < conditions.Length; i++)
		{
			string text = conditions[i];
			if (!string.IsNullOrEmpty(text))
			{
				stringBuilder.AppendFormat("{0}. {1}\n", i + 1, text);
			}
		}
		this.m_UITextConditions.text = stringBuilder.ToString();
	}

	private bool IsSatisfyTheConditions(int businessTypeID)
	{
		FacilityDataTable.BusinessTypeData facilityBusinessTypeData = FacilityDataTable.GetFacilityBusinessTypeData(businessTypeID);
		bool flag = true;
		flag &= GameMain.Instance.FacilityMgr.IsExistBusinessTypeFacility(businessTypeID);
		return true;
	}

	public void UpdateBusinessType()
	{
		GameMain.Instance.FacilityMgr.NextDayBusinessTypeID = this.m_NowSelectingBusinessTypeID;
	}

	public void CloseBusinessWindow()
	{
		if (this.m_CallbackCloseWindow != null)
		{
			this.m_CallbackCloseWindow();
			this.m_CallbackCloseWindow = null;
		}
		if (this.m_CallbackCloseWindowAtArg != null)
		{
			FacilityManager facilityMgr = GameMain.Instance.FacilityMgr;
			FacilityDataTable.BusinessTypeData facilityBusinessTypeData = FacilityDataTable.GetFacilityBusinessTypeData(facilityMgr.NowBusinessTypeID);
			bool arg = this.m_BeforeSelectingBusinessTypeID != this.m_NowSelectingBusinessTypeID;
			string fileName = facilityBusinessTypeData.fileName;
			this.m_CallbackCloseWindowAtArg(arg, fileName);
			this.m_CallbackCloseWindowAtArg = null;
		}
		this.UGUICanvas.interactable = false;
		this.UGUICanvas.FadeOut(0.5f, delegate
		{
			this.UGUICanvas.blocksRaycasts = false;
		});
	}

	private Sprite GetSpriteInAtlas(UIAtlas atlas, string resourceName)
	{
		Sprite sprite = null;
		if (this.m_SpriteDic.TryGetValue(resourceName, out sprite))
		{
			return sprite;
		}
		sprite = uGUIUtility.GetSpriteInAtlas(atlas, resourceName);
		this.m_SpriteDic.Add(resourceName, sprite);
		return sprite;
	}

	private void OnDestroy()
	{
		if (this == null)
		{
			return;
		}
		if (base.gameObject == null)
		{
			return;
		}
		if (this.m_SpriteDic != null)
		{
			int count = this.m_SpriteDic.Count;
			List<Sprite> list = new List<Sprite>(this.m_SpriteDic.Values);
			for (int i = count - 1; i >= 0; i--)
			{
				UnityEngine.Object.Destroy(list[i]);
			}
			this.m_SpriteDic.Clear();
			list.Clear();
		}
	}

	[SerializeField]
	private uGUIListViewer m_UIListBanner;

	[SerializeField]
	private Text m_UITextDescription;

	[SerializeField]
	private Text m_UITextConditions;

	private int m_NowSelectingBusinessTypeID;

	private int m_BeforeSelectingBusinessTypeID;

	private Toggle m_LastSelectToggle;

	private uGUICanvasFade m_uGUICanvasFade;

	private Action m_CallbackCloseWindow;

	private Action<bool, string> m_CallbackCloseWindowAtArg;

	[SerializeField]
	private UIAtlas m_UIAtlasBusinessBannger;

	private Dictionary<string, Sprite> m_SpriteDic = new Dictionary<string, Sprite>();

	private class BannerEnterHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IEventSystemHandler
	{
		private uGUISelectableAnimation selectableAnimation
		{
			get
			{
				if (this.m_SelectableAnimation == null)
				{
					this.m_SelectableAnimation = base.gameObject.GetComponentInChildren<uGUISelectableAnimation>();
				}
				return this.m_SelectableAnimation;
			}
		}

		private Toggle toggle
		{
			get
			{
				if (this.m_Toggle == null)
				{
					this.m_Toggle = base.gameObject.GetComponentInChildren<Toggle>();
				}
				return this.m_Toggle;
			}
		}

		private void Start()
		{
			if (this.IsSelecting() && this.selectableAnimation.enabled)
			{
				this.selectableAnimation.enabled = false;
			}
		}

		public void OnPointerEnter(PointerEventData eventData)
		{
			if (this.IsSelecting())
			{
				if (this.selectableAnimation.enabled)
				{
					this.selectableAnimation.enabled = false;
				}
				return;
			}
			if (!this.selectableAnimation.enabled)
			{
				this.selectableAnimation.enabled = true;
				this.selectableAnimation.OnPointerEnter(eventData);
			}
			if (this.onPointerEnter != null)
			{
				this.onPointerEnter();
			}
		}

		public void OnPointerExit(PointerEventData eventData)
		{
			if (this.IsSelecting())
			{
				if (this.selectableAnimation.enabled)
				{
					this.selectableAnimation.enabled = false;
				}
				return;
			}
			if (!this.selectableAnimation.enabled)
			{
				this.selectableAnimation.enabled = true;
				this.selectableAnimation.OnPointerExit(eventData);
			}
			if (this.onPointerExit != null)
			{
				this.onPointerExit();
			}
		}

		public void OnPointerClick(PointerEventData eventData)
		{
			if (this.IsSelecting())
			{
				if (this.selectableAnimation.enabled)
				{
					this.selectableAnimation.enabled = false;
				}
				return;
			}
			if (!this.selectableAnimation.enabled)
			{
				this.selectableAnimation.enabled = true;
				this.selectableAnimation.OnPointerClick(eventData);
			}
		}

		private bool IsSelecting()
		{
			bool flag = false;
			flag |= (EventSystem.current.currentSelectedGameObject == base.gameObject);
			return flag | this.toggle.isOn;
		}

		public Action onPointerEnter;

		public Action onPointerExit;

		private uGUISelectableAnimation m_SelectableAnimation;

		private Toggle m_Toggle;
	}
}
