using System.Collections.Generic;
using System.Linq;
using Wizard;
using Wizard.Battle.Phase;

public static class EmitHandUtility
{
	public static void SendSelectSkill(NetworkBattleSender.SELECT_SKILL_OPERATION skillSelectOperation, BattleManagerBase battleMgr, BattleCardBase cardInfoToSend, bool isEvolveSelect, List<BattleCardBase> compInfoToSend = null, List<SkillBase> activeSelectSkills = null, bool isBurialRite = false, bool isChoiceBrave = false)
	{
		NetworkStandardBattleMgr networkStandardBattleMgr = ConvertToStandardNetworkBattleMgr(battleMgr);
		if (networkStandardBattleMgr == null)
		{
			return;
		}
		string text = string.Empty;
		List<int> list = new List<int>();
		switch (skillSelectOperation)
		{
		case NetworkBattleSender.SELECT_SKILL_OPERATION.StartSelect:
		case NetworkBattleSender.SELECT_SKILL_OPERATION.StartFusionSelect:
			text = cardInfoToSend.Index.ToString();
			if (activeSelectSkills != null)
			{
				list.AddRange(from s in activeSelectSkills
					where RegisterSkillConditionCheck.IsSkillConditionCheck(s, isNotHandCheck: true)
					select s.SkillPrm.ownerCard.Skills.IndexOf(s));
			}
			break;
		case NetworkBattleSender.SELECT_SKILL_OPERATION.SelectCard:
		case NetworkBattleSender.SELECT_SKILL_OPERATION.CompleteSelect:
		case NetworkBattleSender.SELECT_SKILL_OPERATION.SelectFusionIngredient:
			text = (cardInfoToSend.IsPlayer ? "1" : "0") + ConvertToThreeDigitCardIndex(cardInfoToSend.Index);
			break;
		case NetworkBattleSender.SELECT_SKILL_OPERATION.CompleteFusionSelect:
			text = cardInfoToSend.Index.ToString();
			break;
		case NetworkBattleSender.SELECT_SKILL_OPERATION.StartChoiceSelect:
			text = cardInfoToSend.Index.ToString();
			break;
		case NetworkBattleSender.SELECT_SKILL_OPERATION.CancelSelect:
		case NetworkBattleSender.SELECT_SKILL_OPERATION.CancelChoiceSelect:
			text = string.Empty;
			break;
		case NetworkBattleSender.SELECT_SKILL_OPERATION.SelectChoiceCard:
		case NetworkBattleSender.SELECT_SKILL_OPERATION.CompleteChoiceSelect:
		{
			for (int i = 0; i < compInfoToSend.Count; i++)
			{
				if (i > 0)
				{
					text += ",";
				}
				text += compInfoToSend[i].Index;
			}
			break;
		}
		default:
			Debug.LogError("Invalid Select Skill Operation");
			return;
		}
		networkStandardBattleMgr.NetworkSender.SendSelectSkill(skillSelectOperation, isEvolveSelect, text, list, isBurialRite, isChoiceBrave);
	}

	public static void SendSelectObject(BattleManagerBase battleMgr, BattleCardBase selectedCard)
	{
		if (!GameMgr.GetIns().GetDataMgr().IsRoomBattleType())
		{
			return;
		}
		NetworkStandardBattleMgr networkStandardBattleMgr = ConvertToStandardNetworkBattleMgr(battleMgr);
		if (networkStandardBattleMgr != null && battleMgr.GetCurrentPhase() is MainPhase)
		{
			if (selectedCard != null && selectedCard.IsInHand)
			{
				networkStandardBattleMgr.NetworkSender.SendSelectObject(selectedCard);
			}
			else
			{
				networkStandardBattleMgr.NetworkSender.SendSelectObject(null);
			}
		}
	}

	public static void SendTurnEndReady(BattleManagerBase battleMgr, bool isShortenedTurn)
	{
		ConvertToStandardNetworkBattleMgr(battleMgr)?.NetworkSender.SendTurnEndReady(isShortenedTurn);
	}

	public static void SendSlideObject(BattleManagerBase battleMgr, NetworkBattleSender.SLIDE_OBJECT_TYPE slideObjectType, BattleCardBase selectedCard = null, BattleCardBase attackingCard = null)
	{
		ConvertToStandardNetworkBattleMgr(battleMgr)?.NetworkSender.SendSlideObject(slideObjectType, selectedCard, attackingCard);
	}

	public static void ResetSelectedSlideCard(BattleManagerBase battleMgr)
	{
		ConvertToStandardNetworkBattleMgr(battleMgr)?.NetworkSender.ResetSelectedSlideCard();
	}

	private static NetworkStandardBattleMgr ConvertToStandardNetworkBattleMgr(BattleManagerBase battleMgr)
	{
		if (ToolboxGame.RealTimeNetworkAgent == null)
		{
			return null;
		}
		return battleMgr as NetworkStandardBattleMgr;
	}

	private static string ConvertToThreeDigitCardIndex(int cardIndex)
	{
		return cardIndex.ToString().PadLeft(3, '0');
	}
}
