using System;
using BepInEx.Configuration;
using HarmonyLib;

namespace BlacksmithMaster {
    [HarmonyPatch]
    public class ModuleRemoteChargerManager {
        private static readonly MultipliedObjectManager<ModuleRemoteCharger> Manager =
            new MultipliedObjectManager<ModuleRemoteCharger>(ConfigureModuleRemoteCharger);

        private static ConfigEntry<bool> playerOnly;
        private static ConfigEntry<float> arcFiringIntervalMultiplier;
        private static ConfigEntry<float> chargingRadiusMultiplier;
        private static ConfigEntry<float> powerTransferPerArcMultiplier;

        public static void Setup(ConfigFile config) {
            const float min = 0.01f;
            const float max = 32f;

            playerOnly = config.Bind("Remote Charger", "Player Only", false, new ConfigDescription("Player Only"));
            playerOnly.SettingChanged += (sender, args) => DoPatch();

            arcFiringIntervalMultiplier = config.Bind(
                "Remote Charger", "Arc Firing Interval Multiplier", 1f,
                new ConfigDescription("Arc Firing Interval Multiplier", new AcceptableValueRange<float>(min, max)));
            arcFiringIntervalMultiplier.SettingChanged += (sender, args) => DoPatch();

            chargingRadiusMultiplier = config.Bind(
                "Remote Charger", "Charging Radius Multiplier", 1f,
                new ConfigDescription("Charging Radius Multiplier", new AcceptableValueRange<float>(min, max)));
            chargingRadiusMultiplier.SettingChanged += (sender, args) => DoPatch();

            powerTransferPerArcMultiplier = config.Bind(
                "Remote Charger", "Power Transfer Per Arc Multiplier", 1f,
                new ConfigDescription("Power Transfer Per Arc Multiplier", new AcceptableValueRange<float>(min, max)));
            powerTransferPerArcMultiplier.SettingChanged += (sender, args) => DoPatch();
        }

        private static void ConfigureModuleRemoteCharger(MultipliedObject<ModuleRemoteCharger> obj) {
            obj.AddField(
                new FieldConfiguration<float, float>("m_ArcFiringInterval", arcFiringIntervalMultiplier, ShouldApply));
            obj.AddField(
                new FieldConfiguration<float, float>("m_ChargingRadius", chargingRadiusMultiplier, ShouldApply));
            obj.AddField(new FieldConfiguration<float, float>("m_PowerTransferPerArc", powerTransferPerArcMultiplier,
                                                              ShouldApply));
        }

        private static readonly Func<object, bool> ShouldApply = obj => {
            if (!playerOnly.Value)
                return true;
            return CykUtil.IsPlayerTank(obj as Module);
        };

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleRemoteCharger), "OnAttached")]
        public static void PostfixCreate(ModuleRemoteCharger __instance) {
            Manager.OnObjectAttached(__instance);
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleRemoteCharger), "OnDetaching")]
        public static void PostfixDestroy(ModuleRemoteCharger __instance) {
            Manager.OnObjectDetached(__instance);
        }

        private static void DoPatch() {
            Manager.ApplyAll();
        }

        public static readonly Func<Module, bool> Register = obj => {
            if (Main.debug.Value)
                Console.WriteLine("Registering ModuleRemoteCharger: {0}", obj);
            PostfixCreate(obj as ModuleRemoteCharger);
            return true;
        };
    }
}
