using System;
using BepInEx.Configuration;
using HarmonyLib;

namespace Quasimorph {
    [HarmonyPatch]
    public class ModuleHeartManager {
        private static readonly MultipliedObjectManager<ModuleHeart> Manager =
            new MultipliedObjectManager<ModuleHeart>(ConfigureHeart);

        private static ConfigEntry<bool> playerOnly;
        private static ConfigEntry<float> eventHorizonRadiusMultiplier;
        private static ConfigEntry<float> setupTimeMultiplier;
        private static ConfigEntry<float> startShrinkingRadiusMultiplier;

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

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

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

            setupTimeMultiplier =
                config.Bind("Heart", "Setup Time Multiplier", 1f,
                            new ConfigDescription("Setup Time Multiplier", new AcceptableValueRange<float>(min, max)));
            setupTimeMultiplier.SettingChanged += (sender, args) => DoPatch();

            startShrinkingRadiusMultiplier = config.Bind(
                "Heart", "Start Shrinking Radius Multiplier", 1f,
                new ConfigDescription("Start Shrinking Radius Multiplier", new AcceptableValueRange<float>(min, max)));
            startShrinkingRadiusMultiplier.SettingChanged += (sender, args) => DoPatch();
        }

        private static void ConfigureHeart(MultipliedObject<ModuleHeart> obj) {
            obj.AddField(new FieldConfiguration<float, float>("m_EventHorizonRadius", eventHorizonRadiusMultiplier,
                                                              ShouldApply));
            obj.AddField(new FieldConfiguration<float, float>("m_SetupTime", setupTimeMultiplier, ShouldApply));
            obj.AddField(new FieldConfiguration<float, float>("m_StartShrinkingRadius", startShrinkingRadiusMultiplier,
                                                              ShouldApply));
        }

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

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

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