using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Text;

namespace TeslaConsole.RedPlanet;

public class RPPrintDocument : PrintDocument
{
	private const int sThinBorderSize = 3;

	private const int sThickBorderSize = 14;

	private const int sChampherSize = 14;

	private const double sSmallRectWidthDropFraction = 0.5721493440968718;

	private const double sSmallRectHeightDropFraction = 245.0 / 372.0;

	private const string sSansSerifFontName = "Helvetica LT Std";

	private const string sSerifFontName = "Courier New";

	private readonly RPMissionResults mMissionResults;

	private readonly RPPlayer[][] mFinalRanking;

	private readonly RPPlayer[] mPrintOrder;

	private int mCurrentPlayer;

	private Dictionary<RPPlayer, List<Point>> mPlayerLines;

	private int[] mChartRows;

	private Dictionary<TimeSpan, int> mChartCols;

	internal RPPrintDocument(RPMissionResults missionResults)
	{
		mMissionResults = missionResults;
		base.DocumentName = string.Format("Red Planet {0} mission at {1} on {2}", (mMissionResults.Mission is RPFootballMission) ? "Football" : "Death Race", mMissionResults.MissionStart.ToLongTimeString(), mMissionResults.MissionStart.ToLongDateString());
		mFinalRanking = mMissionResults.GetChartOrder();
		List<RPPlayer> list = new List<RPPlayer>();
		for (int i = 0; i < mFinalRanking.Length; i++)
		{
			list.Add(mFinalRanking[i][0]);
		}
		for (int j = 0; j < mFinalRanking.Length; j++)
		{
			for (int k = 1; k < mFinalRanking[j].Length; k++)
			{
				list.Add(mFinalRanking[j][k]);
			}
		}
		mPrintOrder = list.ToArray();
	}

	protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
	{
		e.PageSettings.Margins = new Margins(50, 50, 50, 50);
		e.PageSettings.Landscape = true;
		base.OnQueryPageSettings(e);
	}

	protected override void Dispose(bool disposing)
	{
		base.Dispose(disposing);
	}

	protected override void OnBeginPrint(PrintEventArgs e)
	{
		mCurrentPlayer = 0;
		base.OnBeginPrint(e);
	}

	protected override void OnPrintPage(PrintPageEventArgs e)
	{
		RPPlayer rPPlayer = mPrintOrder[mCurrentPlayer];
		List<MissionEvent> list = new List<MissionEvent>();
		foreach (MissionEvent @event in mMissionResults.Events)
		{
			if (Contains(@event.InvolvedPilots, rPPlayer))
			{
				list.Add(@event);
			}
		}
		for (int i = 0; i < list.Count - 1; i++)
		{
			for (int j = i + 1; j < list.Count; j++)
			{
				if (list[i].GameTime > list[j].GameTime)
				{
					MissionEvent value = list[i];
					list[i] = list[j];
					list[j] = value;
				}
			}
		}
		int score = mMissionResults.GetScore(rPPlayer);
		int finalPlace = mMissionResults.GetFinalPlace(rPPlayer);
		Size size = new Size(e.PageBounds.Width - 100, e.PageBounds.Height - 100);
		if (size.Width > e.MarginBounds.Width)
		{
			size.Width = e.MarginBounds.Width;
		}
		if (size.Height > e.MarginBounds.Height)
		{
			size.Height = e.MarginBounds.Height;
		}
		Rectangle rectangle = new Rectangle((e.MarginBounds.Width - size.Width) / 2 + e.MarginBounds.Left, (e.MarginBounds.Height - size.Height) / 2 + e.MarginBounds.Top, size.Width, size.Height);
		Rectangle inputRect = rectangle;
		inputRect.Inflate(-14, -14);
		Rectangle rectangle2 = rectangle;
		rectangle2.Inflate(-3, -3);
		Rectangle inputRect2 = rectangle2;
		inputRect2.Inflate(-14, -14);
		Rectangle rectangle3 = rectangle2;
		rectangle3.Inflate(-7, -7);
		Rectangle inputRect3 = rectangle3;
		inputRect3.Inflate(-14, -14);
		Rectangle rectangle4 = rectangle2;
		rectangle4.Inflate(-14, -14);
		Rectangle inputRect4 = rectangle4;
		inputRect4.Inflate(-14, -14);
		Rectangle rectangle5 = rectangle4;
		rectangle5.Inflate(-3, -3);
		Rectangle inputRect5 = rectangle5;
		inputRect5.Inflate(-14, -14);
		int widthDrop = (int)(0.5721493440968718 * (double)rectangle3.Width + 0.5);
		int heightDrop = (int)(245.0 / 372.0 * (double)rectangle3.Height + 0.5);
		Rectangle rectangle6 = RectDrop(rectangle, widthDrop, heightDrop);
		Rectangle rectangle7 = RectDrop(inputRect, widthDrop, heightDrop);
		Rectangle rectangle8 = RectDrop(rectangle2, widthDrop, heightDrop);
		Rectangle rectangle9 = RectDrop(inputRect2, widthDrop, heightDrop);
		Rectangle rectangle10 = RectDrop(rectangle3, widthDrop, heightDrop);
		Rectangle rectangle11 = RectDrop(inputRect3, widthDrop, heightDrop);
		Rectangle rectangle12 = RectDrop(rectangle4, widthDrop, heightDrop);
		Rectangle rectangle13 = RectDrop(inputRect4, widthDrop, heightDrop);
		Rectangle rectangle14 = RectDrop(rectangle5, widthDrop, heightDrop);
		Rectangle rectangle15 = RectDrop(inputRect5, widthDrop, heightDrop);
		using (GraphicsPath graphicsPath = new GraphicsPath())
		{
			graphicsPath.AddLine(rectangle.Left, inputRect.Bottom, rectangle.Left, inputRect.Top);
			graphicsPath.AddLine(inputRect.Left, rectangle.Top, inputRect.Right, rectangle.Top);
			graphicsPath.AddLine(rectangle3.Right, rectangle3.Top, inputRect3.Left, rectangle3.Top);
			graphicsPath.AddLine(rectangle3.Left, inputRect3.Top, rectangle3.Left, rectangle3.Bottom);
			graphicsPath.StartFigure();
			graphicsPath.AddLine(rectangle3.Left, inputRect5.Bottom, rectangle5.Left, inputRect5.Bottom);
			graphicsPath.AddLine(inputRect5.Left, rectangle5.Bottom, inputRect5.Right, rectangle5.Bottom);
			graphicsPath.AddLine(rectangle5.Right, inputRect5.Bottom, rectangle5.Right, rectangle3.Top);
			graphicsPath.AddLine(rectangle3.Right, rectangle3.Top, rectangle3.Right, inputRect3.Bottom);
			graphicsPath.AddLine(inputRect3.Right, rectangle3.Bottom, rectangle3.Left, rectangle3.Bottom);
			graphicsPath.StartFigure();
			graphicsPath.AddLine(rectangle10.Left, rectangle15.Bottom, rectangle14.Left, rectangle15.Bottom);
			graphicsPath.AddLine(rectangle15.Left, rectangle14.Bottom, rectangle15.Right, rectangle14.Bottom);
			graphicsPath.AddLine(rectangle14.Right, rectangle15.Bottom, rectangle14.Right, rectangle10.Top);
			graphicsPath.AddLine(rectangle10.Right, rectangle10.Top, rectangle10.Right, rectangle11.Bottom);
			graphicsPath.AddLine(rectangle11.Right, rectangle10.Bottom, rectangle10.Left, rectangle10.Bottom);
			e.Graphics.FillPath(Brushes.LightGray, graphicsPath);
		}
		using (GraphicsPath graphicsPath2 = new GraphicsPath())
		{
			graphicsPath2.AddLine(rectangle2.Left, inputRect2.Bottom, rectangle.Left, inputRect.Bottom);
			graphicsPath2.AddLine(inputRect.Left, rectangle.Bottom, inputRect.Right, rectangle.Bottom);
			graphicsPath2.AddLine(rectangle.Right, inputRect.Bottom, rectangle.Right, inputRect.Top);
			graphicsPath2.AddLine(inputRect.Right, rectangle.Top, inputRect2.Right, rectangle2.Top);
			graphicsPath2.AddLine(inputRect2.Right, rectangle3.Top, rectangle3.Right, inputRect3.Top);
			graphicsPath2.AddLine(rectangle3.Right, inputRect3.Bottom, inputRect3.Right, rectangle3.Bottom);
			graphicsPath2.AddLine(inputRect2.Left, rectangle3.Bottom, rectangle3.Left, inputRect2.Bottom);
			graphicsPath2.StartFigure();
			graphicsPath2.AddLine(rectangle12.Left, rectangle13.Bottom, rectangle14.Left, rectangle15.Bottom);
			graphicsPath2.AddLine(rectangle14.Left, rectangle15.Top, rectangle15.Left, rectangle14.Top);
			graphicsPath2.AddLine(rectangle15.Right, rectangle14.Top, rectangle14.Right, rectangle15.Top);
			graphicsPath2.AddLine(rectangle12.Right, rectangle13.Top, rectangle10.Right, rectangle13.Top);
			graphicsPath2.AddLine(rectangle10.Right, rectangle10.Top, rectangle11.Left, rectangle10.Top);
			graphicsPath2.AddLine(rectangle10.Left, rectangle11.Top, rectangle10.Left, rectangle13.Bottom);
			graphicsPath2.StartFigure();
			graphicsPath2.AddLine(rectangle4.Left, inputRect4.Bottom, rectangle5.Left, inputRect5.Bottom);
			graphicsPath2.AddLine(rectangle5.Left, rectangle6.Bottom + 14, inputRect5.Left, rectangle6.Bottom);
			graphicsPath2.AddLine(rectangle7.Right, rectangle6.Bottom, rectangle6.Right, rectangle7.Bottom);
			graphicsPath2.AddLine(rectangle6.Right, inputRect5.Top, rectangle6.Right + 14, rectangle5.Top);
			graphicsPath2.AddLine(inputRect5.Right, rectangle5.Top, rectangle5.Right, inputRect5.Top);
			graphicsPath2.AddLine(rectangle4.Right, inputRect4.Top, rectangle3.Right, inputRect4.Top);
			graphicsPath2.AddLine(rectangle3.Right, inputRect3.Top, inputRect3.Right, rectangle3.Top);
			graphicsPath2.AddLine(rectangle10.Right, rectangle3.Top, rectangle10.Right, rectangle11.Bottom);
			graphicsPath2.AddLine(rectangle11.Right, rectangle10.Bottom, inputRect3.Left, rectangle10.Bottom);
			graphicsPath2.AddLine(rectangle3.Left, rectangle10.Bottom, rectangle3.Left, inputRect4.Bottom);
			e.Graphics.FillPath(Brushes.DarkGray, graphicsPath2);
		}
		using (GraphicsPath graphicsPath3 = new GraphicsPath())
		{
			graphicsPath3.AddLine(rectangle2.Left, inputRect2.Bottom, rectangle2.Left, inputRect2.Top);
			graphicsPath3.AddLine(inputRect2.Left, rectangle2.Top, inputRect2.Right, rectangle2.Top);
			graphicsPath3.AddLine(rectangle2.Right, inputRect2.Top, rectangle2.Right, inputRect2.Bottom);
			graphicsPath3.AddLine(inputRect2.Right, rectangle2.Bottom, inputRect2.Left, rectangle2.Bottom);
			graphicsPath3.StartFigure();
			graphicsPath3.AddLine(rectangle12.Left, rectangle13.Bottom, rectangle12.Left, rectangle13.Top);
			graphicsPath3.AddLine(rectangle13.Left, rectangle12.Top, rectangle13.Right, rectangle12.Top);
			graphicsPath3.AddLine(rectangle12.Right, rectangle13.Top, rectangle12.Right, rectangle13.Bottom);
			graphicsPath3.AddLine(rectangle13.Right, rectangle12.Bottom, rectangle13.Left, rectangle12.Bottom);
			graphicsPath3.StartFigure();
			graphicsPath3.AddLine(rectangle4.Left, inputRect4.Bottom, rectangle4.Left, rectangle8.Bottom + 14);
			graphicsPath3.AddLine(rectangle13.Left, rectangle8.Bottom, rectangle9.Right, rectangle8.Bottom);
			graphicsPath3.AddLine(rectangle8.Right, rectangle9.Bottom, rectangle8.Right, inputRect4.Top);
			graphicsPath3.AddLine(rectangle8.Right + 14, rectangle4.Top, inputRect4.Right, rectangle4.Top);
			graphicsPath3.AddLine(rectangle4.Right, inputRect4.Top, rectangle4.Right, inputRect4.Bottom);
			graphicsPath3.AddLine(inputRect4.Right, rectangle4.Bottom, inputRect4.Left, rectangle4.Bottom);
			e.Graphics.FillPath(Brushes.Gray, graphicsPath3);
		}
		using Font font3 = new Font("Helvetica LT Std", 12f);
		using Font font = new Font("Helvetica LT Std", 8f);
		Rectangle rectangle16 = rectangle14;
		rectangle16.X += 175;
		rectangle16.Width -= 178;
		rectangle16.Y += 58;
		rectangle16.Height -= 86;
		RPVehicle rPVehicle = (RPConfig.Vehicles.ContainsKey(rPPlayer.VehicleKey) ? RPConfig.Vehicles[rPPlayer.VehicleKey] : null);
		Image image = rPVehicle?.Image;
		if (image != null)
		{
			e.Graphics.DrawImage(image, GetCenteredScaledRect(rectangle16, image.Size));
		}
		else
		{
			StringFormat stringFormat = new StringFormat();
			stringFormat.LineAlignment = StringAlignment.Center;
			stringFormat.Alignment = StringAlignment.Center;
			using (StringFormat format = stringFormat)
			{
				Rectangle rectangle17 = rectangle16;
				rectangle17.Inflate(-20, -20);
				e.Graphics.DrawString("Null-flux resonation is disrupting contact with the camera drone.  Recon immagry could not be obtained for this vehicle.", font, Brushes.Black, rectangle17, format);
			}
			e.Graphics.DrawRectangle(Pens.Black, rectangle16);
		}
		using (Font font2 = new Font("Helvetica LT Std", 18f, FontStyle.Bold))
		{
			using StringFormat stringFormat2 = new StringFormat();
			stringFormat2.Alignment = StringAlignment.Center;
			stringFormat2.LineAlignment = StringAlignment.Far;
			e.Graphics.DrawString(rPPlayer.Name, font2, Brushes.Black, rectangle14.Left + rectangle14.Width / 2, rectangle16.Top - 10, stringFormat2);
		}
		using (StringFormat stringFormat3 = new StringFormat())
		{
			stringFormat3.Alignment = StringAlignment.Center;
			e.Graphics.DrawString((rPVehicle != null) ? rPVehicle.Name : rPPlayer.VehicleKey, font3, Brushes.Black, rectangle16.Left + rectangle16.Width / 2, rectangle16.Bottom + 5, stringFormat3);
		}
		int num = rectangle15.Left - 7;
		Rectangle rectangle18 = new Rectangle(num, rectangle16.Top, rectangle16.Left - num, rectangle15.Bottom - rectangle16.Top);
		using (Font font4 = new Font("Helvetica LT Std", 16f, FontStyle.Bold))
		{
			using StringFormat stringFormat4 = new StringFormat();
			stringFormat4.Alignment = StringAlignment.Center;
			StringBuilder stringBuilder = new StringBuilder();
			if (rPPlayer is RPFootballPlayer)
			{
				RPFootballPlayer rPFootballPlayer = (RPFootballPlayer)rPPlayer;
				stringBuilder.AppendLine(RPConfig.Football.Teams.ContainsKey(rPFootballPlayer.TeamKey) ? RPConfig.Football.Teams[rPFootballPlayer.TeamKey].Name : rPFootballPlayer.TeamKey);
				stringBuilder.AppendLine(RPConfig.Football.Positions.ContainsKey(rPFootballPlayer.PositionKey) ? RPConfig.Football.Positions[rPFootballPlayer.PositionKey] : rPFootballPlayer.PositionKey);
			}
			stringBuilder.AppendLine(string.Format("{0}{1} Place", finalPlace, finalPlace switch
			{
				3 => "rd", 
				2 => "nd", 
				1 => "st", 
				_ => "th", 
			}));
			e.Graphics.DrawString(stringBuilder.ToString(), font4, Brushes.Black, rectangle18, stringFormat4);
		}
		using (StringFormat stringFormat5 = new StringFormat())
		{
			stringFormat5.LineAlignment = StringAlignment.Far;
			stringFormat5.SetTabStops(0f, new float[1] { 80f });
			int laps = 0;
			TimeSpan fastestLap = TimeSpan.Zero;
			int deaths = 0;
			int kills = 0;
			GetPlayerStats(out laps, out fastestLap, out deaths, out kills, rPPlayer, list);
			StringBuilder stringBuilder2 = new StringBuilder();
			if (rPPlayer is RPFootballPlayer)
			{
				stringBuilder2.AppendLine($"Team Score:\t{score}");
				stringBuilder2.AppendLine();
			}
			else
			{
				stringBuilder2.AppendLine($"Score:\t{score}");
				stringBuilder2.AppendLine();
				stringBuilder2.AppendLine($"Laps:\t{laps}");
				stringBuilder2.AppendLine();
				stringBuilder2.AppendLine($"Fastest Lap:\t{RPStrings.GetTimeString(fastestLap)}");
				stringBuilder2.AppendLine();
			}
			stringBuilder2.AppendLine($"Deaths:\t{deaths}");
			stringBuilder2.AppendLine();
			stringBuilder2.AppendLine($"Kills:\t{kills}");
			e.Graphics.DrawString(stringBuilder2.ToString(), font, Brushes.Black, rectangle18, stringFormat5);
		}
		int num2 = rectangle5.Top + 40;
		Rectangle rectangle19 = new Rectangle(rectangle6.Right + 5, num2, rectangle5.Right - rectangle6.Right - 10, rectangle6.Bottom - 5 - num2);
		using (StringFormat stringFormat6 = new StringFormat())
		{
			stringFormat6.Alignment = StringAlignment.Center;
			e.Graphics.DrawString("Mission Highlights", font3, Brushes.Black, (rectangle19.Right + rectangle19.Left) / 2, rectangle5.Top + 15, stringFormat6);
		}
		using (Font font5 = new Font("Courier New", 10f))
		{
			string text = mMissionResults.mMissionStart.ToString() + " ";
			string s = text;
			string text2 = "";
			if (mMissionResults.Mission is RPRaceMission)
			{
				List<string> list2 = new List<string>();
				RPPlayer[][] array = mFinalRanking;
				foreach (RPPlayer[] array2 in array)
				{
					list2.Add(array2[0].Name);
				}
				text2 = RPStrings.Recap(list2.ToArray());
			}
			Random random = new Random();
			List<MissionEvent> list3 = new List<MissionEvent>(list);
			SortedList<TimeSpan, string> sortedList = new SortedList<TimeSpan, string>();
			string text3 = text + text2;
			while (e.Graphics.MeasureString(text3, font5, rectangle19.Width).Height < (float)rectangle19.Height)
			{
				s = text3;
				if (list3.Count < 1)
				{
					break;
				}
				int index = random.Next(list3.Count);
				MissionEvent missionEvent = list3[index];
				list3.RemoveAt(index);
				string text4 = null;
				if (missionEvent is BoostEvent && ((BoostEvent)missionEvent).BoosterOn == 1)
				{
					text4 = RPStrings.Boost(missionEvent.GameTime, missionEvent.ReportingPilot.Name, RPConfig.VehicleNameOrKey(missionEvent.ReportingPilot.VehicleKey));
				}
				else if (missionEvent is ScoredEvent)
				{
					text4 = RPStrings.Score(missionEvent.GameTime, missionEvent.ReportingPilot.Name, RPConfig.VehicleNameOrKey(missionEvent.ReportingPilot.VehicleKey));
				}
				else if (missionEvent is DamagedEvent)
				{
					DamagedEvent damagedEvent = (DamagedEvent)missionEvent;
					if (damagedEvent.DamageLoss > 0)
					{
						text4 = ((damagedEvent.DamageLoss < 10) ? RPStrings.LightDamage(damagedEvent.GameTime, damagedEvent.ReportingPilot.Name, RPConfig.VehicleNameOrKey(damagedEvent.ReportingPilot.VehicleKey), damagedEvent.Damager.Name) : ((damagedEvent.DamageLoss >= 100) ? RPStrings.HeavyDamage(damagedEvent.GameTime, damagedEvent.ReportingPilot.Name, RPConfig.VehicleNameOrKey(damagedEvent.ReportingPilot.VehicleKey), damagedEvent.Damager.Name) : RPStrings.ModerateDamage(damagedEvent.GameTime, damagedEvent.ReportingPilot.Name, RPConfig.VehicleNameOrKey(damagedEvent.ReportingPilot.VehicleKey), damagedEvent.Damager.Name)));
					}
				}
				else if (missionEvent is KilledEvent)
				{
					KilledEvent killedEvent = (KilledEvent)missionEvent;
					text4 = ((killedEvent.ReportingPilot != killedEvent.Killer) ? RPStrings.Kill(killedEvent.GameTime, killedEvent.ReportingPilot.Name, RPConfig.VehicleNameOrKey(killedEvent.ReportingPilot.VehicleKey), killedEvent.Killer.Name) : RPStrings.SelfKill(killedEvent.GameTime, killedEvent.ReportingPilot.Name, RPConfig.VehicleNameOrKey(killedEvent.ReportingPilot.VehicleKey)));
				}
				if (text4 != null)
				{
					TimeSpan gameTime;
					for (gameTime = missionEvent.GameTime; sortedList.ContainsKey(gameTime); gameTime += TimeSpan.FromTicks(1L))
					{
					}
					sortedList.Add(gameTime, text4);
				}
				StringBuilder stringBuilder3 = new StringBuilder(text);
				foreach (KeyValuePair<TimeSpan, string> item in sortedList)
				{
					if (stringBuilder3.Length > 0)
					{
						stringBuilder3.Append("  ");
					}
					stringBuilder3.Append(item.Value);
				}
				if (stringBuilder3.Length > 0)
				{
					stringBuilder3.Append("  ");
				}
				stringBuilder3.Append(text2);
				text3 = stringBuilder3.ToString();
			}
			e.Graphics.DrawString(s, font5, Brushes.Black, rectangle19);
		}
		int num3 = rectangle6.Bottom + 5;
		Rectangle rectangle20 = new Rectangle(inputRect5.Left, num3, inputRect5.Width, rectangle5.Bottom - num3);
		Rectangle rectangle21 = new Rectangle(rectangle20.Right - 250, rectangle20.Top, 250, rectangle20.Height - 20);
		Image image2 = (RPConfig.Maps.ContainsKey(mMissionResults.Mission.MapKey) ? RPConfig.Maps[mMissionResults.Mission.MapKey] : null)?.Image;
		if (image2 != null)
		{
			e.Graphics.DrawImage(image2, GetCenteredScaledRect(rectangle21, image2.Size));
		}
		else
		{
			StringFormat stringFormat7 = new StringFormat();
			stringFormat7.LineAlignment = StringAlignment.Center;
			stringFormat7.Alignment = StringAlignment.Center;
			using (StringFormat format2 = stringFormat7)
			{
				Rectangle rectangle22 = rectangle21;
				rectangle22.Inflate(-20, -20);
				e.Graphics.DrawString("The surveillance satellite is currently on a transitional orbit.  Satellite imagery could not be obtained for this map.", font, Brushes.Black, rectangle22, format2);
			}
			e.Graphics.DrawRectangle(Pens.Black, rectangle21);
		}
		Rectangle rect = new Rectangle(rectangle20.Left, rectangle20.Top, rectangle21.Left - 5 - rectangle20.Left, 92);
		Dictionary<RPPlayer, int[]> dictionary = new Dictionary<RPPlayer, int[]>();
		foreach (RPPlayer player in mMissionResults.Mission.Players)
		{
			if (player != rPPlayer)
			{
				dictionary.Add(player, new int[4]);
			}
		}
		foreach (MissionEvent event2 in mMissionResults.Events)
		{
			if (event2 is KilledEvent)
			{
				KilledEvent killedEvent2 = (KilledEvent)event2;
				if (killedEvent2.ReportingPilot != killedEvent2.Killer)
				{
					if (killedEvent2.Killer == rPPlayer)
					{
						dictionary[killedEvent2.ReportingPilot][3]++;
					}
					else if (killedEvent2.ReportingPilot == rPPlayer)
					{
						dictionary[killedEvent2.Killer][2]++;
					}
				}
			}
			else
			{
				if (!(event2 is DamagedEvent))
				{
					continue;
				}
				DamagedEvent damagedEvent2 = (DamagedEvent)event2;
				if (damagedEvent2.ReportingPilot != damagedEvent2.Damager)
				{
					if (damagedEvent2.Damager == rPPlayer)
					{
						dictionary[damagedEvent2.ReportingPilot][1] += damagedEvent2.DamageLoss;
					}
					else if (damagedEvent2.ReportingPilot == rPPlayer)
					{
						dictionary[damagedEvent2.Damager][0] += damagedEvent2.DamageLoss;
					}
				}
			}
		}
		using (Pen pen = new Pen(Color.Gray, 2f))
		{
			using Font font6 = new Font("Helvetica LT Std", 6f, FontStyle.Regular);
			using StringFormat stringFormat8 = new StringFormat();
			e.Graphics.DrawRectangle(Pens.Gray, rect);
			int num4 = 120;
			int num5 = 14;
			int num6 = rect.Left + 1;
			int num7 = rect.Left + num4;
			int num8 = (rect.Width - num4) / Math.Max(1, dictionary.Count);
			if (dictionary.Count == 0)
			{
				e.Graphics.DrawLine(pen, num7, rect.Top, num7, rect.Bottom);
			}
			int num9 = rect.Bottom;
			for (int l = 0; l < 5; l++)
			{
				num9 -= num5;
				e.Graphics.DrawLine(Pens.Gray, rect.Left, num9, rect.Right, num9);
			}
			int num10 = rect.Top + 1;
			stringFormat8.LineAlignment = StringAlignment.Near;
			e.Graphics.DrawString("Pilot", font6, Brushes.Black, num6, num10, stringFormat8);
			int num11 = rect.Bottom - num5 / 2;
			int num12 = num11 - num5;
			int num13 = num12 - num5;
			int num14 = num13 - num5;
			int num15 = num14 - num5;
			stringFormat8.LineAlignment = StringAlignment.Center;
			e.Graphics.DrawString("Score", font6, Brushes.Black, num6, num15, stringFormat8);
			e.Graphics.DrawString("Damage You Received From", font6, Brushes.Black, num6, num14, stringFormat8);
			e.Graphics.DrawString("Damage You Inflicted On", font6, Brushes.Black, num6, num13, stringFormat8);
			e.Graphics.DrawString("Times You Were Killed By", font6, Brushes.Black, num6, num12, stringFormat8);
			e.Graphics.DrawString("Times You Killed", font6, Brushes.Black, num6, num11, stringFormat8);
			int num16 = num7;
			foreach (KeyValuePair<RPPlayer, int[]> item2 in dictionary)
			{
				int num17 = num16 + 1;
				e.Graphics.DrawLine(pen, num16, rect.Top, num16, rect.Bottom);
				stringFormat8.LineAlignment = StringAlignment.Near;
				e.Graphics.DrawString(item2.Key.Name, font6, Brushes.Black, num17, num10, stringFormat8);
				stringFormat8.LineAlignment = StringAlignment.Center;
				e.Graphics.DrawString(mMissionResults.GetScore(item2.Key).ToString(), font6, Brushes.Black, num17, num15, stringFormat8);
				e.Graphics.DrawString(item2.Value[0].ToString(), font6, Brushes.Black, num17, num14, stringFormat8);
				e.Graphics.DrawString(item2.Value[1].ToString(), font6, Brushes.Black, num17, num13, stringFormat8);
				e.Graphics.DrawString(item2.Value[2].ToString(), font6, Brushes.Black, num17, num12, stringFormat8);
				e.Graphics.DrawString(item2.Value[3].ToString(), font6, Brushes.Black, num17, num11, stringFormat8);
				num16 += num8;
			}
		}
		int num18 = rectangle20.Left + 60;
		int num19 = rect.Bottom + 120;
		Rectangle rectangle23 = new Rectangle(num18, num19, rect.Right - 75 - num18, rectangle20.Bottom - 85 - num19);
		if (mPlayerLines == null)
		{
			mPlayerLines = new Dictionary<RPPlayer, List<Point>>();
			RPPlayer[][] array = mFinalRanking;
			foreach (RPPlayer[] array3 in array)
			{
				RPPlayer[] array4 = array3;
				foreach (RPPlayer key in array4)
				{
					mPlayerLines.Add(key, new List<Point>());
				}
			}
			mChartRows = new int[mPlayerLines.Count];
			float num20 = (float)rectangle23.Height / (float)mPlayerLines.Count;
			for (int n = 0; n < mChartRows.Length; n++)
			{
				mChartRows[n] = (int)(num20 * (float)n + 0.5f) + rectangle23.Top;
			}
			mChartCols = new Dictionary<TimeSpan, int>();
			TimeSpan timeSpan = mMissionResults.ActuallMissionTime.Subtract(TimeSpan.FromSeconds(10.0));
			TimeSpan timeSpan2 = TimeSpan.FromSeconds(15.0);
			for (TimeSpan timeSpan3 = timeSpan2; timeSpan3 < timeSpan; timeSpan3 += timeSpan2)
			{
				mChartCols.Add(timeSpan3, (int)((float)timeSpan3.Ticks / (float)mMissionResults.ActuallMissionTime.Ticks * (float)rectangle23.Width + 0.5f) + rectangle23.Left);
			}
			int num21 = 0;
			array = mFinalRanking;
			foreach (RPPlayer[] array5 in array)
			{
				RPPlayer[] array4 = array5;
				foreach (RPPlayer key2 in array4)
				{
					mPlayerLines[key2].Add(new Point(rectangle23.Left, mChartRows[num21++]));
				}
			}
			foreach (KeyValuePair<TimeSpan, int> mChartCol in mChartCols)
			{
				num21 = 0;
				array = mMissionResults.GetChartOrder(mChartCol.Key);
				foreach (RPPlayer[] array6 in array)
				{
					RPPlayer[] array4 = array6;
					foreach (RPPlayer key3 in array4)
					{
						mPlayerLines[key3].Add(new Point(mChartCol.Value, mChartRows[num21++]));
					}
				}
			}
			num21 = 0;
			array = mFinalRanking;
			foreach (RPPlayer[] array7 in array)
			{
				RPPlayer[] array4 = array7;
				foreach (RPPlayer key4 in array4)
				{
					mPlayerLines[key4].Add(new Point(rectangle23.Right, mChartRows[num21++]));
				}
			}
		}
		using (Font font7 = new Font("Helvetica LT Std", 8f))
		{
			using (StringFormat stringFormat9 = new StringFormat())
			{
				stringFormat9.LineAlignment = StringAlignment.Center;
				foreach (KeyValuePair<RPPlayer, List<Point>> mPlayerLine in mPlayerLines)
				{
					if (mPlayerLine.Key != rPPlayer)
					{
						DrawPlayerLine(e.Graphics, mPlayerLine.Key, mPlayerLine.Value.ToArray(), rectangle23.Right + 5, Pens.LightGray, Brushes.LightGray, font7, stringFormat9);
					}
				}
				DrawPlayerLine(e.Graphics, rPPlayer, mPlayerLines[rPPlayer].ToArray(), rectangle23.Right + 5, Pens.Black, Brushes.Black, font7, stringFormat9);
			}
			using Pen pen2 = new Pen(Color.Black, 1.5f);
			using (Font font8 = new Font("Courier New", 8f))
			{
				int num22 = 5;
				int num23 = 3;
				e.Graphics.DrawLines(pen2, new Point[3]
				{
					new Point(rectangle23.Left, rectangle23.Top),
					new Point(rectangle23.Left, rectangle23.Bottom),
					new Point(rectangle23.Right, rectangle23.Bottom)
				});
				using StringFormat stringFormat10 = new StringFormat();
				stringFormat10.Alignment = StringAlignment.Far;
				stringFormat10.LineAlignment = StringAlignment.Center;
				int num24 = 0;
				int num25 = 0;
				RPPlayer[][] array = mFinalRanking;
				foreach (RPPlayer[] array8 in array)
				{
					int num26 = mChartRows[num25];
					e.Graphics.DrawLine(pen2, rectangle23.Left - num22, num26, rectangle23.Left + num22, num26);
					Graphics graphics = e.Graphics;
					int num27 = ++num25;
					graphics.DrawString(num27.ToString(), font8, Brushes.Black, rectangle23.Left - 10, num26, stringFormat10);
					RPPlayer[] array4 = array8;
					for (int m = 0; m < array4.Length; m++)
					{
						_ = array4[m];
						num24++;
					}
				}
				int num28 = rectangle23.Bottom + 23;
				stringFormat10.Alignment = StringAlignment.Center;
				stringFormat10.LineAlignment = StringAlignment.Near;
				e.Graphics.DrawString("0", font8, Brushes.Black, rectangle23.Left, num28, stringFormat10);
				foreach (KeyValuePair<TimeSpan, int> mChartCol2 in mChartCols)
				{
					if (mChartCol2.Key.Seconds == 0)
					{
						e.Graphics.DrawLine(pen2, mChartCol2.Value, rectangle23.Bottom - num22, mChartCol2.Value, rectangle23.Bottom + num22);
						e.Graphics.DrawString(mChartCol2.Key.Minutes.ToString(), font8, Brushes.Black, mChartCol2.Value, num28, stringFormat10);
					}
					else
					{
						e.Graphics.DrawLine(pen2, mChartCol2.Value, rectangle23.Bottom - num23, mChartCol2.Value, rectangle23.Bottom);
					}
				}
				e.Graphics.DrawLine(pen2, rectangle23.Right, rectangle23.Bottom - num22, rectangle23.Right, rectangle23.Bottom + num22);
				e.Graphics.DrawString(((int)(mMissionResults.ActuallMissionTime.TotalMinutes + 0.5)).ToString(), font8, Brushes.Black, rectangle23.Right, num28, stringFormat10);
				e.Graphics.DrawString("Time", font, Brushes.Black, (rectangle23.Left + rectangle23.Right) / 2, rectangle23.Bottom + 68, stringFormat10);
				stringFormat10.LineAlignment = StringAlignment.Center;
				e.Graphics.DrawString("P\r\nl\r\na\r\nc\r\ne", font, Brushes.Black, rectangle23.Left - 54, (rectangle23.Bottom + rectangle23.Top) / 2, stringFormat10);
				int num29 = rectangle23.Top - 70;
				int num30 = rectangle23.Left + 10;
				int num31 = (rectangle23.Right + rectangle23.Left) / 2 - 30;
				int num32 = rectangle23.Right - 85;
				stringFormat10.Alignment = StringAlignment.Near;
				DrawScoreMarker(e.Graphics, pen2, num30, num29, 15f);
				e.Graphics.DrawString("Score", font7, Brushes.Black, num30 + 15, num29, stringFormat10);
				DrawKillMarker(e.Graphics, pen2, num31, num29, 15f);
				e.Graphics.DrawString("Kill", font7, Brushes.Black, num31 + 15, num29, stringFormat10);
				DrawDeathMarker(e.Graphics, pen2, num32, num29, 15f);
				e.Graphics.DrawString("Death", font7, Brushes.Black, num32 + 15, num29, stringFormat10);
			}
			foreach (MissionEvent event3 in mMissionResults.Events)
			{
				if (event3 is ScoredEvent)
				{
					DrawScoreMarker(e.Graphics, (event3.ReportingPilot == rPPlayer) ? Pens.Black : Pens.LightGray, GetMarkerPosition(event3.GameTime, mPlayerLines[event3.ReportingPilot], rectangle23), 10f);
				}
				else if (event3 is KilledEvent)
				{
					KilledEvent killedEvent3 = (KilledEvent)event3;
					if (killedEvent3.Killer != killedEvent3.ReportingPilot)
					{
						DrawKillMarker(e.Graphics, (killedEvent3.ReportingPilot == rPPlayer || killedEvent3.Killer == rPPlayer) ? Pens.Black : Pens.LightGray, GetMarkerPosition(killedEvent3.GameTime, mPlayerLines[killedEvent3.Killer], rectangle23), 10f);
					}
					DrawDeathMarker(e.Graphics, (killedEvent3.ReportingPilot == rPPlayer || killedEvent3.Killer == rPPlayer) ? Pens.Black : Pens.LightGray, GetMarkerPosition(killedEvent3.GameTime, mPlayerLines[killedEvent3.ReportingPilot], rectangle23), 10f);
				}
			}
		}
		e.HasMorePages = ++mCurrentPlayer < mPrintOrder.Length;
		base.OnPrintPage(e);
	}

	private PointF GetMarkerPosition(TimeSpan time, List<Point> chartLine, RectangleF chartArea)
	{
		TimeSpan timeSpan = TimeSpan.Zero;
		TimeSpan timeSpan2 = mMissionResults.ActuallMissionTime;
		int num = 0;
		foreach (KeyValuePair<TimeSpan, int> mChartCol in mChartCols)
		{
			timeSpan2 = mChartCol.Key;
			if (mChartCol.Key < time)
			{
				num++;
				timeSpan = timeSpan2;
				continue;
			}
			break;
		}
		if (timeSpan == timeSpan2)
		{
			timeSpan2 = mMissionResults.ActuallMissionTime;
		}
		float num2 = ((float)time.Ticks - (float)timeSpan.Ticks) / ((float)timeSpan2.Ticks - (float)timeSpan.Ticks);
		PointF pointF = chartLine[num];
		PointF pointF2 = chartLine[num + 1];
		return new PointF((pointF2.X - pointF.X) * num2 + pointF.X, (pointF2.Y - pointF.Y) * num2 + pointF.Y);
	}

	private static void DrawKillMarker(Graphics graphics, Pen pen, PointF point, float size)
	{
		DrawKillMarker(graphics, pen, point.X, point.Y, size);
	}

	private static void DrawKillMarker(Graphics graphics, Pen pen, float x, float y, float size)
	{
		float y2 = y + size / 2f;
		graphics.DrawPolygon(pen, new PointF[3]
		{
			new PointF(x - size / 2f, y2),
			new PointF(x + size / 2f, y2),
			new PointF(x, y - size / 2f)
		});
	}

	private static void DrawDeathMarker(Graphics graphics, Pen pen, PointF point, float size)
	{
		DrawDeathMarker(graphics, pen, point.X, point.Y, size);
	}

	private static void DrawDeathMarker(Graphics graphics, Pen pen, float x, float y, float size)
	{
		graphics.DrawRectangle(pen, x - size / 2f, y - size / 2f, size, size);
	}

	private static void DrawScoreMarker(Graphics graphics, Pen pen, PointF point, float size)
	{
		DrawScoreMarker(graphics, pen, point.X, point.Y, size);
	}

	private static void DrawScoreMarker(Graphics graphics, Pen pen, float x, float y, float size)
	{
		graphics.DrawEllipse(pen, x - size / 2f, y - size / 2f, size, size);
	}

	private static void DrawPlayerLine(Graphics graphics, RPPlayer player, Point[] points, int playerNameX, Pen pen, Brush brush, Font font, StringFormat format)
	{
		graphics.DrawLines(pen, points);
		graphics.DrawString(player.Name, font, brush, playerNameX, points[points.Length - 1].Y, format);
	}

	private static void GetPlayerStats(out int laps, out TimeSpan fastestLap, out int deaths, out int kills, RPPlayer player, IEnumerable<MissionEvent> relaventEventsSorted)
	{
		laps = 0;
		fastestLap = TimeSpan.MaxValue;
		deaths = 0;
		kills = 0;
		bool flag = true;
		TimeSpan timeSpan = TimeSpan.Zero;
		foreach (MissionEvent item in relaventEventsSorted)
		{
			if (item.ReportingPilot == player)
			{
				if (item is ScoredEvent)
				{
					if (flag)
					{
						flag = false;
					}
					else
					{
						TimeSpan timeSpan2 = item.GameTime - timeSpan;
						if (timeSpan2 < fastestLap)
						{
							fastestLap = timeSpan2;
						}
						laps++;
					}
					timeSpan = item.GameTime;
				}
				else if (item is KilledEvent)
				{
					flag = true;
					deaths++;
				}
			}
			else if (item is KilledEvent && ((KilledEvent)item).Killer == player)
			{
				kills++;
			}
		}
		if (fastestLap == TimeSpan.MaxValue)
		{
			fastestLap = TimeSpan.Zero;
		}
	}

	private static void DrawRedXBox(Graphics g, Rectangle rect)
	{
		g.DrawRectangle(Pens.Red, rect);
		g.DrawLine(Pens.Red, rect.Left, rect.Top, rect.Right, rect.Bottom);
		g.DrawLine(Pens.Red, rect.Left, rect.Bottom, rect.Right, rect.Top);
	}

	private static Rectangle RectDrop(Rectangle inputRect, int widthDrop, int heightDrop)
	{
		return new Rectangle(inputRect.X, inputRect.Y, inputRect.Width - widthDrop, inputRect.Height - heightDrop);
	}

	private static Rectangle GetCenteredScaledRect(Rectangle bounds, Size originalSize)
	{
		float num = (float)bounds.Width / (float)bounds.Height;
		float num2 = (float)originalSize.Width / (float)originalSize.Height;
		Size size = bounds.Size;
		if (num > num2)
		{
			size.Width = (int)((float)size.Height * num2 + 0.5f);
		}
		else
		{
			size.Height = (int)((float)size.Width / num2 + 0.5f);
		}
		return new Rectangle(bounds.X + (bounds.Width - size.Width) / 2, bounds.Y + (bounds.Height - size.Height) / 2, size.Width, size.Height);
	}

	private static bool Contains(IEnumerable<RPPlayer> players, RPPlayer player)
	{
		foreach (RPPlayer player2 in players)
		{
			if (player2 == player)
			{
				return true;
			}
		}
		return false;
	}
}
