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

public class LifeModeChangeWindow : MonoBehaviour
{
	private void UpdateNowSelectType()
	{
		this.m_NowSelectType = GameModeManager.nowGameMode;
	}

	private void SetNowSelectType(GameModeManager.Type type)
	{
		this.m_NowSelectType = type;
	}

	private void SetNowGameMode()
	{
		GameModeManager.nowGameMode = this.m_NowSelectType;
	}

	private uGUICanvasFade uGUICanvasFade
	{
		get
		{
			if (this.m_uGUICanvasFade == null)
			{
				this.m_uGUICanvasFade = base.GetComponent<uGUICanvasFade>();
			}
			return this.m_uGUICanvasFade;
		}
	}

	private void Init()
	{
		if (this.m_IsInit)
		{
			return;
		}
		this.m_IsInit = true;
		this.m_UIListBanner.tempItem.AddComponent<LifeModeChangeWindow.BannerEnterHandler>();
		this.m_ButtonClose.onClick.AddListener(new UnityAction(this.CloseWindow));
	}

	private void Start()
	{
		if (this.m_IsInit)
		{
			return;
		}
		this.Init();
		this.uGUICanvasFade.FadeOut(0f, null);
		base.gameObject.SetActive(false);
	}

	public void Open(Action callback)
	{
		this.Init();
		this.m_CallbackCloseWindow = callback;
		this.OpenWindow();
	}

	private void OpenWindow()
	{
		LifeModeChangeWindow.<OpenWindow>c__AnonStorey0 <OpenWindow>c__AnonStorey = new LifeModeChangeWindow.<OpenWindow>c__AnonStorey0();
		<OpenWindow>c__AnonStorey.$this = this;
		this.uGUICanvasFade.interactable = false;
		this.uGUICanvasFade.blocksRaycasts = true;
		this.uGUICanvasFade.alpha = 0f;
		this.uGUICanvasFade.FadeIn(0.3f, delegate
		{
			<OpenWindow>c__AnonStorey.$this.uGUICanvasFade.interactable = true;
		});
		this.UpdateNowSelectType();
		Dictionary<GameModeManager.Type, GameModeManager.Data> dataDic = GameModeManager.DataDic;
		<OpenWindow>c__AnonStorey.keyList = new List<GameModeManager.Type>(dataDic.Keys);
		<OpenWindow>c__AnonStorey.valueList = new List<GameModeManager.Data>(dataDic.Values);
		this.m_UIListBanner.Show<Toggle>(dataDic.Count, delegate(int index, Toggle toggle)
		{
			GameModeManager.Type dataType = <OpenWindow>c__AnonStorey.keyList[index];
			GameModeManager.Data data = <OpenWindow>c__AnonStorey.valueList[index];
			if (toggle.image != null)
			{
				Sprite spriteInAtlas = <OpenWindow>c__AnonStorey.$this.GetSpriteInAtlas(<OpenWindow>c__AnonStorey.$this.m_UIAtlasBusinessBannger, data.strBannerImageName);
				if (spriteInAtlas != null)
				{
					toggle.image.sprite = spriteInAtlas;
				}
			}
			if (dataType == <OpenWindow>c__AnonStorey.$this.m_NowSelectType)
			{
				toggle.isOn = true;
			}
			toggle.onValueChanged.AddListener(delegate(bool value)
			{
				if (!value)
				{
					return;
				}
				<OpenWindow>c__AnonStorey.SetNowSelectType(dataType);
				<OpenWindow>c__AnonStorey.UpdateBannerList();
			});
			LifeModeChangeWindow.BannerEnterHandler component = toggle.GetComponent<LifeModeChangeWindow.BannerEnterHandler>();
			component.onPointerEnter = delegate()
			{
				<OpenWindow>c__AnonStorey.UpdateDescription(data.strDescription);
			};
			component.onPointerExit = delegate()
			{
				<OpenWindow>c__AnonStorey.UpdateDescription(GameModeManager.GetData(<OpenWindow>c__AnonStorey.m_NowSelectType).strDescription);
			};
		});
		this.UpdateDescription(GameModeManager.GetData().strDescription);
		this.UpdateBannerList();
	}

	private void UpdateBannerList()
	{
		foreach (GameObject gameObject in this.m_UIListBanner.ItemArray)
		{
			LifeModeChangeWindow.BannerEnterHandler component = gameObject.GetComponent<LifeModeChangeWindow.BannerEnterHandler>();
			component.UpdateColorBlock();
			component.toggle.interactable = !component.toggle.isOn;
		}
	}

	private void UpdateDescription(string msg)
	{
		this.m_UITextDescription.text = msg;
	}

	public void CloseWindow()
	{
		this.SetNowGameMode();
		if (this.m_CallbackCloseWindow != null)
		{
			this.m_CallbackCloseWindow();
			this.m_CallbackCloseWindow = null;
		}
		this.uGUICanvasFade.interactable = false;
		this.uGUICanvasFade.FadeOut(0.3f, delegate
		{
			base.gameObject.SetActive(false);
			this.uGUICanvasFade.blocksRaycasts = false;
			this.uGUICanvasFade.alpha = 0f;
		});
	}

	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();
		}
	}

	private GameModeManager.Type m_NowSelectType;

	[SerializeField]
	private uGUIListViewer m_UIListBanner;

	[SerializeField]
	private Text m_UITextDescription;

	[SerializeField]
	private Button m_ButtonClose;

	private uGUICanvasFade m_uGUICanvasFade;

	private Action m_CallbackCloseWindow;

	private bool m_IsInit;

	[SerializeField]
	private UIAtlas m_UIAtlasBusinessBannger;

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

	private class BannerEnterHandler : UIBehaviour, IPointerEnterHandler, IPointerExitHandler, IEventSystemHandler
	{
		public Toggle toggle
		{
			get
			{
				if (this.m_Toggle == null)
				{
					this.m_Toggle = base.gameObject.GetComponentInChildren<Toggle>();
				}
				return this.m_Toggle;
			}
		}

		protected override void Start()
		{
			this.InitColorBlock();
		}

		private void InitColorBlock()
		{
			if (this.m_IsInitColorBlock)
			{
				return;
			}
			this.m_IsInitColorBlock = true;
			this.m_ColorBlockDefault = this.toggle.colors;
			this.m_ColorBlockSelected = this.toggle.colors;
			this.m_ColorBlockSelected.normalColor = this.m_ColorBlockSelected.highlightedColor;
			this.m_ColorBlockSelected.disabledColor = this.m_ColorBlockSelected.highlightedColor;
			this.m_ColorBlockSelected.pressedColor = this.m_ColorBlockSelected.highlightedColor;
		}

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

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

		private bool IsSelecting()
		{
			return this.toggle.isOn;
		}

		public void UpdateColorBlock()
		{
			this.InitColorBlock();
			bool flag = this.IsSelecting();
			this.toggle.colors = ((!flag) ? this.m_ColorBlockDefault : this.m_ColorBlockSelected);
		}

		public Action onPointerEnter;

		public Action onPointerExit;

		private ColorBlock m_ColorBlockDefault;

		private ColorBlock m_ColorBlockSelected;

		private Toggle m_Toggle;

		private bool m_IsInitColorBlock;
	}
}
