using System;
using BepInEx.Configuration;
using HarmonyLib;

namespace TerraTech {
    [HarmonyPatch]
    public class ModuleWingManager {
        private static readonly MultipliedObjectManager<ModuleWing.Aerofoil> manager =
            new MultipliedObjectManager<ModuleWing.Aerofoil>(ConfigureAerofoil);

        public static ConfigEntry<bool> playerOnly;
        public static ConfigEntry<float> angleRangeMultiplier;
        public static ConfigEntry<float> turnSpeedMultiplier;
        public static ConfigEntry<float> liftStrengthMultiplier;

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

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

            angleRangeMultiplier =
                config.Bind("Aerofoil", "Angle Range Multiplier", 1f,
                            new ConfigDescription("Angle Range Multiplier", new AcceptableValueRange<float>(min, max)));
            angleRangeMultiplier.SettingChanged += (sender, args) => DoPatch();

            turnSpeedMultiplier =
                config.Bind("Aerofoil", "Turn Speed Multiplier", 1f,
                            new ConfigDescription("Turn Speed Multiplier", new AcceptableValueRange<float>(min, max)));
            turnSpeedMultiplier.SettingChanged += (sender, args) => DoPatch();

            liftStrengthMultiplier = config.Bind(
                "Aerofoil", "Lift Strength Multiplier", 1f,
                new ConfigDescription("Lift Strength Multiplier", new AcceptableValueRange<float>(min, max)));
            liftStrengthMultiplier.SettingChanged += (sender, args) => DoPatch();
        }

        private static void ConfigureAerofoil(MultipliedObject<ModuleWing.Aerofoil> obj) {
            obj.AddField(
                new FieldConfiguration<float, float>("flapAngleRangeActual", angleRangeMultiplier, ShouldApply));
            obj.AddField(
                new FieldConfiguration<float, float>("flapAngleRangeVisual", angleRangeMultiplier, ShouldApply));

            obj.AddField(new FieldConfiguration<float, float>("flapTurnSpeed", turnSpeedMultiplier, ShouldApply));
            obj.AddField(new FieldConfiguration<float, float>("liftStrength", liftStrengthMultiplier, ShouldApply));
        }

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

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleWing), "OnAttached")]
        static void PostfixCreate(ModuleWing __instance) {
            for (int i = 0; i < __instance.m_Aerofoils.Length; i++) {
                var aerofoil = __instance.m_Aerofoils[i];
                manager.OnObjectAttached(aerofoil);
            }
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleWing), "OnDetaching")]
        static void PostfixDestroy(ModuleWing __instance) {
            for (int i = 0; i < __instance.m_Aerofoils.Length; i++) {
                var aerofoil = __instance.m_Aerofoils[i];
                manager.OnObjectDetached(aerofoil);
            }
        }

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