using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace TeslaConsole;

internal class RPStrings
{
	private static bool sInitalized = false;

	private static List<string> sFirstScore = new List<string>();

	private static List<string> sFirstBoost = new List<string>();

	private static List<string> sFirstDeath = new List<string>();

	private static List<string> sFirstKill = new List<string>();

	private static List<string> sShortestLap = new List<string>();

	private static List<string> sLightDamage = new List<string>();

	private static List<string> sModerateDamage = new List<string>();

	private static List<string> sHeavyDamage = new List<string>();

	private static List<string> sKill = new List<string>();

	private static List<string> sSelfKill = new List<string>();

	private static List<string> sScore = new List<string>();

	private static List<string> sBoost = new List<string>();

	private static List<string> sRecap = new List<string>();

	private static Random sRandom = new Random();

	private static void Initalize()
	{
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "RedPlanet\\RPStrings.xml"));
		if (xmlDocument.DocumentElement.Name != "RPStrings")
		{
			throw new XmlException("Document element was not an RPStrings node.");
		}
		foreach (XmlNode childNode in xmlDocument.DocumentElement.ChildNodes)
		{
			switch (childNode.Name)
			{
			case "FirstScore":
				if (!sFirstScore.Contains(childNode.InnerText))
				{
					sFirstScore.Add(childNode.InnerText);
				}
				break;
			case "FirstBoost":
				if (!sFirstBoost.Contains(childNode.InnerText))
				{
					sFirstBoost.Add(childNode.InnerText);
				}
				break;
			case "FirstDeath":
				if (!sFirstDeath.Contains(childNode.InnerText))
				{
					sFirstDeath.Add(childNode.InnerText);
				}
				break;
			case "FirstKill":
				if (!sFirstKill.Contains(childNode.InnerText))
				{
					sFirstKill.Add(childNode.InnerText);
				}
				break;
			case "ShortestLap":
				if (!sShortestLap.Contains(childNode.InnerText))
				{
					sShortestLap.Add(childNode.InnerText);
				}
				break;
			case "LightDamage":
				if (!sLightDamage.Contains(childNode.InnerText))
				{
					sLightDamage.Add(childNode.InnerText);
				}
				break;
			case "ModerateDamage":
				if (!sModerateDamage.Contains(childNode.InnerText))
				{
					sModerateDamage.Add(childNode.InnerText);
				}
				break;
			case "HeavyDamage":
				if (!sHeavyDamage.Contains(childNode.InnerText))
				{
					sHeavyDamage.Add(childNode.InnerText);
				}
				break;
			case "Kill":
				if (!sKill.Contains(childNode.InnerText))
				{
					sKill.Add(childNode.InnerText);
				}
				break;
			case "SelfKill":
				if (!sSelfKill.Contains(childNode.InnerText))
				{
					sSelfKill.Add(childNode.InnerText);
				}
				break;
			case "Score":
				if (!sScore.Contains(childNode.InnerText))
				{
					sScore.Add(childNode.InnerText);
				}
				break;
			case "Boost":
				if (!sBoost.Contains(childNode.InnerText))
				{
					sBoost.Add(childNode.InnerText);
				}
				break;
			case "Recap":
				if (!sRecap.Contains(childNode.InnerText))
				{
					sRecap.Add(childNode.InnerText);
				}
				break;
			}
		}
		sInitalized = true;
	}

	private static string GetRandomStringAndReplace(List<string> list, TimeSpan time, string player, string vehicle, string damager)
	{
		if (!sInitalized)
		{
			Initalize();
		}
		if (list.Count == 0)
		{
			return null;
		}
		return list[sRandom.Next(list.Count)].Replace("{time}", GetTimeString(time)).Replace("{player}", player).Replace("{vehicle}", vehicle)
			.Replace("{damager}", damager);
	}

	public static string GetTimeString(TimeSpan time)
	{
		int num = (int)(time.TotalSeconds + 0.5);
		int num2 = num / 60;
		return $"{num2:D2}:{num % 60:D2}";
	}

	public static string FirstScore(TimeSpan time, string player, string vehicle, string damager)
	{
		return GetRandomStringAndReplace(sFirstScore, time, player, vehicle, damager);
	}

	public static string FirstBoost(TimeSpan time, string player, string vehicle)
	{
		return GetRandomStringAndReplace(sFirstBoost, time, player, vehicle, "");
	}

	public static string FirstDeath(TimeSpan time, string player, string vehicle, string damager)
	{
		return GetRandomStringAndReplace(sFirstDeath, time, player, vehicle, damager);
	}

	public static string FirstKill(TimeSpan time, string player, string vehicle, string damager)
	{
		return GetRandomStringAndReplace(sFirstKill, time, player, vehicle, damager);
	}

	public static string ShortestLap(TimeSpan time, TimeSpan lapTime, string player, string vehicle)
	{
		return GetRandomStringAndReplace(sShortestLap, time, player, vehicle, "").Replace("{lap_time}", GetTimeString(lapTime));
	}

	public static string LightDamage(TimeSpan time, string player, string vehicle, string damager)
	{
		return GetRandomStringAndReplace(sLightDamage, time, player, vehicle, damager);
	}

	public static string ModerateDamage(TimeSpan time, string player, string vehicle, string damager)
	{
		return GetRandomStringAndReplace(sModerateDamage, time, player, vehicle, damager);
	}

	public static string HeavyDamage(TimeSpan time, string player, string vehicle, string damager)
	{
		return GetRandomStringAndReplace(sHeavyDamage, time, player, vehicle, damager);
	}

	public static string Kill(TimeSpan time, string player, string vehicle, string damager)
	{
		return GetRandomStringAndReplace(sKill, time, player, vehicle, damager);
	}

	public static string SelfKill(TimeSpan time, string player, string vehicle)
	{
		return GetRandomStringAndReplace(sSelfKill, time, player, vehicle, "");
	}

	public static string Score(TimeSpan time, string player, string vehicle)
	{
		return GetRandomStringAndReplace(sScore, time, player, vehicle, "");
	}

	public static string Boost(TimeSpan time, string player, string vehicle)
	{
		return GetRandomStringAndReplace(sBoost, time, player, vehicle, "");
	}

	public static string Recap(string[] playerRanking)
	{
		StringBuilder stringBuilder = new StringBuilder(playerRanking[0]);
		for (int i = 1; i < playerRanking.Length - 1; i++)
		{
			stringBuilder.AppendFormat(", {0}", playerRanking[i]);
		}
		return GetRandomStringAndReplace(sRecap, TimeSpan.Zero, "", "", "").Replace("{winners}", stringBuilder.ToString()).Replace("{looser}", (playerRanking.Length > 1) ? playerRanking[playerRanking.Length - 1] : "nobody");
	}
}
