using System;
using BepInEx.Configuration;
using HarmonyLib;

namespace TerraTech {
    [HarmonyPatch]
    public class ModuleItemProducerManager {
        private static readonly MultipliedObjectManager<ModuleItemProducer> manager =
            new MultipliedObjectManager<ModuleItemProducer>(ConfigureModuleItemProducer);

        public static ConfigEntry<bool> playerOnly;
        public static ConfigEntry<float> resourceGroundRadiusMultiplier;
        public static ConfigEntry<float> minDispenseIntervalMultiplier;
        public static ConfigEntry<float> secPerItemProducedMultiplier;

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

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

            resourceGroundRadiusMultiplier = config.Bind(
                "Item Producer", "Resource Ground Radius Multiplier", 1f,
                new ConfigDescription("Resource Ground Radius Multiplier", new AcceptableValueRange<float>(min, max)));
            resourceGroundRadiusMultiplier.SettingChanged += (sender, args) => DoPatch();

            minDispenseIntervalMultiplier = config.Bind(
                "Item Producer", "Min Dispense Interval Multiplier", 1f,
                new ConfigDescription("Min Dispense Interval Multiplier", new AcceptableValueRange<float>(min, max)));
            minDispenseIntervalMultiplier.SettingChanged += (sender, args) => DoPatch();

            secPerItemProducedMultiplier = config.Bind(
                "Item Producer", "Sec Per Item Produced Multiplier", 1f,
                new ConfigDescription("Sec Per Item Produced Multiplier", new AcceptableValueRange<float>(min, max)));
            secPerItemProducedMultiplier.SettingChanged += (sender, args) => DoPatch();
        }

        private static void ConfigureModuleItemProducer(MultipliedObject<ModuleItemProducer> obj) {
            obj.AddField(new FieldConfiguration<float, float>("m_ResourceGroundRadius", resourceGroundRadiusMultiplier,
                                                              ShouldApply));
            obj.AddField(new FieldConfiguration<float, float>("m_MinDispenseInterval", minDispenseIntervalMultiplier,
                                                              ShouldApply));
            obj.AddField(new FieldConfiguration<float, float>("m_SecPerItemProduced", secPerItemProducedMultiplier,
                                                              ShouldApply));
        }

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

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

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

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