using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard.Replay;

namespace Wizard;

public class ReplayDialogContent : MonoBehaviour
{
	[SerializeField]
	private GameObject _rootReplayContentView;

	[SerializeField]
	private ReplayContentView _prefabReplayContentView;

	private ReplayContentView _replayContentView;

	public Action OnClickReplayButton;

	private long _battle_id;

	private ReplayInfoItem _replayInfo;

	public void ReplayButtonClicked()
	{
		OnClickReplayButton.Call();
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE_TRANS);
		long battleId = _battle_id;
		ReplayInfoItem replayInfo = _replayInfo;
		CheckTimeSlipRotationPeriodTask task = new CheckTimeSlipRotationPeriodTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			GoReplay(battleId, replayInfo);
		}));
	}

	private static void GoReplay(long battleId, ReplayInfoItem replayInfo)
	{
		string replayDataDirectoryPath = Application.persistentDataPath + "/NewReplay";
		string text = (Directory.Exists(replayDataDirectoryPath) ? (from d in Directory.GetDirectories(replayDataDirectoryPath, "*", SearchOption.TopDirectoryOnly)
			select d.Replace("\\", "/")).FirstOrDefault((string f) => f == replayDataDirectoryPath + "/" + battleId) : null);
		bool num = (text == null) | (Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.NEWREPLAY_ALL) || (Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.NEWREPLAY_EXCLUDE_ROTATION) && replayInfo.BattleFormat != Format.Rotation));
		UIManager.ChangeViewSceneParam param = new UIManager.ChangeViewSceneParam();
		param.MyPageMenuIndex = 6;
		param.IsCutCardMotion = true;
		param.OnFinishChangeView = delegate
		{
			ReplayDialog.Create();
		};
		param.IsUpdateFooterMenuTexture = true;
		if (num)
		{
			ReplayDetailTask replayDetailTask = new ReplayDetailTask();
			replayDetailTask.SetParameter(PlayerStaticData.UserViewerID, battleId);
			UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(replayDetailTask, delegate
			{
				ReplayController.StartPlayReplay(replayInfo, UIManager.ViewScene.MyPage, param);
			}));
			return;
		}
		Data.ReplayBattleInfo = new ReplayDetailInfo(NewReplayBattleMgr.ReadJson(Directory.GetFiles(text, "*", SearchOption.TopDirectoryOnly).ToList().FirstOrDefault((string x) => x.Contains("replay_info.json"))));
		Data.ReplayBattleInfo.is_two_pick = replayInfo.BattleParameter.IsTwoPick;
		Data.ReplayBattleInfo._battleType = (int)replayInfo.BattleParameter.BattleType;
		Data.ReplayBattleInfo._twoPickFormat = replayInfo.BattleParameter.TwoPickFormat;
		Data.CurrentFormat = replayInfo.BattleFormat;
		ReplayController.StartPlayReplay(UIManager.ViewScene.MyPage, param, isNewReplay: true, battleId.ToString());
	}

	public IEnumerator Setup(ReplayInfoItem item, List<string> loadedTextures)
	{
		if (_replayContentView == null)
		{
			_replayContentView = NGUITools.AddChild(_rootReplayContentView, _prefabReplayContentView.gameObject).GetComponent<ReplayContentView>();
		}
		_replayInfo = item;
		_battle_id = item.BattleId;
		_replayContentView.SetOpponentPlayerInfo(item);
		_replayContentView.SetBattleInfo(item);
		string text = Application.persistentDataPath + "/NewReplay";
		if ((!Directory.Exists(text) || !(from d in Directory.GetDirectories(text, "*", SearchOption.TopDirectoryOnly)
			select d.Replace("\\", "/")).Contains(text + "/" + _battle_id)) | (Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.NEWREPLAY_ALL) || (Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.NEWREPLAY_EXCLUDE_ROTATION) && _replayInfo.BattleFormat != Format.Rotation)))
		{
			_replayContentView.SetOldReplayLabel(isActive: true);
		}
		else
		{
			_replayContentView.SetOldReplayLabel(isActive: false);
		}
		_replayContentView.ClearTexture();
		while (true)
		{
			if (item.BattleId != _battle_id)
			{
				yield break;
			}
			string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(item.OpponentEmblemId, ResourcesManager.AssetLoadPathType.Emblem_S);
			string assetTypePath2 = Toolbox.ResourcesManager.GetAssetTypePath(item.OpponentCountryCode, ResourcesManager.AssetLoadPathType.Country_S);
			if (!loadedTextures.Contains(assetTypePath))
			{
				yield return null;
				continue;
			}
			if (string.IsNullOrEmpty(assetTypePath2) || loadedTextures.Contains(assetTypePath2))
			{
				break;
			}
			yield return null;
		}
		_replayContentView.SetTexture(item);
	}
}
