using System;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using HarmonyLib.Tools;

namespace TavernDave {
	[BepInPlugin(pluginGuid, pluginName, pluginVersion)]
	public class Main : BaseUnityPlugin {
		private const string pluginGuid = "TavernDave";
		private const string pluginName = "TavernDave";
		private const string pluginVersion = "1.0.0";

		public static ConfigEntry<bool> debug;

		public static ConfigEntry<float> moneyMultiplier;
		public static ConfigEntry<int> fastSpeed;
		public static ConfigEntry<float> staffXpMultiplier;
		public static ConfigEntry<float> peoplePerMinuteMultiplier;
		public static ConfigEntry<float> peoplePerMinuteOffset;
		public static ConfigEntry<float> prestigeMultiplier;

		public void Awake() {
			debug = Config.Bind("General", "Debug", false);

			moneyMultiplier = Config.Bind("General", "MoneyMultiplier", 1f);
			fastSpeed = Config.Bind("General", "FastSpeed", 1);
			staffXpMultiplier = Config.Bind("General", "StaffXpMultiplier", 1f);
			peoplePerMinuteMultiplier = Config.Bind("General", "PeoplePerMinuteMultiplier", 1f);
			peoplePerMinuteOffset = Config.Bind("General", "PeoplePerMinuteOffset", 0f);
			prestigeMultiplier = Config.Bind("General", "PrestigeMultiplier", 1f);

			Logger.LogInfo("Cyka mod loaded");
			HarmonyFileLog.Enabled = true;
			Harmony harmony = new Harmony(pluginGuid);
			harmony.PatchAll();
			var originalMethods = harmony.GetPatchedMethods();
			Logger.LogInfo("Patched " + originalMethods.Count() + " methods");
			foreach (var method in originalMethods) {
				Logger.LogInfo("Patched " + method.Name);
			}
		}
	}

	[HarmonyPatch]
	public class Patches {
		[HarmonyPrefix]
		[HarmonyPatch(typeof(TavernModel), "ChangeMoney")]
		public static void PrefixMoney(ref int value) {
			if (Main.debug.Value)
				Console.WriteLine($"Money is {value}");
			if (value > 0) {
				value = (int)(value * Main.moneyMultiplier.Value);
				if (Main.debug.Value)
					Console.WriteLine($"Money modified to {value}");
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(UiController), "ChangeGameSpeed")]
		public static void PrefixSpeed(ref int gameSpeed) {
			if (Main.debug.Value)
				Console.WriteLine($"Game speed is {gameSpeed}");
			if (gameSpeed > 1) {
				gameSpeed = Main.fastSpeed.Value;
				if (Main.debug.Value)
					Console.WriteLine($"Game speed modified to {gameSpeed}");
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(StaffModel), "UpdateXp")]
		public static void PrefixXp(ref int id, ref int amount) {
			if (Main.debug.Value)
				Console.WriteLine($"Staff xp is {amount}");
			if (amount > 0) {
				amount = (int)(amount * Main.staffXpMultiplier.Value);
				if (Main.debug.Value)
					Console.WriteLine($"Staff xp modified to {amount}");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(TechTreeModel), nameof(TechTreeModel.GetBonusPeoplePerMinute))]
		public static void PostfixPeoplePerMinute(ref float __result) {
			__result *= Main.peoplePerMinuteMultiplier.Value;
			__result += Main.peoplePerMinuteOffset.Value;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(TavernModel), nameof(TavernModel.GetQuality))]
		public static void PostfixQuality(ref int __result) {
			if (Main.debug.Value)
				Console.WriteLine($"Quality is {__result}");
			if (__result > 0) {
				__result = (int)(__result * Main.prestigeMultiplier.Value);
				if (Main.debug.Value)
					Console.WriteLine($"Quality modified to {__result}");
			}
		}
	}
}