using System;
using BepInEx.Configuration;
using HarmonyLib;

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

        private static ConfigEntry<bool> playerOnly;
        private static ConfigEntry<float> resourceGroundRadiusMultiplier;
        private static ConfigEntry<float> minDispenseIntervalMultiplier;
        private 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")]
        public static void PostfixCreate(ModuleItemProducer __instance) {
            Manager.OnObjectAttached(__instance);
        }

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