using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using Dalamud.Game.ClientState.Objects.Types;
using ImGuiNET;
using Lumina.Text;
using Newtonsoft.Json;

namespace CrystallineConflictWinsTracker;

internal class PluginUI : IDisposable
{
	private Configuration configuration;

	private bool settingsVisible;

	private bool visible;

	public List<PvpSeason> pvpSeasons = new List<PvpSeason>();

	private List<PvpWinEntry>? currentData;

	private string currentlySelectedClassName;

	private List<PvpWinEntry> currentClassGames;

	private bool pickedClass;

	private PvpSeason currentlySelectedSeason;

	private PvpSeason previouslySelectedSeason;

	private string previouslySelectedClassName;

	public string DataPath { get; }

	public bool SettingsVisible
	{
		get
		{
			return settingsVisible;
		}
		set
		{
			settingsVisible = value;
		}
	}

	public bool Visible
	{
		get
		{
			return visible;
		}
		set
		{
			visible = value;
		}
	}

	public PluginUI(Configuration configuration, string DataPath)
	{
		this.configuration = configuration;
		this.DataPath = DataPath;
		pvpSeasons.Add(new PvpSeason
		{
			startDate = new DateTime(1970, 1, 1),
			endDate = new DateTime(2095, 1, 1),
			season = 0,
			seasonLabel = "All"
		});
		pvpSeasons.Add(new PvpSeason
		{
			startDate = new DateTime(2022, 4, 12),
			endDate = new DateTime(2022, 7, 6),
			season = 1,
			seasonLabel = "Season 1"
		});
		pvpSeasons.Add(new PvpSeason
		{
			startDate = new DateTime(2022, 7, 6),
			endDate = new DateTime(2022, 8, 22),
			season = 2,
			seasonLabel = "Season 2"
		});
		pvpSeasons.Add(new PvpSeason
		{
			startDate = new DateTime(2022, 8, 23),
			endDate = new DateTime(2022, 10, 31),
			season = 3,
			seasonLabel = "Season 3"
		});
		pvpSeasons.Add(new PvpSeason
		{
			startDate = new DateTime(2022, 10, 31),
			endDate = new DateTime(2023, 1, 10),
			season = 4,
			seasonLabel = "Season 4"
		});
		pvpSeasons.Add(new PvpSeason
		{
			startDate = new DateTime(2023, 1, 10),
			endDate = new DateTime(2023, 4, 4),
			season = 5,
			seasonLabel = "Season 5"
		});
		pvpSeasons.Add(new PvpSeason
		{
			startDate = new DateTime(2023, 4, 4),
			endDate = new DateTime(2024, 10, 3),
			season = 6,
			seasonLabel = "Season 6"
		});
		pvpSeasons.Add(new PvpSeason
		{
			startDate = new DateTime(2023, 10, 3),
			season = 7,
			seasonLabel = "Season 7"
		});
		currentlySelectedSeason = pvpSeasons[7];
	}

	public void DrawMainWindow()
	{
		if (!Visible)
		{
			pickedClass = false;
			return;
		}
		List<PvpWinEntry> list = (currentData = JsonConvert.DeserializeObject<List<PvpWinEntry>>(File.ReadAllText(DataPath + "pvpstats.json")) ?? new List<PvpWinEntry>());
		List<PvpWinEntry> list2 = list.FindAll((PvpWinEntry c) => !c.matchDate.HasValue);
		List<PvpWinEntry> list3 = list.FindAll((PvpWinEntry c) => c.matchDate >= new DateTime(2022, 7, 6) && c.matchDate <= new DateTime(2022, 8, 22));
		List<PvpWinEntry> list4 = list.FindAll((PvpWinEntry c) => c.matchDate >= new DateTime(2022, 8, 22) && c.matchDate <= new DateTime(2022, 10, 31));
		List<PvpWinEntry> list5 = list.FindAll((PvpWinEntry c) => c.matchDate >= new DateTime(2022, 11, 1) && c.matchDate <= new DateTime(2023, 1, 10));
		List<PvpWinEntry> list6 = list.FindAll((PvpWinEntry c) => c.matchDate >= new DateTime(2023, 1, 10) && c.matchDate <= new DateTime(2023, 4, 4));
		List<PvpWinEntry> list7 = list.FindAll((PvpWinEntry c) => c.matchDate >= new DateTime(2023, 4, 4) && c.matchDate <= new DateTime(2023, 10, 3));
		List<PvpWinEntry> list8 = list.FindAll((PvpWinEntry c) => c.matchDate >= new DateTime(2023, 10, 3));
		if (currentlySelectedSeason != null && currentlySelectedSeason.season != 0)
		{
			List<PvpWinEntry> list9 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
			switch (currentlySelectedSeason.season)
			{
			case 1:
				currentData = list2;
				list9 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
				currentClassGames = list9;
				break;
			case 2:
				currentData = list3;
				list9 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
				currentClassGames = list9;
				break;
			case 3:
				currentData = list4;
				list9 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
				currentClassGames = list9;
				break;
			case 4:
				currentData = list5;
				list9 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
				currentClassGames = list9;
				break;
			case 5:
				currentData = list6;
				list9 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
				currentClassGames = list9;
				break;
			case 6:
				currentData = list7;
				list9 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
				currentClassGames = list9;
				break;
			case 7:
				currentData = list8;
				list9 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
				currentClassGames = list9;
				break;
			}
		}
		List<string> list10 = new List<string>();
		foreach (PvpWinEntry currentDatum in currentData)
		{
			if (!list10.Contains(currentDatum.className))
			{
				list10.Add(currentDatum.className);
			}
		}
		if (!pickedClass && DalamudApi.ClientState.IsLoggedIn && (GameObject)(object)DalamudApi.ClientState.LocalPlayer != (GameObject)null && ((Character)DalamudApi.ClientState.LocalPlayer).ClassJob.GameData != null)
		{
			currentlySelectedClassName = SeString.op_Implicit(((Character)DalamudApi.ClientState.LocalPlayer).ClassJob.GameData.Name);
			List<PvpWinEntry> list11 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
			currentClassGames = list11;
		}
		ImGui.SetNextWindowSize(new Vector2(700f, 330f), (ImGuiCond)4);
		ImGui.SetNextWindowSizeConstraints(new Vector2(700f, 330f), new Vector2(float.MaxValue, float.MaxValue));
		if (ImGui.Begin("Crystalline Conflict stats", ref visible, (ImGuiWindowFlags)24) && (GameObject)(object)DalamudApi.ClientState.LocalPlayer != (GameObject)null && ((Character)DalamudApi.ClientState.LocalPlayer).ClassJob.GameData != null)
		{
			if (ImGui.BeginCombo("###seasonSelection", (currentlySelectedSeason == null) ? "All" : currentlySelectedSeason.seasonLabel))
			{
				foreach (PvpSeason pvpSeason in pvpSeasons)
				{
					if (ImGui.Selectable(pvpSeason.seasonLabel))
					{
						currentlySelectedSeason = pvpSeason;
					}
				}
				ImGui.EndCombo();
			}
			if (ImGui.BeginCombo("###jobSelection", (currentlySelectedClassName == null) ? SeString.op_Implicit(((Character)DalamudApi.ClientState.LocalPlayer).ClassJob.GameData.Name) : currentlySelectedClassName))
			{
				foreach (string item in list10)
				{
					if (ImGui.Selectable(item))
					{
						currentlySelectedClassName = item;
						List<PvpWinEntry> list12 = currentData.FindAll((PvpWinEntry b) => b.className == currentlySelectedClassName);
						currentClassGames = list12;
						pickedClass = true;
					}
				}
				ImGui.EndCombo();
			}
		}
		ImGui.Separator();
		if (currentClassGames != null)
		{
			List<PvpWinEntry> list13 = currentClassGames.FindAll((PvpWinEntry b) => !b.win);
			List<PvpWinEntry> list14 = currentClassGames.FindAll((PvpWinEntry b) => b.win);
			int count = list14.FindAll((PvpWinEntry b) => b.ranked).Count;
			int count2 = list14.FindAll((PvpWinEntry b) => !b.ranked).Count;
			int count3 = list13.FindAll((PvpWinEntry b) => b.ranked).Count;
			int count4 = list13.FindAll((PvpWinEntry b) => !b.ranked).Count;
			decimal num = default(decimal);
			if (count3 > 0 || count > 0)
			{
				num = Math.Round(decimal.Divide(count, count + count3) * 100m, 2);
			}
			decimal num2 = default(decimal);
			if (count2 > 0 || count4 > 0)
			{
				num2 = Math.Round(decimal.Divide(count2, count2 + count4) * 100m, 2);
			}
			ImGui.Text(currentlySelectedClassName + " stats");
			ImGui.TextColored(new Vector4(0f, 1f, 1f, 1f), "Ranked");
			ImGui.TextColored(new Vector4(1f, 0f, 0f, 1f), "Losses: " + count3);
			ImGui.SameLine();
			ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f), "Wins: " + count);
			ImGui.SameLine();
			ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f), "(WR: " + num + " %%)");
			ImGui.TextColored(new Vector4(0f, 1f, 1f, 1f), "Casual");
			ImGui.TextColored(new Vector4(1f, 0f, 0f, 1f), "Losses: " + count4);
			ImGui.SameLine();
			ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f), "Wins: " + count2);
			ImGui.SameLine();
			ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f), "(WR: " + num2 + " %%)");
			ImGui.Separator();
			List<PvpWinEntry> list15 = currentData.FindAll((PvpWinEntry w) => w.win && w.ranked);
			List<PvpWinEntry> list16 = currentData.FindAll((PvpWinEntry w) => !w.win && w.ranked);
			decimal num3 = default(decimal);
			if (list15.Count > 0 && list16.Count > 0)
			{
				num3 = Math.Round(decimal.Divide(list15.Count, list15.Count + list16.Count) * 100m, 2);
			}
			ImGui.Text("Total stats");
			ImGui.TextColored(new Vector4(0f, 1f, 1f, 1f), "Ranked");
			ImGui.TextColored(new Vector4(1f, 0f, 0f, 1f), "Losses: " + list16.Count);
			ImGui.SameLine();
			ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f), "Wins: " + list15.Count);
			ImGui.SameLine();
			ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f), "(WR: " + num3 + " %%)");
			List<PvpWinEntry> list17 = currentData.FindAll((PvpWinEntry w) => w.win && !w.ranked);
			List<PvpWinEntry> list18 = currentData.FindAll((PvpWinEntry w) => !w.win && !w.ranked);
			decimal num4 = default(decimal);
			if (list17.Count > 0 && list18.Count > 0)
			{
				num4 = Math.Round(decimal.Divide(list17.Count, list17.Count + list18.Count) * 100m, 2);
			}
			ImGui.TextColored(new Vector4(0f, 1f, 1f, 1f), "Casual");
			ImGui.TextColored(new Vector4(1f, 0f, 0f, 1f), "Losses: " + list18.Count);
			ImGui.SameLine();
			ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f), "Wins: " + list17.Count);
			ImGui.SameLine();
			ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f), "(WR: " + num4 + " %%)");
		}
		ImGui.End();
	}

	public void Dispose()
	{
	}

	public void Draw()
	{
		DrawMainWindow();
		DrawSettingsWindow();
	}

	public void DrawSettingsWindow()
	{
		if (!SettingsVisible)
		{
			return;
		}
		ImGui.SetNextWindowSize(new Vector2(450f, 100f), (ImGuiCond)1);
		if (ImGui.Begin("World Map Enhancer Settings", ref settingsVisible, (ImGuiWindowFlags)58))
		{
			bool enabled = configuration.Enabled;
			if (ImGui.Checkbox("Enable zooming out with right click", ref enabled))
			{
				configuration.Enabled = enabled;
				configuration.Save();
			}
			int rightClickDelay = configuration.RightClickDelay;
			if (ImGui.InputInt("Right click release delay", ref rightClickDelay, 25, 100))
			{
				configuration.RightClickDelay = rightClickDelay;
				configuration.Save();
			}
		}
		ImGui.End();
	}
}
