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

namespace Wizard;

public class PracticePuzzleUI : UIBase
{
	private const int INVALID_GROUP_ID = 0;

	private const string BG_TEXTURE_PATH = "bg_puzzle";

	private const int CHARACTER_ID = 3704;

	[SerializeField]
	private UITexture _character;

	[SerializeField]
	private SimpleScrollViewUI _puzzleListScrollView;

	[SerializeField]
	private UIButton _decideButton;

	[SerializeField]
	private UIButton _missionButton;

	[SerializeField]
	private UITexture _backGround;

	[SerializeField]
	private GameObject _puzzleMissionDialogPrefab;

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

	private List<PracticePuzzleData> _puzzleListData;

	private PracticePuzzleData _selectData;

	public static void ClearInMyPageScene()
	{
		GameMgr.GetIns().GetDataMgr().PracticePuzzleGroupId = 0;
	}

	public override void onFirstStart()
	{
		base.onFirstStart();
		Data.MyPageNotifications.data.IsPracticePuzzleBadgeEnable = false;
		UIManager.GetInstance().ShowFooterMenu(isShow: true);
		UIManager.GetInstance()._Footer.UpdateCurrentIndex(1);
		UIManager.GetInstance()._Footer.UpdateSoloPlayBadgeIcon();
		InitializeTopBar();
		UIEventListener.Get(_decideButton.gameObject).onClick = delegate
		{
			OnClickDecideButton();
		};
		UIEventListener.Get(_missionButton.gameObject).onClick = delegate
		{
			OnClickMissionButton();
		};
		PracticePuzzleInfoTask task = new PracticePuzzleInfoTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			_puzzleListData = task.PuzzleDataList;
			LoadResource(delegate
			{
				InitializeCharacterTexture();
				InitializeBackGround();
				InitializeButtonEffect();
				_puzzleListScrollView.CreateScrollView(_puzzleListData.Count, InitializePlate);
				int defaultScrollIndex = GetDefaultScrollIndex(_puzzleListData);
				_puzzleListScrollView.MovePlateByIndex(defaultScrollIndex, SimpleScrollViewUI.VerticalMovement.Center);
				_selectData = _puzzleListData[defaultScrollIndex];
				UpdateSelectCursor(_selectData);
				UIManager.GetInstance().OnReadyViewScene(isFadein: true);
			});
		}));
	}

	private int GetDefaultScrollIndex(List<PracticePuzzleData> list)
	{
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].GroupdId == GameMgr.GetIns().GetDataMgr().PracticePuzzleGroupId)
			{
				return i;
			}
		}
		return 0;
	}

	protected override void OnDestroy()
	{
		base.OnDestroy();
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadFileList);
		_loadFileList.Clear();
	}

	private void LoadResource(Action onFinish)
	{
		_loadFileList.Add(GetCharacterTexturePath(isFetch: false));
		_loadFileList.Add(GetBackGroundTexturePath(isFetch: false));
		_loadFileList.Add(Toolbox.ResourcesManager.GetAssetTypePath("cmn_ui_btn_1", ResourcesManager.AssetLoadPathType.Effect2D));
		foreach (PracticePuzzleData puzzleListDatum in _puzzleListData)
		{
			_loadFileList.Add(PracticePuzzlePlate.GetPlateTexturePath(puzzleListDatum, isFetch: false));
		}
		StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadFileList, delegate
		{
			onFinish.Call();
		}));
	}

	private string GetBackGroundTexturePath(bool isFetch)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath("bg_puzzle", ResourcesManager.AssetLoadPathType.Background, isFetch);
	}

	private void InitializeButtonEffect()
	{
		EffectUtility.CreateEffect2D(new Effect2dCreateParam
		{
			Parent = _decideButton.gameObject,
			EffectName = "cmn_ui_btn_1",
			ColorCode = eColorCodeId.DECISION_BTN_2_COLOR,
			InitActive = false,
			UnloadAssetList = _loadFileList
		}).SetActive(value: true);
	}

	private void InitializePlate(int index, GameObject plateObject)
	{
		plateObject.GetComponent<PracticePuzzlePlate>().UpdateView(_puzzleListData[index], OnSelectPuzzle, _puzzleListData[index] == _selectData);
	}

	private void InitializeBackGround()
	{
		_backGround.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(GetBackGroundTexturePath(isFetch: true));
	}

	private void OnSelectPuzzle(PracticePuzzleData data)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
		_selectData = data;
		UpdateSelectCursor(data);
	}

	private void UpdateSelectCursor(PracticePuzzleData selectData)
	{
		foreach (GameObject activePlate in _puzzleListScrollView.ActivePlateList)
		{
			PracticePuzzlePlate component = activePlate.GetComponent<PracticePuzzlePlate>();
			component.UpdateSelectSpriteVisible(component.PuzzleData == selectData);
		}
	}

	private string GetCharacterTexturePath(bool isFetch)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath(3704.ToString("00"), ResourcesManager.AssetLoadPathType.ClassCharaBase, isFetch);
	}

	private void InitializeCharacterTexture()
	{
		_character.mainTexture = Toolbox.ResourcesManager.LoadObject(GetCharacterTexturePath(isFetch: true)) as Texture;
	}

	private void InitializeTopBar()
	{
		UIManager.ChangeViewSceneParam changeViewSceneParam = new UIManager.ChangeViewSceneParam();
		changeViewSceneParam.MyPageMenuIndex = 1;
		changeViewSceneParam.IsCutCardMotion = true;
		changeViewSceneParam.OnFinishChangeView = delegate
		{
			MyPageMenu.Instance.GoToPracticeTypeSelect();
		};
		string titleMsg = Data.SystemText.Get("Mission_0095");
		UIManager.GetInstance().CreateTopBar(base.gameObject, titleMsg, UIManager.ViewScene.MyPage, MoneyDraw: false, changeViewSceneParam).gameObject.layer = LayerMask.NameToLayer("FrontUI");
	}

	private void OnClickDecideButton()
	{
		if (_selectData == null)
		{
			return;
		}
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		PracticePuzzleListTask task = new PracticePuzzleListTask();
		task.SetParameter(_selectData.GroupdId);
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			List<string> loadList = PuzzleQuestSelectDialog.CollectResourcePath(task.PuzzleQuestInfo.DisplayDatas);
			UIManager.GetInstance().createInSceneCenterLoading();
			StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadList, delegate
			{
				UIManager.GetInstance().closeInSceneCenterLoading();
				PuzzleQuestSelectDialog.CreateDialog(task.PuzzleQuestInfo, delegate(PuzzleQuestData puzzleData, int difficulty)
				{
					StartBattle(_selectData.GroupdId, puzzleData, difficulty);
				}).OnClose = delegate
				{
					Toolbox.ResourcesManager.RemoveAssetGroup(loadList);
				};
			}));
		}));
	}

	public static void StartBattle(int groupId, PuzzleQuestData data, int difficulty)
	{
		PuzzleUtil.SetPracticePuzzleData(groupId, data, difficulty, DataMgr.BattleType.Practice);
		PuzzleUtil.ChangeSceneToPracticePuzzle(data);
	}

	private void OnClickMissionButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		PracticePuzzleMissionListTask puzzleMissionTask = new PracticePuzzleMissionListTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(puzzleMissionTask, delegate
		{
			PracticePuzzleMissionDialog.Create(puzzleMissionTask.MissionData, _puzzleMissionDialogPrefab);
		}));
	}
}
