using System;
 using System.Collections.Generic;
 using System.Reflection.Emit;
 using HarmonyLib;
using UnityEngine.UI;

namespace CykaRaider
{
	[HarmonyPatch]
	public class Patches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameManager), "FixedUpdate")]
		static void lootTimeAdder(GameManager __instance) {
			var trav = Traverse.Create(__instance);
			var lootTime = trav.Field("lootTime").GetValue<int>();
			if (lootTime > 0) {
				if (Main.debugMode.Value)
					Console.WriteLine("CykaRaider: FixedUpdate: Old lootTime {0}", lootTime);
				lootTime += Main.lootTimeAdder.Value;
				if (Main.debugMode.Value)
					Console.WriteLine("CykaRaider: FixedUpdate: New lootTime {0}", lootTime);
				trav.Field("lootTime").SetValue(lootTime);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(DataHub), "GetFixedData")]
		static void maxStackMultiplier(DataHub __instance) {
			foreach (var itemData in __instance.itemDataDic) {
				if (itemData.Value.maxStack > 0) {
					if (Main.debugMode.Value)
						Console.WriteLine("CykaRaider: GetFixedData: Old maxStack {0}", itemData.Value.maxStack);
					itemData.Value.maxStack = (int)(itemData.Value.maxStack * Main.maxStackMultiplier.Value);
					if (Main.debugMode.Value)
						Console.WriteLine("CykaRaider: GetFixedData: New maxStack {0}", itemData.Value.maxStack);
				}
			}
		}
		
		[HarmonyPostfix]
		[HarmonyPatch(typeof(MyBagControl), "GetAllButtonShow")]
		static void autoLoot(MyBagControl __instance) {
			if (Main.autoLoot.Value) {
				if (Main.debugMode.Value)
					Console.WriteLine("CykaRaider: autoLoot: AutoLoot is enabled");
				var trav = Traverse.Create(__instance);
				var button = trav.Field("getAll_BT").GetValue<Button>();
				button.onClick.Invoke();
				if (Main.debugMode.Value)
					Console.WriteLine("CykaRaider: autoLoot: Button clicked");
				__instance.BeClosed();
				if (Main.debugMode.Value)
					Console.WriteLine("CykaRaider: autoLoot: Bag closed");
			}
		}
		
		[HarmonyPatch(typeof(NPCControl), "UpgradeSkill")]
		public static class upgradePriceMultiplier {
			static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) {
				if (Main.debugMode.Value)
					Console.WriteLine("CykaRaider: upgradePriceMultiplier: Transpiler");
				var codes = new List<CodeInstruction>(instructions);
				for (int i = 0; i < codes.Count; i++) {
					// Does not work for -125k.... idk why... go fix....
					if (codes[i].opcode == OpCodes.Ldc_I4 && ((int)codes[i].operand == 125000 || (int)codes[i].operand == -125000)) {
						if (Main.debugMode.Value)
							Console.WriteLine("CykaRaider: upgradePriceMultiplier: Old price {0}", codes[i].operand);
						codes[i].operand = (int)((int)codes[i].operand * Main.upgradePriceMultiplier.Value);
						if (Main.debugMode.Value)
							Console.WriteLine("CykaRaider: upgradePriceMultiplier: New price {0}", codes[i].operand);
					}
				}
				return codes;
			}
		}
		
		[HarmonyPatch(typeof(NPCControl), "RefreshSkill")]
		public static class refreshSkillPriceMultiplier {
			static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) {
				if (Main.debugMode.Value)
					Console.WriteLine("CykaRaider: refreshSkillPriceMultiplier: Transpiler");
				var codes = new List<CodeInstruction>(instructions);
				for (int i = 0; i < codes.Count; i++) {
					if (codes[i].opcode == OpCodes.Ldc_I4 && ((int)codes[i].operand == 125000 || (int)codes[i].operand == -125000)) {
						if (Main.debugMode.Value)
							Console.WriteLine("CykaRaider: refreshSkillPriceMultiplier: Old price {0}", codes[i].operand);
						codes[i].operand = (int)((int)codes[i].operand * Main.upgradePriceMultiplier.Value);
						if (Main.debugMode.Value)
							Console.WriteLine("CykaRaider: refreshSkillPriceMultiplier: New price {0}", codes[i].operand);
					}
				}
				return codes;
			}
		}
		//		[HarmonyPrefix]
		//		[HarmonyPatch(typeof(XRL.World.Parts.Experience), "HandleEvent")]
		//		static void xpMultiplier(ref AwardedXPEvent E) {
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AwardedXPEvent: Old XP {0}", E.Amount);
		//			E.Amount = (int)(E.Amount * Main.xpMultiplier.Value);
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AwardedXPEvent: New XP {0}", E.Amount);
		//		}
		//
		//		[HarmonyPrefix]
		//		[HarmonyPatch(typeof(XRL.World.Parts.Leveler), "AddHitpoints")]
		//		static void hitpointsPerLevelMultiplier(StringBuilder sb, ref int HPGain) {
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddHitpoints: Old HP for level {0}", HPGain);
		//			if (HPGain == 0)
		//				HPGain = 1;
		//			HPGain = (int)(HPGain * Main.hitpointsPerLevelMultiplier.Value);
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddHitpoints: New HP for level {0}", HPGain);
		//		}
		//
		//		[HarmonyPrefix]
		//		[HarmonyPatch(typeof(XRL.World.Parts.Leveler), "AddSkillPoints")]
		//		static void skillPointsPerLevelMultiplier(StringBuilder sb, ref int SPGain) {
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddSkillPoints: Old SP for level {0}", SPGain);
		//			if (SPGain == 0)
		//				SPGain = 1;
		//			SPGain = (int)(SPGain * Main.skillPointsPerLevelMultiplier.Value);
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddSkillPoints: New SP for level {0}", SPGain);
		//		}
		//
		//		[HarmonyPrefix]
		//		[HarmonyPatch(typeof(XRL.World.Parts.Leveler), "AddMutationPoints")]
		//		static void mutationPointsPerLevelMultiplier(StringBuilder sb, ref int MPGain) {
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddMutationPoints: Old MP for level {0}", MPGain);
		//			if (MPGain == 0)
		//				MPGain = 1;
		//			MPGain = (int)(MPGain * Main.mutationPointsPerLevelMultiplier.Value);
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddMutationPoints: New MP for level {0}", MPGain);
		//		}
		//
		//		[HarmonyPrefix]
		//		[HarmonyPatch(typeof(XRL.World.Parts.Leveler), "AddAttributePoints")]
		//		static void attributePointsPerLevelMultiplier(StringBuilder sb, ref int APGain) {
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddAttributePoints: Old AP for level {0}", APGain);
		//			if (APGain == 0)
		//				APGain = 1;
		//			APGain = (int)(APGain * Main.attributePointsPerLevelMultiplier.Value);
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddAttributePoints: New AP for level {0}", APGain);
		//		}
		//
		//		[HarmonyPrefix]
		//		[HarmonyPatch(typeof(XRL.World.Parts.Leveler), "AddAttributeBonus")]
		//		static void attributeBonusPerLevelMultiplier(StringBuilder sb, ref int ABGain) {
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddAttributeBonus: Old AB for level {0}", ABGain);
		//			ABGain = (int)(ABGain * Main.attributeBonusPerLevelMultiplier.Value);
		//			if (ABGain == 0)
		//				ABGain = 2;
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddAttributeBonus: New AB for level {0}", ABGain);
		//		}
		//
		//		[HarmonyPrefix]
		//		[HarmonyPatch(typeof(XRL.World.Parts.BitLocker), "AddBits")]
		//		static void disassemblyBonusMultiplier(ref string Bits) {
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddBits: Old {0}", Bits);
		//
		//			StringBuilder output = new StringBuilder();
		//			foreach (char c in Bits) {
		//				output.Append(new string(c, (int)Math.Ceiling(Main.disassemblyBonusMultiplier.Value)));
		//			}
		//
		//			Bits = output.ToString();
		//
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: AddBits: New {0}", Bits);
		//		}
		//
		//		[HarmonyPostfix]
		//		[HarmonyPatch(typeof(GetTinkeringBonusEvent), nameof(GetTinkeringBonusEvent.GetFor),
		//			new Type[] {
		//				typeof(GameObject), typeof(GameObject), typeof(string), typeof(int), typeof(int), typeof(int),
		//				typeof(bool), typeof(bool), typeof(bool), typeof(bool)
		//			},
		//			new ArgumentType[] {
		//				ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal,
		//				ArgumentType.Out, ArgumentType.Out, ArgumentType.Out, ArgumentType.Normal, ArgumentType.Normal
		//			})]
		//		static void reverseEngineerChanceMultiplier(GameObject Actor, GameObject Item, string Type, int BaseRating,
		//			int Bonus, ref int SecondaryBonus, ref bool Interrupt, ref bool PsychometryApplied, bool Interruptable,
		//			bool ForSifrah, ref int __result) {
		//			if (Main.debugMode.Value)
		//				Console.WriteLine(
		//					"CykaRaider: GetFor: Actor {0} Item {1} Type {2} BaseRating {3} Bonus {4} SecondaryBonus {5} Interrupt {6} PsychometryApplied {7} Interruptable {8} ForSifrah {9} Result {10}",
		//					Actor.ID, Item.ID, Type, BaseRating, Bonus, SecondaryBonus, Interrupt, PsychometryApplied,
		//					Interruptable, ForSifrah, __result);
		//			__result = (int)(__result * Main.reverseEngineerChanceMultiplier.Value);
		//			__result += Main.reverseEngineerChanceAdditional.Value;
		//			if (__result > 100)
		//				__result = 100;
		//			if (Main.debugMode.Value)
		//				Console.WriteLine("CykaRaider: GetFor: New Result {0}", __result);
		//		}
		//	}
	}
}
