using System;
using BepInEx.Configuration;
using HarmonyLib;

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

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

        public static void Setup(ConfigFile config) {
            float min = 0.01f;
            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")]
        static void PostfixCreate(ModuleRemoteCharger __instance) {
            manager.OnObjectAttached(__instance);
        }

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

        public static void DoPatch() {
            manager.ApplyAll();
        }
    }
}
