using System.Collections.Generic;
using HarmonyLib;

// TODO: Fix this, no workey
namespace TerraTech {
    [HarmonyPatch]
    public class WirelessChargerPropertiesManager {
        private static Dictionary<ModuleRemoteCharger, float> radius = new Dictionary<ModuleRemoteCharger, float>();
        private static Dictionary<ModuleRemoteCharger, float> transferPerArc = new Dictionary<ModuleRemoteCharger, float>();
        private static Dictionary<ModuleRemoteCharger, float> arcFiringInterval = new Dictionary<ModuleRemoteCharger, float>();

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleRemoteCharger), "OnAttached")]
        static void PostfixCreate(ModuleRemoteCharger __instance) {
            // Console.WriteLine("ModuleRemoteCharger.OnAttached");
            if (!radius.ContainsKey(__instance)) {
                radius.Add(__instance, __instance.m_ChargingRadius);
                transferPerArc.Add(__instance, __instance.m_PowerTransferPerArc);
                arcFiringInterval.Add(__instance, __instance.m_ArcFiringInterval);
                // Console.WriteLine("Patching {0}; m_RefillRate: {1}; m_Capacity: {2}", __instance.name, __instance.m_RefillRate, __instance.m_Capacity);
                DoPatchSingle(__instance);
                // Console.WriteLine("Patched {0}; m_RefillRate: {1}; m_Capacity: {2}", __instance.name, __instance.m_RefillRate, __instance.m_Capacity);
            }
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleRemoteCharger), "OnDetaching")]
        static void PostfixDestroy(ModuleRemoteCharger __instance) {
            // Console.WriteLine("ModuleRemoteCharger.OnAttached");
            // Console.WriteLine("Restoring {0}; m_RefillRate: {1}; m_Capacity: {2}", __instance.name, __instance.m_RefillRate, __instance.m_Capacity);
            DoRestoreSingle(__instance);
            // Console.WriteLine("Restored {0}; m_RefillRate: {1}; m_Capacity: {2}", __instance.name, __instance.m_RefillRate, __instance.m_Capacity);
            radius.Remove(__instance);
            transferPerArc.Remove(__instance);
            arcFiringInterval.Remove(__instance);
        }

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

        static void DoPatchSingle(ModuleRemoteCharger moduleRemoteCharger) {
            moduleRemoteCharger.m_ChargingRadius = radius[moduleRemoteCharger] * Main.wirelessChargingRadiusMultiplier.Value;
            moduleRemoteCharger.m_PowerTransferPerArc = radius[moduleRemoteCharger] * Main.wirelessChargingPowerPerArcMultiplier.Value;
            moduleRemoteCharger.m_ArcFiringInterval = radius[moduleRemoteCharger] * Main.wirelessChargingArcFiringIntervalMultiplier.Value;
        }

        static void DoRestoreSingle(ModuleRemoteCharger moduleRemoteCharger) {
            if (radius.ContainsKey(moduleRemoteCharger)) {
                moduleRemoteCharger.m_ChargingRadius = radius[moduleRemoteCharger];
                moduleRemoteCharger.m_PowerTransferPerArc = radius[moduleRemoteCharger];
                moduleRemoteCharger.m_ArcFiringInterval = radius[moduleRemoteCharger];
            }
        }
    }
}