using System;
using System.Collections.Generic;
using System.Text;
using CykaOfQud;
using HarmonyLib;
using XRL.World;

namespace CykaOfIndustry {
	[HarmonyPatch]
	public class Patches {
		[HarmonyPrefix]
		[HarmonyPatch(typeof(XRL.World.Parts.Experience), "HandleEvent")]
		static void xpMultiplier(ref AwardedXPEvent E) {
			if (Main.debugMode.Value)
				Console.WriteLine("CykaOfQud: AwardedXPEvent: Old XP {0}", E.Amount);
			E.Amount = (int)(E.Amount * Main.xpMultiplier.Value);
			if (Main.debugMode.Value)
				Console.WriteLine("CykaOfQud: 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("CykaOfQud: AddHitpoints: Old HP for level {0}", HPGain);
			if (HPGain == 0)
				HPGain = 1;
			HPGain = (int)(HPGain * Main.hitpointsPerLevelMultiplier.Value);
			if (Main.debugMode.Value)
				Console.WriteLine("CykaOfQud: 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("CykaOfQud: AddSkillPoints: Old SP for level {0}", SPGain);
			if (SPGain == 0)
				SPGain = 1;
			SPGain = (int)(SPGain * Main.skillPointsPerLevelMultiplier.Value);
			if (Main.debugMode.Value)
				Console.WriteLine("CykaOfQud: 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("CykaOfQud: AddMutationPoints: Old MP for level {0}", MPGain);
			if (MPGain == 0)
				MPGain = 1;
			MPGain = (int)(MPGain * Main.mutationPointsPerLevelMultiplier.Value);
			if (Main.debugMode.Value)
				Console.WriteLine("CykaOfQud: 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("CykaOfQud: AddAttributePoints: Old AP for level {0}", APGain);
			if (APGain == 0)
				APGain = 1;
			APGain = (int)(APGain * Main.attributePointsPerLevelMultiplier.Value);
			if (Main.debugMode.Value)
				Console.WriteLine("CykaOfQud: 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("CykaOfQud: AddAttributeBonus: Old AB for level {0}", ABGain);
			ABGain = (int)(ABGain * Main.attributeBonusPerLevelMultiplier.Value);
			if (ABGain == 0)
				ABGain = 2;
			if (Main.debugMode.Value)
				Console.WriteLine("CykaOfQud: 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("CykaOfQud: 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("CykaOfQud: 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(
					"CykaOfQud: 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("CykaOfQud: GetFor: New Result {0}", __result);
		}
	}
}