using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard;

public class BattleStageChoiceWindow : MonoBehaviour
{
	public class PageData
	{
		public readonly GameObject Obj;

		public BattleStageChoiceObject[] StageList;

		public PageData(BattleStageChoiceObject[] stageList, GameObject obj)
		{
			StageList = stageList;
			Obj = obj;
		}
	}

	public class StageData
	{
		public GameObject StageDataObject;

		public string TextureName;

		public StageData(GameObject obj, string textureName)
		{
			StageDataObject = obj;
			TextureName = textureName;
		}
	}

	private bool _initializeEnd;

	[SerializeField]
	private UIButton _leftButton;

	[SerializeField]
	private UIButton _rightButton;

	[SerializeField]
	private UIButton _allOffButton;

	[SerializeField]
	private BoxCollider _flickCollider;

	[SerializeField]
	private UIGrid _radioIconsGrid;

	[SerializeField]
	private UIGrid _deckTableOriginal;

	[SerializeField]
	private GameObject _deckTableRoot;

	private List<UIGrid> _stagePageList = new List<UIGrid>();

	[SerializeField]
	private BattleStageChoiceObject _battleTageSelectOriginal;

	[SerializeField]
	private UISprite _radioIconOriginal;

	[SerializeField]
	private GameObject _allOnLabel;

	[SerializeField]
	private GameObject _allOffLabel;

	private int _currentPage;

	private bool _isChangePage;

	private float _timeChangePage;

	private List<UISprite> _radioIconClones;

	private List<PageData> _pageList = new List<PageData>();

	private int _maxSelectStageNum;

	private bool _isScroll = true;

	private int _loadDoneStageTextureNum;

	private const int MAXNUM_STAGE_PER_TABLE = 9;

	private List<string> _loadedResourceList = new List<string>();

	public bool[] SettingOffStageIndexs { get; private set; }

	public Action<bool> OnAllOff { get; set; }

	private void Update()
	{
		if (_isChangePage)
		{
			_timeChangePage += Time.deltaTime;
			if (_timeChangePage >= 0.2f)
			{
				_isChangePage = false;
				_timeChangePage = 0f;
			}
		}
	}

	public IEnumerator LoadResource(Action onLoadEnd)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		ResourcesManager resourcesManager = Toolbox.ResourcesManager;
		for (int i = 0; i < Data.Load.data.OpenBattleFieldIdList.Count; i++)
		{
			string path = "BattleStage_" + int.Parse(Data.Load.data.OpenBattleFieldIdList[i]).ToString("00");
			_loadedResourceList.Add(resourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.BattlePass));
		}
		yield return StartCoroutine(resourcesManager.LoadAssetGroupAsync(_loadedResourceList, null));
		UIManager.GetInstance().closeInSceneCenterLoading();
		onLoadEnd.Call();
	}

	public void Initialize()
	{
		if (_initializeEnd)
		{
			return;
		}
		_initializeEnd = true;
		PlayerPrefsWrapper.TurnOnFirsStageIfStageIdListAllOff();
		_maxSelectStageNum = Data.Load.data.OpenBattleFieldIdList.Count;
		if (_maxSelectStageNum <= 9)
		{
			_isScroll = false;
		}
		else
		{
			_isScroll = true;
		}
		if (_isScroll)
		{
			UIEventListener uIEventListener = UIEventListener.Get(_flickCollider.gameObject);
			uIEventListener.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uIEventListener.onDrag, new UIEventListener.VectorDelegate(OnDragPanel));
			UIEventListener uIEventListener2 = UIEventListener.Get(_rightButton.gameObject);
			uIEventListener2.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onClick, (UIEventListener.VoidDelegate)delegate
			{
				NextPage();
			});
			UIEventListener uIEventListener3 = UIEventListener.Get(_leftButton.gameObject);
			uIEventListener3.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener3.onClick, (UIEventListener.VoidDelegate)delegate
			{
				PrevPage();
			});
		}
		UIEventListener uIEventListener4 = UIEventListener.Get(_allOffButton.gameObject);
		uIEventListener4.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener4.onClick, (UIEventListener.VoidDelegate)delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			if (IsAllOff())
			{
				for (int i = 0; i < SettingOffStageIndexs.Length; i++)
				{
					SettingOffStageIndexs[i] = false;
				}
				_allOnLabel.gameObject.SetActive(value: false);
				_allOffLabel.gameObject.SetActive(value: true);
				OnAllOff(obj: false);
			}
			else
			{
				for (int j = 0; j < SettingOffStageIndexs.Length; j++)
				{
					SettingOffStageIndexs[j] = true;
				}
				_allOnLabel.gameObject.SetActive(value: true);
				_allOffLabel.gameObject.SetActive(value: false);
				OnAllOff(obj: true);
			}
			UpdateStageOffView();
		});
		_radioIconClones = new List<UISprite>();
		SettingOffStageIndexs = new bool[_maxSelectStageNum];
		int num = _maxSelectStageNum;
		_loadDoneStageTextureNum = 0;
		int num2 = 1 + num / 9;
		for (int num3 = 0; num3 < num2; num3++)
		{
			int num4 = num;
			if (num4 >= 9)
			{
				num4 = 9;
			}
			AddStageTable(num3, num4);
			num -= 9;
			if (num <= 0)
			{
				break;
			}
		}
		if (!_isScroll)
		{
			_leftButton.gameObject.SetActive(value: false);
			_rightButton.gameObject.SetActive(value: false);
		}
		for (int num5 = 0; num5 < _stagePageList.Count; num5++)
		{
			_stagePageList[num5].Reposition();
		}
		foreach (PageData page in _pageList)
		{
			page.Obj.SetActive(value: false);
		}
		_pageList[_currentPage].Obj.SetActive(value: true);
		SettingOffStageIndexs = ConvertSaveDataToStageIndexList();
		UpdateStageOffView();
		UpdateAllOnOffLabel();
		UpdateRadioButtonView();
	}

	private bool[] ConvertSaveDataToStageIndexList()
	{
		string[] array = PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.OFF_STAGE_ID).Split(',');
		bool[] array2 = new bool[_maxSelectStageNum];
		for (int i = 0; i < array.Length; i++)
		{
			for (int j = 0; j < _maxSelectStageNum; j++)
			{
				if (array[i] == Data.Load.data.OpenBattleFieldIdList[j].ToString())
				{
					array2[j] = true;
					break;
				}
			}
		}
		return array2;
	}

	public void SaveSetting()
	{
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.OFF_STAGE_ID, PlayerPrefsWrapper.ConvertStageIdListToSaveData(SettingOffStageIndexs));
	}

	private void UpdateStageOffView()
	{
		int num = 0;
		foreach (PageData page in _pageList)
		{
			BattleStageChoiceObject[] stageList = page.StageList;
			foreach (BattleStageChoiceObject battleStageChoiceObject in stageList)
			{
				if (SettingOffStageIndexs[num])
				{
					battleStageChoiceObject.SettingOffSelect(isOff: true);
				}
				else
				{
					battleStageChoiceObject.SettingOffSelect(isOff: false);
				}
				num++;
			}
		}
	}

	private void UpdateAllOnOffLabel()
	{
		if (IsAllOff())
		{
			_allOnLabel.gameObject.SetActive(value: true);
			_allOffLabel.gameObject.SetActive(value: false);
			OnAllOff(obj: true);
		}
		else
		{
			_allOnLabel.gameObject.SetActive(value: false);
			_allOffLabel.gameObject.SetActive(value: true);
			OnAllOff(obj: false);
		}
	}

	private bool IsAllOff()
	{
		bool result = true;
		for (int i = 0; i < _maxSelectStageNum; i++)
		{
			if (!SettingOffStageIndexs[i])
			{
				result = false;
				break;
			}
		}
		return result;
	}

	private void AddStageTable(int tableIndex, int createNum)
	{
		BattleStageChoiceObject[] array = new BattleStageChoiceObject[createNum];
		UIGrid uIGrid = UnityEngine.Object.Instantiate(_deckTableOriginal);
		uIGrid.transform.parent = _deckTableRoot.transform;
		uIGrid.transform.localScale = Vector3.one;
		uIGrid.transform.localPosition = Vector3.zero;
		int num = 0;
		uIGrid.sorting = UIGrid.Sorting.Custom;
		_stagePageList.Add(uIGrid);
		for (int i = 0; i < createNum; i++)
		{
			string textureId = Data.Load.data.OpenBattleFieldIdList[_loadDoneStageTextureNum];
			BattleStageChoiceObject battleStageChoiceObject = CreateStageFrame(tableIndex * 9 + i, textureId);
			_loadDoneStageTextureNum++;
			uIGrid.AddChild(battleStageChoiceObject.transform);
			battleStageChoiceObject.transform.localScale = Vector3.one;
			battleStageChoiceObject.gameObject.name = (num + i).ToString();
			array[i] = battleStageChoiceObject;
		}
		if (_isScroll)
		{
			UISprite uISprite = UnityEngine.Object.Instantiate(_radioIconOriginal);
			_radioIconClones.Add(uISprite);
			_radioIconsGrid.AddChild(uISprite.transform);
			uISprite.transform.localScale = Vector3.one;
		}
		PageData item = new PageData(array, uIGrid.gameObject);
		_pageList.Add(item);
	}

	private BattleStageChoiceObject CreateStageFrame(int index, string textureId)
	{
		BattleStageChoiceObject battleStageChoiceObject = UnityEngine.Object.Instantiate(_battleTageSelectOriginal);
		battleStageChoiceObject.Init();
		UIEventListener uIEventListener = UIEventListener.Get(battleStageChoiceObject.gameObject);
		uIEventListener.onPress = (UIEventListener.BoolDelegate)Delegate.Combine(uIEventListener.onPress, (UIEventListener.BoolDelegate)delegate(GameObject g, bool b)
		{
			StartCoroutine(PushedAnimation(g));
		});
		battleStageChoiceObject._onButton = (Action)Delegate.Combine(battleStageChoiceObject._onButton, (Action)delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			SettingOffStageIndexs[index] = !SettingOffStageIndexs[index];
			UpdateStageOffView();
			UpdateAllOnOffLabel();
		});
		ResourcesManager resourcesManager = Toolbox.ResourcesManager;
		string path = "BattleStage_" + int.Parse(textureId).ToString("00");
		Texture texture = resourcesManager.LoadObject<Texture>(resourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.BattleStage, isfetch: true));
		battleStageChoiceObject.SettingTexture(texture);
		UIEventListener uIEventListener2 = UIEventListener.Get(battleStageChoiceObject.gameObject);
		uIEventListener2.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uIEventListener2.onDrag, new UIEventListener.VectorDelegate(OnDragPanel));
		return battleStageChoiceObject;
	}

	private void OnDragPanel(GameObject obj, Vector2 dir)
	{
		if (_isScroll)
		{
			if (dir.x >= 70f)
			{
				PrevPage();
			}
			else if (dir.x <= -70f)
			{
				NextPage();
			}
		}
	}

	private void NextPage()
	{
		if (ChangePage(_currentPage + 1))
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		}
	}

	private void PrevPage()
	{
		if (ChangePage(_currentPage - 1))
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		}
	}

	private bool ChangePage(int newPage, bool isImmediate = false)
	{
		int currentPage = _currentPage;
		if (_isChangePage)
		{
			return false;
		}
		int count = _radioIconClones.Count;
		bool flag = false;
		if (newPage < 0)
		{
			newPage = count - 1;
			flag = true;
		}
		if (newPage >= count)
		{
			newPage = 0;
			flag = true;
		}
		if (currentPage == newPage)
		{
			isImmediate = true;
		}
		_currentPage = newPage;
		foreach (PageData page in _pageList)
		{
			page.Obj.SetActive(value: false);
		}
		_pageList[_currentPage].Obj.SetActive(value: true);
		_pageList[currentPage].Obj.SetActive(value: true);
		float num = ((_currentPage < currentPage) ? 1400f : (-1400f));
		if (flag)
		{
			num = 0f - num;
		}
		Vector3 pos = new Vector3(num, 0f, 0f);
		Vector3 localPosition = new Vector3(0f - num, 0f, 0f);
		Vector3 zero = Vector3.zero;
		_pageList[_currentPage].Obj.transform.localPosition = localPosition;
		TweenPosition.Begin(_pageList[currentPage].Obj, isImmediate ? 0f : 0.2f, pos);
		TweenPosition.Begin(_pageList[_currentPage].Obj, isImmediate ? 0f : 0.2f, zero);
		if (!isImmediate)
		{
			_isChangePage = true;
		}
		UpdateRadioButtonView();
		bool flag2;
		bool active = (flag2 = _pageList.Count >= 2) || flag2;
		_radioIconsGrid.gameObject.SetActive(active);
		return true;
	}

	private void UpdateRadioButtonView()
	{
		for (int i = 0; i < _radioIconClones.Count; i++)
		{
			if (i == _currentPage)
			{
				_radioIconClones[i].spriteName = _radioIconClones[i].spriteName.Replace("_off", "_on");
			}
			else
			{
				_radioIconClones[i].spriteName = _radioIconClones[i].spriteName.Replace("_on", "_off");
			}
		}
	}

	private IEnumerator PushedAnimation(GameObject obj)
	{
		TweenScale scl = obj.GetComponent<TweenScale>();
		if ((bool)scl)
		{
			scl.PlayForward();
			while (Input.GetMouseButton(0))
			{
				yield return null;
			}
			scl.PlayReverse();
		}
	}

	private void OnDestroy()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResourceList);
		_loadedResourceList = null;
	}
}
