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

namespace Wizard;

public class ReplayContentView : MonoBehaviour
{
	[SerializeField]
	private UITexture _emblemTexture;

	[SerializeField]
	private UITexture _countoryTexture;

	[SerializeField]
	private UILabel _nameLabel;

	[SerializeField]
	private UILabel _descriptionLabel;

	[SerializeField]
	private UILabel _classNameLabel;

	[SerializeField]
	private UISprite _classIconSprite;

	[SerializeField]
	private UISprite _subClassIconSprite;

	[SerializeField]
	private UILabel _winLabel;

	[SerializeField]
	private UILabel _loseLabel;

	[SerializeField]
	private UISprite _formatIconSprite;

	[SerializeField]
	private UILabel _formatLabel;

	[SerializeField]
	private UILabel _oldReplayLabel;

	[SerializeField]
	private UIGrid _abilityRoot;

	[SerializeField]
	private UISprite _abilityIcon;

	private List<UISprite> _abilityIcons = new List<UISprite>();

	private const float ABILITY_ORIGINAL_POS_X = -405.6f;

	private const int ABILITY_ICON_WIDTH = 25;

	private const int CLASS_LABEL_ORIGINAL_WIDTH = 237;

	private const int CLASS_LABEL_AND_ICON_GAP = 4;

	public void SetData(ReplayInfoItem item)
	{
		ClearTexture();
		SetOpponentPlayerInfo(item);
		SetBattleInfo(item);
		SetTexture(item);
	}

	public void ClearTexture()
	{
		_emblemTexture.mainTexture = null;
		_countoryTexture.mainTexture = null;
	}

	public void SetOpponentPlayerInfo(ReplayInfoItem item)
	{
		_classIconSprite.spriteName = ClassCharaPrm.GetIconSpriteName((CardBasePrm.ClanType)item.OpponentClassId);
		_nameLabel.text = item.OpponentName;
		if (FormatBehaviorManager.GetDefaultBehaviour(item.BattleFormat).UseSubClass)
		{
			_classNameLabel.gameObject.SetActive(value: false);
			_subClassIconSprite.gameObject.SetActive(value: true);
			_subClassIconSprite.spriteName = ClassCharaPrm.GetIconSpriteName((CardBasePrm.ClanType)item.OpponentSubClassId);
			_abilityRoot.gameObject.SetActive(value: false);
			return;
		}
		_classNameLabel.gameObject.SetActive(value: true);
		_subClassIconSprite.gameObject.SetActive(value: false);
		_abilityIcon.gameObject.SetActive(value: false);
		MyRotationInfo myRotationInfo = null;
		if (!string.IsNullOrEmpty(item.OpponentRotationId))
		{
			myRotationInfo = Data.MyRotationAllInfo.Get(item.OpponentRotationId);
		}
		_abilityRoot.gameObject.SetActive(myRotationInfo != null);
		if (myRotationInfo != null)
		{
			_classNameLabel.text = DeckData.CreateMyRotationClassName(item.OpponentClassId, myRotationInfo);
			_classNameLabel.width = 233 - myRotationInfo.Abilities.Count * 25;
			_classNameLabel.InitializeFont();
			Vector3 localPosition = _abilityRoot.transform.localPosition;
			localPosition.x = -401.6f + _classNameLabel.printedSize.x;
			_abilityRoot.transform.localPosition = localPosition;
			for (int i = 0; i < _abilityIcons.Count; i++)
			{
				_abilityIcons[i].gameObject.SetActive(value: false);
				Object.Destroy(_abilityIcons[i].gameObject);
			}
			_abilityIcons.Clear();
			for (int j = 0; j < myRotationInfo.Abilities.Count; j++)
			{
				UISprite component = NGUITools.AddChild(_abilityRoot.gameObject, _abilityIcon.gameObject).GetComponent<UISprite>();
				component.spriteName = myRotationInfo.Abilities[j].IconName;
				component.gameObject.SetActive(value: true);
				_abilityIcons.Add(component);
			}
			_abilityRoot.Reposition();
		}
		else if (item.BattleFormat == Format.Avatar)
		{
			ClassCharacterMasterData charaPrmByCharaId = GameMgr.GetIns().GetDataMgr().GetCharaPrmByCharaId(item.OpponentCharaId);
			_classNameLabel.text = charaPrmByCharaId.chara_name;
		}
		else
		{
			_classNameLabel.text = GameMgr.GetIns().GetDataMgr().GetClanNameByKey(item.OpponentClassId);
		}
		ClassCharaPrm.SetClassLabelSetting(_classNameLabel, (CardBasePrm.ClanType)item.OpponentClassId);
	}

	public void SetBattleInfo(ReplayInfoItem item)
	{
		string battleTypeName = UIUtil.GetBattleTypeName(item.BattleParameter.BattleType);
		string clanNameByKey = GameMgr.GetIns().GetDataMgr().GetClanNameByKey(item.ClassId);
		string text = ConvertTime.ToLocal(item.BattleStartTime, ConvertTime.FORMAT.TIME_DATE_SHORT);
		if (item.BattleFormat == Format.Avatar)
		{
			string chara_name = GameMgr.GetIns().GetDataMgr().GetCharaPrmByCharaId(item.CharaId)
				.chara_name;
			_descriptionLabel.text = Data.SystemText.Get("Dia_Replay_002", battleTypeName, chara_name, text);
		}
		else
		{
			_descriptionLabel.text = Data.SystemText.Get("Dia_Replay_002", battleTypeName, clanNameByKey, text);
		}
		_winLabel.gameObject.SetActive(item.IsWin);
		_loseLabel.gameObject.SetActive(!item.IsWin);
		if (item.BattleParameter.IsTwoPick || item.BattleParameter.IsSealed)
		{
			if (UIUtil.IsTwoPickForReplay(item.BattleParameter))
			{
				_formatIconSprite.gameObject.SetActive(value: true);
				_formatLabel.gameObject.SetActive(value: true);
				_formatIconSprite.spriteName = "icon_2pick";
				_formatLabel.text = Data.SystemText.Get("Battle_0004");
			}
			else if (item.BattleParameter.IsSealed)
			{
				_formatIconSprite.gameObject.SetActive(value: true);
				_formatLabel.gameObject.SetActive(value: true);
				_formatIconSprite.spriteName = "icon_sealed_s";
				_formatLabel.text = Data.SystemText.Get("BattleName_Sealed");
			}
			else if (item.BattleParameter.TwoPickFormat == TwoPickFormat.Cube || item.BattleParameter.TwoPickFormat == TwoPickFormat.BackdraftCube)
			{
				if (Data.MyPageNotifications.data.RoomRule.ChallengePickFormat == TwoPickFormat.Cube)
				{
					_formatIconSprite.gameObject.SetActive(value: true);
					_formatIconSprite.spriteName = "icon_2pick";
				}
				else
				{
					_formatIconSprite.gameObject.SetActive(value: false);
				}
				_formatLabel.gameObject.SetActive(value: true);
				_formatLabel.text = Data.SystemText.Get("RoomBattle_0113");
			}
			else if (item.BattleParameter.TwoPickFormat == TwoPickFormat.Chaos || item.BattleParameter.TwoPickFormat == TwoPickFormat.BackdraftChaos)
			{
				if (Data.MyPageNotifications.data.RoomRule.ChallengePickFormat == TwoPickFormat.Chaos)
				{
					_formatIconSprite.gameObject.SetActive(value: true);
					_formatIconSprite.spriteName = "icon_2pick";
				}
				else
				{
					_formatIconSprite.gameObject.SetActive(value: false);
				}
				_formatLabel.gameObject.SetActive(value: true);
				_formatLabel.text = Data.SystemText.Get("Chaos_FormatName");
			}
			else
			{
				_formatIconSprite.gameObject.SetActive(value: false);
				_formatLabel.gameObject.SetActive(value: false);
			}
		}
		else if (item.BattleFormat == Format.Hof)
		{
			_formatIconSprite.gameObject.SetActive(value: false);
			_formatLabel.gameObject.SetActive(value: true);
			_formatLabel.text = Data.SystemText.Get("Colosseum_0108");
		}
		else if (item.BattleFormat == Format.Windfall)
		{
			_formatIconSprite.gameObject.SetActive(value: false);
			_formatLabel.gameObject.SetActive(value: true);
			_formatLabel.text = Data.SystemText.Get("Colosseum_0115");
		}
		else
		{
			_formatIconSprite.gameObject.SetActive(value: true);
			_formatLabel.gameObject.SetActive(value: true);
			_formatIconSprite.spriteName = FormatBehaviorManager.GetDefaultBehaviour(item.BattleFormat).SmallIconSpriteName;
			_formatLabel.text = FormatBehaviorManager.GetFormatName(item.BattleFormat);
		}
	}

	public void SetTexture(ReplayInfoItem item)
	{
		string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(item.OpponentEmblemId, ResourcesManager.AssetLoadPathType.Emblem_S, isfetch: true);
		_emblemTexture.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(assetTypePath);
		if (!string.IsNullOrEmpty(item.OpponentCountryCode))
		{
			string assetTypePath2 = Toolbox.ResourcesManager.GetAssetTypePath(item.OpponentCountryCode, ResourcesManager.AssetLoadPathType.Country_S, isfetch: true);
			_countoryTexture.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(assetTypePath2);
		}
	}

	public void SetOldReplayLabel(bool isActive)
	{
		_oldReplayLabel.text = Data.SystemText.Get("Dia_Replay_003");
		_oldReplayLabel.gameObject.SetActive(isActive);
	}
}
