using System;
using BepInEx.Configuration;
using HarmonyLib;

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

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

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

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleShieldGenerator), "OnDetaching")]
        public static void PostfixDestroy(ModuleShieldGenerator __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 ModuleShieldGenerator: {0}", obj);
            PostfixCreate(obj as ModuleShieldGenerator);
            return true;
        };
    }
}
