using System;
using System.Collections.Generic;
using System.Text;
using Munga.Net;

namespace TeslaConsole.RedPlanet;

[Serializable]
internal abstract class RPMission
{
	private string mScenarioKey;

	private string mMapKey;

	private string mTimeOfDayKey;

	private string mWeatherKey;

	private bool mScoreCompression;

	private TimeSpan mGameLength;

	private readonly List<RPCamera> mCameras = new List<RPCamera>();

	public string ScenarioKey => mScenarioKey;

	public string MapKey => mMapKey;

	public string TimeOfDayKey => mTimeOfDayKey;

	public string WeatherKey => mWeatherKey;

	public bool ScoreCompression => mScoreCompression;

	public TimeSpan GameLength => mGameLength;

	public abstract IEnumerable<RPPlayer> Players { get; }

	public abstract int PlayerCount { get; }

	public List<RPCamera> Cameras => mCameras;

	protected RPMission(string scenarioKey, string mapKey, string timeOfDayKey, string weatherKey, bool scoreCompression, TimeSpan gameLength)
	{
		if (scenarioKey == null)
		{
			throw new ArgumentNullException("scenarioKey");
		}
		if (mapKey == null)
		{
			throw new ArgumentNullException("mapKey");
		}
		if (timeOfDayKey == null)
		{
			throw new ArgumentNullException("timeOfDayKey");
		}
		if (weatherKey == null)
		{
			throw new ArgumentNullException("weatherKey");
		}
		if (gameLength <= TimeSpan.Zero)
		{
			throw new ArgumentOutOfRangeException("gameLength");
		}
		mScenarioKey = scenarioKey;
		mMapKey = mapKey;
		mTimeOfDayKey = timeOfDayKey;
		mWeatherKey = weatherKey;
		mScoreCompression = scoreCompression;
		mGameLength = gameLength;
	}

	public EggFileMessage[] ToEggFileMessages()
	{
		string text = ToEggString();
		text = text.Replace("\r\n", "\0");
		text = text.Replace('\n', '\0');
		byte[] bytes = Encoding.ASCII.GetBytes(text);
		EggFileMessage[] array = new EggFileMessage[(bytes.Length + 999) / 1000];
		byte[] array2 = new byte[1000];
		for (int i = 0; i < array.Length; i++)
		{
			int num = i * 1000;
			int num2 = bytes.Length - num;
			if (num2 > 1000)
			{
				num2 = 1000;
			}
			Buffer.BlockCopy(bytes, num, array2, 0, num2);
			array[i] = new EggFileMessage(i, bytes.Length, num2, array2);
		}
		return array;
	}

	public string ToEggString()
	{
		StringBuilder stringBuilder = new StringBuilder();
		List<string> list = new List<string>();
		stringBuilder.Append("[mission]\n");
		stringBuilder.Append("adventure=Red Planet\n");
		stringBuilder.AppendFormat("map={0}\n", mMapKey);
		stringBuilder.AppendFormat("scenario={0}\n", mScenarioKey);
		stringBuilder.AppendFormat("time={0}\n", mTimeOfDayKey);
		stringBuilder.AppendFormat("weather={0}\n", mWeatherKey);
		stringBuilder.AppendFormat("temperature=0\n");
		stringBuilder.AppendFormat("compression={0}\n", mScoreCompression ? "1" : "0");
		stringBuilder.AppendFormat("length={0}\n", mGameLength.TotalSeconds);
		stringBuilder.Append("[pilots]\n");
		foreach (RPPlayer player in Players)
		{
			stringBuilder.AppendFormat("pilot={0}\n", player.PodHostAddress);
		}
		foreach (RPCamera camera in Cameras)
		{
			stringBuilder.AppendFormat("pilot={0}\n", camera.PodHostAddress);
		}
		int num = 1;
		foreach (RPPlayer player2 in Players)
		{
			player2.AppendParticipant(num++, stringBuilder);
			list.Add(player2.Name);
		}
		foreach (RPCamera camera2 in Cameras)
		{
			camera2.AppendParticipant(num++, stringBuilder);
			list.Add("Camera");
		}
		AppendMissionSpecificData(stringBuilder);
		stringBuilder.Append("[largebitmap]\n");
		foreach (string item in list)
		{
			stringBuilder.AppendFormat("bitmap=BitMap::Large::{0}\n", item);
		}
		stringBuilder.Append("[smallbitmap]\n");
		foreach (string item2 in list)
		{
			stringBuilder.AppendFormat("bitmap=BitMap::Small::{0}\n", item2);
		}
		foreach (string item3 in list)
		{
			stringBuilder.AppendFormat("[BitMap::Large::{0}]\n", item3);
			stringBuilder.Append(PlasmaBitmaps.GenerateString(128, 32, item3));
			stringBuilder.Append("width=8\n");
			stringBuilder.AppendFormat("[BitMap::Small::{0}]\n", item3);
			stringBuilder.Append(PlasmaBitmaps.GenerateString(64, 16, item3));
			stringBuilder.Append("width=4\n");
		}
		AppendOrdinals(stringBuilder);
		return stringBuilder.ToString();
	}

	protected abstract void AppendMissionSpecificData(StringBuilder egg);

	private static void AppendOrdinals(StringBuilder sb)
	{
		sb.Append("[ordinals]\n");
		sb.Append("bitmap=Ordinal::BitMap::1\n");
		sb.Append("bitmap=Ordinal::BitMap::2\n");
		sb.Append("bitmap=Ordinal::BitMap::3\n");
		sb.Append("bitmap=Ordinal::BitMap::4\n");
		sb.Append("[Ordinal::BitMap::1]\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=000038000003C000001FF00000000F00\n");
		sb.Append("bitmap=0000F8000003C000003FF80000000F00\n");
		sb.Append("bitmap=0001F8000003C00000707C0000000F00\n");
		sb.Append("bitmap=0001F8000003C00000603C0000000F00\n");
		sb.Append("bitmap=00007801FC0FF00000003C3DF807FF00\n");
		sb.Append("bitmap=00007803FE0FF00000003C3FFC0FFF00\n");
		sb.Append("bitmap=00007803C703C00000003C3E3C1F0F00\n");
		sb.Append("bitmap=000078078303C0000000783C1E1E0F00\n");
		sb.Append("bitmap=000078078003C0000000783C1E1E0F00\n");
		sb.Append("bitmap=00007807C003C0000000F03C1E1E0F00\n");
		sb.Append("bitmap=00007807F003C0000001E03C1E1E0F00\n");
		sb.Append("bitmap=00007803FC03C0000003C03C1E1E0F00\n");
		sb.Append("bitmap=00007801FE03C0000007803C1E1E0F00\n");
		sb.Append("bitmap=000078007F03C000000F003C1E1E0F00\n");
		sb.Append("bitmap=000078001F03C000001E003C1E1E0F00\n");
		sb.Append("bitmap=000078000F03C000003C003C1E1E0F00\n");
		sb.Append("bitmap=000078060F03C0000078003C1E1E0F00\n");
		sb.Append("bitmap=000078071E03E0000078003C1E1F1F00\n");
		sb.Append("bitmap=00007803FE01F000007FFC3C1E0FFF00\n");
		sb.Append("bitmap=00007801FC00F000007FFC3C1E07EF00\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("x=128\n");
		sb.Append("y=32\n");
		sb.Append("width=8\n");
		sb.Append("[Ordinal::BitMap::2]\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=001FFF0000003C0000003C03C0780000\n");
		sb.Append("bitmap=001FFF0000003C0000007C03C0780000\n");
		sb.Append("bitmap=00000E0000003C000000FC03C0780000\n");
		sb.Append("bitmap=00003C0000003C000001BC03C0780000\n");
		sb.Append("bitmap=0000700F3E1FFC000003BC0FF07BF000\n");
		sb.Append("bitmap=0001E00F7E3FFC0000073C0FF07FF800\n");
		sb.Append("bitmap=0003800FFE7C3C0000063C03C07C7800\n");
		sb.Append("bitmap=0007F80FFE783C00000C3C03C0783C00\n");
		sb.Append("bitmap=0007FE0F80783C0000183C03C0783C00\n");
		sb.Append("bitmap=00001E0F00783C0000383C03C0783C00\n");
		sb.Append("bitmap=00000F0F00783C0000703C03C0783C00\n");
		sb.Append("bitmap=00000F0F00783C00007FFF03C0783C00\n");
		sb.Append("bitmap=00000F0F00783C00007FFF03C0783C00\n");
		sb.Append("bitmap=00000F0F00783C0000003C03C0783C00\n");
		sb.Append("bitmap=00000F0F00783C0000003C03C0783C00\n");
		sb.Append("bitmap=00000F0F00783C0000003C03C0783C00\n");
		sb.Append("bitmap=00180F0F00783C0000003C03C0783C00\n");
		sb.Append("bitmap=001C1F0F007C7C0000003C03E0783C00\n");
		sb.Append("bitmap=000FFE0F003FFC0000003C01F0783C00\n");
		sb.Append("bitmap=0007FC0F001FBC0000003C00F0783C00\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("x=128\n");
		sb.Append("y=32\n");
		sb.Append("width=8\n");
		sb.Append("[Ordinal::BitMap::3]\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=001FFF03C07800000000FC03C0780000\n");
		sb.Append("bitmap=001FFF03C07800000003FC03C0780000\n");
		sb.Append("bitmap=001E0003C07800000007C003C0780000\n");
		sb.Append("bitmap=001E0003C0780000000F0003C0780000\n");
		sb.Append("bitmap=001E000FF07BF000000F000FF07BF000\n");
		sb.Append("bitmap=001E000FF07FF800001E000FF07FF800\n");
		sb.Append("bitmap=001E0003C07C7800001E0003C07C7800\n");
		sb.Append("bitmap=001FFC03C0783C00001EFC03C0783C00\n");
		sb.Append("bitmap=001FFE03C0783C00001FFE03C0783C00\n");
		sb.Append("bitmap=00001F03C0783C00001F1F03C0783C00\n");
		sb.Append("bitmap=00000F03C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=00000F03C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=00000F03C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=00000F03C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=00000F03C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=00000F03C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=00180F03C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=001C1F03E0783C00001F1F03E0783C00\n");
		sb.Append("bitmap=000FFE01F0783C00000FFE01F0783C00\n");
		sb.Append("bitmap=0007FC00F0783C000007FC00F0783C00\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("x=128\n");
		sb.Append("y=32\n");
		sb.Append("width=8\n");
		sb.Append("[Ordinal::BitMap::4]\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=001FFF03C07800000007FC03C0780000\n");
		sb.Append("bitmap=001FFF03C0780000000FFE03C0780000\n");
		sb.Append("bitmap=00000F03C0780000001F1F03C0780000\n");
		sb.Append("bitmap=00000F03C0780000001E0F03C0780000\n");
		sb.Append("bitmap=00000F0FF07BF000001E0F0FF07BF000\n");
		sb.Append("bitmap=00001F0FF07FF800001E0F0FF07FF800\n");
		sb.Append("bitmap=00001E03C07C7800001E0F03C07C7800\n");
		sb.Append("bitmap=00003E03C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=00003C03C0783C00000F1E03C0783C00\n");
		sb.Append("bitmap=00003C03C0783C000007FC03C0783C00\n");
		sb.Append("bitmap=00007803C0783C000007FC03C0783C00\n");
		sb.Append("bitmap=00007803C0783C00000F1E03C0783C00\n");
		sb.Append("bitmap=00007803C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=0000F003C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=0000F003C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=0000F003C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=0000F003C0783C00001E0F03C0783C00\n");
		sb.Append("bitmap=0000F003E0783C00000F1E03E0783C00\n");
		sb.Append("bitmap=0000F001F0783C00000FFE01F0783C00\n");
		sb.Append("bitmap=0000F000F0783C000007FC00F0783C00\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("bitmap=00000000000000000000000000000000\n");
		sb.Append("x=128\n");
		sb.Append("y=32\n");
		sb.Append("width=8\n");
	}
}
