using System;
using BepInEx.Configuration;
using HarmonyLib;

namespace TerraTech {
    [HarmonyPatch]
    public class ModuleShieldGeneratorManager {
        private static readonly MultipliedObjectManager<ModuleShieldGenerator> manager =
            new MultipliedObjectManager<ModuleShieldGenerator>(ConfigureShieldGenerator);

        public static ConfigEntry<bool> playerOnly;
        public static ConfigEntry<float> radiusMultiplier;
        public static ConfigEntry<float> radiusMultiplierHealing;
        public static ConfigEntry<float> heartbeatIntervalMultiplier;
        public static ConfigEntry<float> powerUpDelayMultiplier;

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

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

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

            heartbeatIntervalMultiplier = config.Bind(
                "Shield", "Heartbeat Interval Multiplier", 1f,
                new ConfigDescription("Heartbeat Interval Multiplier", new AcceptableValueRange<float>(min, max)));
            heartbeatIntervalMultiplier.SettingChanged += (sender, args) => DoPatch();

            powerUpDelayMultiplier = config.Bind(
                "Shield", "Power Up Delay Multiplier", 1f,
                new ConfigDescription("Power Up Delay Multiplier", new AcceptableValueRange<float>(min, max)));
            powerUpDelayMultiplier.SettingChanged += (sender, args) => DoPatch();

            radiusMultiplierHealing = config.Bind(
                "Shield", "Radius Multiplier Healing", 1f,
                new ConfigDescription("Radius Multiplier Healing", new AcceptableValueRange<float>(min, max)));
            radiusMultiplierHealing.SettingChanged += (sender, args) => DoPatch();
        }

        private static void ConfigureShieldGenerator(MultipliedObject<ModuleShieldGenerator> obj) {
            obj.AddField(new FieldConfiguration<float, float>("m_HealingHeartbeatInterval", heartbeatIntervalMultiplier,
                                                              ShouldApply));

            obj.AddField(new FieldConfiguration<float, float>("m_Radius", radiusMultiplier, instance => {
                if (!ShouldApply(instance))
                    return radiusMultiplier;
                var shield = (ModuleShieldGenerator)instance;
                return shield.m_Healing ? radiusMultiplierHealing : radiusMultiplier;
            }));

            obj.AddField(new FieldConfiguration<float, float>("m_PowerUpDelay", powerUpDelayMultiplier, ShouldApply));
        }

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

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleShieldGenerator), "OnAttached")]
        static void PostfixCreate(ModuleShieldGenerator __instance) {
            manager.OnObjectAttached(__instance);
        }

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

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