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

public class StockReceiveMgr : StockSequenceMgr
{
	private List<int> DisConnectIndexList = new List<int>();

	public int SequenceAlreadyNumber { get; private set; }

	public StockReceiveMgr(string name)
		: base(name)
	{
	}

	public override void StockData(Dictionary<string, object> data)
	{
		base.StockData(data);
		string text = data["uri"].ToString();
		if (text == NetworkBattleDefine.NetworkURINames[NetworkBattleDefine.NetworkBattleURI.TurnEnd] || text == NetworkBattleDefine.NetworkURINames[NetworkBattleDefine.NetworkBattleURI.TurnEndFinal])
		{
			ToolboxGame.RealTimeNetworkAgent.SetGungnirSendActionSequence(isActive: true);
		}
	}

	public Dictionary<string, object> GetFrontData()
	{
		if (sequenceDataList.Count == 0)
		{
			return null;
		}
		int num = -1;
		Dictionary<string, object> result = new Dictionary<string, object>();
		foreach (Dictionary<string, object> sequenceData in sequenceDataList)
		{
			if (num == -1 || GetSequenceNumber(sequenceData) < num)
			{
				num = GetSequenceNumber(sequenceData);
				result = sequenceData;
			}
		}
		return result;
	}

	public Dictionary<string, object> FindData(int sequenceNum)
	{
		if (sequenceDataList.Count == 0)
		{
			return null;
		}
		return sequenceDataList.Find((Dictionary<string, object> x) => GetSequenceNumber(x) == sequenceNum);
	}

	public int GetMaxSequenceNumber()
	{
		if (sequenceDataList.Count == 0)
		{
			return SequenceAlreadyNumber;
		}
		int num = SequenceAlreadyNumber;
		bool flag;
		do
		{
			flag = false;
			foreach (Dictionary<string, object> sequenceData in sequenceDataList)
			{
				if (GetSequenceNumber(sequenceData) == num + 1)
				{
					num = GetSequenceNumber(sequenceData);
					flag = true;
					break;
				}
			}
		}
		while (flag);
		return num;
	}

	public bool CheckStockData(Dictionary<string, object> data)
	{
		if (DisConnectIndexList.Any((int d) => d == GetSequenceNumber(data)))
		{
			return true;
		}
		int dataSequenceNumber = GetSequenceNumber(data);
		string dataUri = data["uri"].ToString();
		if (dataSequenceNumber <= SequenceAlreadyNumber)
		{
			LocalLog.AccumulateLastTraceLog("CheckStockData False  SequenceAlreadyNumber " + SequenceAlreadyNumber + "dataSequenceNumber" + dataSequenceNumber);
			return false;
		}
		if (sequenceDataList.Any((Dictionary<string, object> d) => dataSequenceNumber == GetSequenceNumber(d)))
		{
			LocalLog.AccumulateLastTraceLog("CheckStockData False Already dataSequenceNumber" + dataSequenceNumber);
			if (sequenceDataList.Any((Dictionary<string, object> d) => d["uri"].ToString() == dataUri))
			{
				LocalLog.AccumulateLastTraceLog("CheckStockData False Already Uri" + dataUri);
				return false;
			}
		}
		return true;
	}

	public void SettingOppoDisconnectSequenceNumber(Dictionary<string, object> data)
	{
		if (data["uri"].ToString() == "OppoDisconnect")
		{
			data[sequenceName] = (SequenceAlreadyNumber + 1).ToString();
			DisConnectIndexList.Add(SequenceAlreadyNumber + 1);
		}
	}

	public void UpdateSequenceAlreadyNumber(Dictionary<string, object> data)
	{
		int sequenceNumber = GetSequenceNumber(data);
		if (SequenceAlreadyNumber < sequenceNumber)
		{
			SequenceAlreadyNumber = sequenceNumber;
		}
	}

	public bool IsNoStockData(Dictionary<string, object> data)
	{
		if (data["uri"].ToString() == NetworkBattleDefine.NetworkURINames[NetworkBattleDefine.NetworkBattleURI.JudgeResult] || data["uri"].ToString() == NetworkBattleDefine.NetworkURINames[NetworkBattleDefine.NetworkBattleURI.BattleFinish])
		{
			return true;
		}
		if (!data.ContainsKey(sequenceName))
		{
			return true;
		}
		return GetSequenceNumber(data) == 0;
	}

	public void Recovery(int sequenceAlreadyNumber)
	{
		SequenceAlreadyNumber = sequenceAlreadyNumber;
		ClearSequenceDataList();
	}

	public void SetSequenceNumber(int num)
	{
		SequenceAlreadyNumber = num;
	}
}
