using System;
using System.Collections.Generic;
using System.Text;

namespace TeslaConsole.RedPlanet;

[Serializable]
internal class RPFootballMission : RPMission
{
	private readonly List<RPFootballPlayer> mFootballPlayers = new List<RPFootballPlayer>();

	public IEnumerable<RPFootballPlayer> FootballPlayers => mFootballPlayers;

	public override IEnumerable<RPPlayer> Players => mFootballPlayers.ToArray();

	public override int PlayerCount => mFootballPlayers.Count;

	public RPFootballMission(string mapKey, string timeOfDayKey, string weatherKey, bool scoreCompression, TimeSpan gameLength)
		: base(RPConfig.Football.Key, mapKey, timeOfDayKey, weatherKey, scoreCompression, gameLength)
	{
	}

	public void AddFootballTeam(IEnumerable<RPFootballPlayer> footballTeam)
	{
		bool flag = false;
		string text = null;
		foreach (RPFootballPlayer item in footballTeam)
		{
			if (text == null)
			{
				text = item.TeamKey;
				foreach (RPFootballPlayer mFootballPlayer in mFootballPlayers)
				{
					if (mFootballPlayer.TeamKey == item.TeamKey)
					{
						throw new ArgumentException("The provided RPFootballPlayers' team has already been added", "footballTeam");
					}
				}
			}
			else if (text != item.TeamKey)
			{
				throw new ArgumentException("The provided RPFootballPlayers were not on the same team", "footballTeam");
			}
			if (item.PositionKey == "runner")
			{
				if (flag)
				{
					throw new ArgumentException("The provided RPFootballPlayers contained more than one runner", "footballTeam");
				}
				flag = true;
			}
		}
		if (!flag)
		{
			throw new ArgumentException("The provided RPFootballPlayers did not contain a runner", "footballTeam");
		}
		mFootballPlayers.AddRange(footballTeam);
	}

	protected override void AppendMissionSpecificData(StringBuilder egg)
	{
		Dictionary<string, List<RPFootballPlayer>> dictionary = new Dictionary<string, List<RPFootballPlayer>>();
		foreach (RPFootballPlayer mFootballPlayer in mFootballPlayers)
		{
			if (!dictionary.ContainsKey(mFootballPlayer.TeamKey))
			{
				dictionary.Add(mFootballPlayer.TeamKey, new List<RPFootballPlayer>());
			}
			dictionary[mFootballPlayer.TeamKey].Add(mFootballPlayer);
		}
		int num = 1;
		StringBuilder stringBuilder = new StringBuilder("[teams]\n");
		StringBuilder stringBuilder2 = new StringBuilder();
		StringBuilder stringBuilder3 = new StringBuilder("[teambitmap]\n");
		StringBuilder stringBuilder4 = new StringBuilder();
		StringBuilder stringBuilder5 = new StringBuilder();
		foreach (KeyValuePair<string, List<RPFootballPlayer>> item in dictionary)
		{
			stringBuilder.AppendFormat("team=team::{0}\n", item.Key);
			stringBuilder2.AppendFormat("[team::{0}]\n", item.Key);
			stringBuilder2.AppendFormat("bitmapindex={0}\n", num++);
			stringBuilder2.AppendFormat("pilots=pilots::{0}\n", item.Key);
			stringBuilder3.AppendFormat("bitmap=BitMap::Small::team::{0}\n", item.Key);
			stringBuilder4.AppendFormat("[BitMap::Small::team::{0}]\n", item.Key);
			stringBuilder4.Append(PlasmaBitmaps.GenerateString(64, 16, item.Key));
			stringBuilder4.Append("width=4\n");
			stringBuilder5.AppendFormat("[pilots::{0}]\n", item.Key);
			foreach (RPFootballPlayer item2 in item.Value)
			{
				stringBuilder5.AppendFormat("pilot={0}\n", item2.PodHostAddress);
			}
		}
		egg.Append(stringBuilder);
		egg.Append(stringBuilder2);
		egg.Append(stringBuilder5);
		egg.Append(stringBuilder3);
		egg.Append(stringBuilder4);
	}
}
