using System;
using BepInEx.Configuration;
using HarmonyLib;

namespace TerraTech {
    [HarmonyPatch]
    public class ModuleWheelsManager {
        private static readonly MultipliedObjectManager<ManWheels.TorqueParams> TorqueParamsManager =
            new MultipliedObjectManager<ManWheels.TorqueParams>(ConfigureTorqueParams);

        private static ConfigEntry<bool> playerOnly;
        private static ConfigEntry<float> torqueRpmMultiplier;
        private static ConfigEntry<float> torqueMultiplier;

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

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

            torqueRpmMultiplier =
                config.Bind("TorqueParams", "Torque RPM Multiplier", 1f,
                            new ConfigDescription("Torque RPM Multiplier", new AcceptableValueRange<float>(min, max)));
            torqueRpmMultiplier.SettingChanged += (sender, args) => DoPatch();

            torqueMultiplier =
                config.Bind("TorqueParams", "Torque Multiplier", 1f,
                            new ConfigDescription("Torque Multiplier", new AcceptableValueRange<float>(min, max)));
            torqueMultiplier.SettingChanged += (sender, args) => DoPatch();
        }

        private static void ConfigureTorqueParams(MultipliedObject<ManWheels.TorqueParams> obj) {
            obj.AddField(new FieldConfiguration<float, float>("torqueCurveMaxRpm", torqueRpmMultiplier, ShouldApply));
            obj.AddField(new FieldConfiguration<float, float>("torqueCurveMaxTorque", torqueMultiplier, ShouldApply));
        }

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

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleWheels), "OnAttached")]
        public static void PostfixCreate(ModuleWheels __instance) {
            var trav = Traverse.Create(__instance);
            var torqueParams = trav.Field("torqueParams");
            TorqueParamsManager.OnObjectAttached(torqueParams.GetValue<ManWheels.TorqueParams>());
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleWheels), "OnDetaching")]
        public static void PostfixDestroy(ModuleWheels __instance) {
            var trav = Traverse.Create(__instance);
            var torqueParams = trav.Field("torqueParams");
            TorqueParamsManager.OnObjectDetached(torqueParams.GetValue<ManWheels.TorqueParams>());
        }

        private static void DoPatch() {
            TorqueParamsManager.ApplyAll();
        }

        public static readonly Func<Module, bool> Register = obj => {
            if (Main.debug.Value)
                Console.WriteLine("Registering ModuleWheels: {0}", obj);
            PostfixCreate(obj as ModuleWheels);
            return true;
        };
    }
}
