using System;
using System.Collections.Generic;
using BepInEx.Configuration;
using HarmonyLib;

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

        public static ConfigEntry<bool> playerOnly;
        public static ConfigEntry<float> eventHorizonRadiusMultiplier;
        public static ConfigEntry<float> setupTimeMultiplier;
        public 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")]
        static void PostfixCreate(ModuleHeart __instance) {
            manager.OnObjectAttached(__instance);
        }

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

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