using System;
using System.Collections.Generic;
using HarmonyLib;

namespace TerraTech {
    [HarmonyPatch]
    public class BeamPropertiesManager {
        private static Dictionary<ModuleItemHolderBeam, float> strenghts = new Dictionary<ModuleItemHolderBeam, float>();
        private static Dictionary<ModuleItemHolderBeam, float> radii = new Dictionary<ModuleItemHolderBeam, float>();

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleItemHolderBeam), "OnAttached")]
        static void PostfixCreate(ModuleItemHolderBeam __instance) {
            // Console.WriteLine("ModuleItemHolderBeam.OnAttached");
            if (!strenghts.ContainsKey(__instance)) {
                strenghts.Add(__instance, GetStrength(__instance));
                radii.Add(__instance, GetRadius(__instance));
                // Console.WriteLine("Patching {0}; m_BeamStrength: {1}; m_Pickup.m_PickupRange: {2}", __instance.name, GetStrength(__instance), GetRadius(__instance));
                DoPatchSingle(__instance);
                // Console.WriteLine("Patched {0}; m_BeamStrength: {1}; m_Pickup.m_PickupRange: {2}", __instance.name, GetStrength(__instance), GetRadius(__instance));
            }
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleItemHolderBeam), "OnDetaching")]
        static void PostfixDestroy(ModuleItemHolderBeam __instance) {
            // Console.WriteLine("ModuleItemHolderBeam.OnDetaching");
            // Console.WriteLine("Restoring {0}; m_BeamStrength: {1}; m_Pickup.m_PickupRange: {2}", __instance.name, GetStrength(__instance), GetRadius(__instance));
            DoRestoreSingle(__instance);
            // Console.WriteLine("Restored {0}; m_BeamStrength: {1}; m_Pickup.m_PickupRange: {2}", __instance.name, GetStrength(__instance), GetRadius(__instance));
            strenghts.Remove(__instance);
            radii.Remove(__instance);
        }

        public static void DoPatch() {
            // Console.WriteLine("Modifying {0} ModuleItemHolderBeam", strenghts.Count);
            foreach (KeyValuePair<ModuleItemHolderBeam, float> keyValuePair in strenghts) {
                DoRestoreSingle(keyValuePair.Key);
                DoPatchSingle(keyValuePair.Key);
            }
        }

        static void DoPatchSingle(ModuleItemHolderBeam moduleItemHolderBeam) {
            SetStrength(moduleItemHolderBeam, strenghts[moduleItemHolderBeam] * Main.beamStrenghtMultiplier.Value);
            SetRadius(moduleItemHolderBeam, radii[moduleItemHolderBeam] * Main.beamRadiusMultiplier.Value);
        }

        static void DoRestoreSingle(ModuleItemHolderBeam moduleItemHolderBeam) {
            if (strenghts.ContainsKey(moduleItemHolderBeam)) {
                SetStrength(moduleItemHolderBeam, strenghts[moduleItemHolderBeam]);
                SetRadius(moduleItemHolderBeam, radii[moduleItemHolderBeam]);
            }
        }

        private static float GetStrength(ModuleItemHolderBeam moduleItemHolderBeam) {
            return Traverse.Create(moduleItemHolderBeam).Field("m_BeamStrength").GetValue() as float? ?? 0f;
        }

        private static void SetStrength(ModuleItemHolderBeam moduleItemHolderBeam, float value) {
            Traverse.Create(moduleItemHolderBeam).Field("m_BeamStrength").SetValue(value);
        }

        private static float GetRadius(ModuleItemHolderBeam moduleItemHolderBeam) {
            ModuleItemPickup moduleItemPickup = Traverse.Create(moduleItemHolderBeam).Field("m_Pickup").GetValue() as ModuleItemPickup;
            if (moduleItemPickup) {
                float radius = Traverse.Create(moduleItemPickup).Field("m_PickupRange").GetValue() as float? ?? 0f;
                return radius;
            }
            return 0f;
        }

        private static void SetRadius(ModuleItemHolderBeam moduleItemHolderBeam, float value) {
            ModuleItemPickup moduleItemPickup = Traverse.Create(moduleItemHolderBeam).Field("m_Pickup").GetValue() as ModuleItemPickup;
            if (moduleItemPickup) {
                Traverse.Create(moduleItemPickup).Field("m_PickupRange").SetValue(value);
            }
        }
    }
}