using System;
using birdseye;
using birdseye.Regiments;
using birdseye.Regiments.Commanders;
using birdseye.Regiments.Platoons.Modules;
using birdseye.Regiments.Units.Rigging;
using birdseye.Regiments.Units.WeaponSystems;
using birdseye.Regiments.Zones;
using HarmonyLib;
using UnityEngine;

namespace Regiments {
    [HarmonyPatch]
    public class Patches {
        private static float baseHqValue = 0;
        private static float baseSupplyValue = 0;

        [HarmonyPrefix]
        [HarmonyPatch(typeof(SupplyPointsController), "AddSPs")]
        static void SpMultiplier(ref float value) {
            if (value == 0) {
                return;
            }

            float multiplier = Main.SPMultiplier.Value;
            // Console.WriteLine("Multiplying SP {0} by {1}", value, multiplier);
            value *= multiplier;
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(TacAidPointsController), "AddTP")]
        static void TpMultiplier(ref float val) {
            if (val == 0) {
                return;
            }

            float multiplier = Main.TPMultiplier.Value;
            // Console.WriteLine("Multiplying TP {0} by {1}", val, multiplier);
            val *= multiplier;
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(MapZoneFactory), "MakeRefitZone")]
        static void SupplyAoeMultiplier(ref eSides side, ref Vector3 position, ref float size) {
            float multiplier = Main.SupplyAOEMultiplier.Value;
            Console.WriteLine("Multiplying supply aoe {0} by {1}", size, multiplier);
            size *= multiplier;
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(PlatoonHQVisuals), "Start")]
        static void HqAoeMultiplier(PlatoonHQVisuals __instance) {
            Console.WriteLine("Patching HQ radius - base range {0}", baseHqValue);
            if (baseHqValue == 0) {
                baseHqValue = __instance.platoon.parameters.HQRadius;
                Console.WriteLine("Base range is 0, assigning new: {0}", baseHqValue);
            }

            var hqRadius = Traverse.Create(__instance.platoon.parameters).Field("HQRadius");
            hqRadius.SetValue(baseHqValue * Main.HQAOEMultiplier.Value);
            Console.WriteLine("HQ radius patched to: {0}", __instance.platoon.parameters.HQRadius);
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(PlatoonSupplyModule), "Start")]
        static void SupplyAoeMultiplier(PlatoonSupplyModule __instance) {
            Console.WriteLine("Patching supply amount - base amount {0}", baseSupplyValue);
            if (baseSupplyValue == 0) {
                baseSupplyValue = __instance.platoon.parameters.supplyPointsPerUnit;
                Console.WriteLine("Base amount is 0, assigning new: {0}", baseSupplyValue);
            }

            __instance.platoon.parameters.supplyPointsPerUnit =
                (int)(baseSupplyValue * Main.SupplyAmountMultiplier.Value);
            Console.WriteLine("Supply amount patched to: {0}", __instance.platoon.parameters.supplyPointsPerUnit);
        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(WeaponAttackBase), "GetReloadInterval")]
        static void FireRateMultiplier(ref float __result) {
            Console.WriteLine("Patching fire rate");

            __result *= Main.FireRateMultiplier.Value;
            Console.WriteLine("Fire rate patched to: {0}", __result);
        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(WeaponAttackBase), "GetAimInterval")]
        static void AimingIntervalMultiplier(ref float __result) {
            Console.WriteLine("Patching aim interval - base amount {0}", baseSupplyValue);

            __result *= Main.AimIntervalMultiplier.Value;
            Console.WriteLine("Aim interval patched to: {0}", __result);
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(TimerManager), "GetPersistentTimer")]
        static void Transpiler(ref float interval, ref string hint) {
            if (hint == "burst") {
                Console.WriteLine("Patching burst interval from: {0}", interval);
                interval *= Main.FireRateMultiplier.Value;
                Console.WriteLine("Burst interval patched to: {0}", interval);
            }
        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(RefitZone), "OnRefitStep")]
        static void SupplyRateMultiplier(ref float hpVal, ref float ammoVal) {
            Console.WriteLine("Patching refit step from: {0} {1}", hpVal, ammoVal);
            hpVal *= Main.SupplyRateMultiplier.Value;
            ammoVal *= Main.SupplyRateMultiplier.Value;
            Console.WriteLine("Patching refit step to: {0} {1}", hpVal, ammoVal);
        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(UnitMovementRig), nameof(UnitMovementRig.maxSpeed), MethodType.Getter)]
        static void MovementSpeedMultiplier(ref float __result) {
            __result *= Main.MovementSpeedMultiplier.Value;
        }

        // [HarmonyPatch(typeof(AdvancedRulesData), "Validate")]
        // static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) {
        //     var codes = new List<CodeInstruction>(instructions);
        //
        //     foreach (var code in codes) {
        //         if (code.opcode == OpCodes.Ldc_R4) {
        //             Console.WriteLine("Changing " + code);
        //             code.operand = (float)code.operand * 2;
        //             Console.WriteLine("Changed " + code);
        //         }
        //     }
        //
        //     return codes.AsEnumerable();
        // }

        // [HarmonyPatch(typeof(AdvancedRulesData), "IsWithinLimits")]
        // static IEnumerable<CodeInstruction> Transpiler2(IEnumerable<CodeInstruction> instructions) {
        //     var codes = new List<CodeInstruction>(instructions);
        //
        //     foreach (var code in codes) {
        //         if (code.opcode == OpCodes.Ldc_R4) {
        //             Console.WriteLine("Changing " + code);
        //             code.operand = (float)code.operand * 2;
        //             Console.WriteLine("Changed " + code);
        //         }
        //     }
        //
        //     return codes.AsEnumerable();
        // }

        // private static float baseAccuracy;
        //
        // [HarmonyPostfix]
        // [HarmonyPatch(typeof(SkirmishAdvancedRules), "Update")]
        // static void AccuracyPatch(SkirmishAdvancedRules __instance) {
        //     var accMod = Traverse.Create(__instance).Field("currentARD").Field("accuracyModifier");
        //     if (baseAccuracy == 0) {
        //         baseAccuracy = (float)accMod.GetValue();
        //         Console.WriteLine("Base accuracy set to {0}", baseAccuracy);
        //     }
        //
        //     accMod.SetValue(baseAccuracy * 4f);
        //     Console.WriteLine("Accuracy now: {0}", accMod.GetValue());
        // }
    }
}
