using System;
using BepInEx.Configuration;
using HarmonyLib;

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

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

        public static void Setup(ConfigFile config) {
            const float min = 0.01f;
            const 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));
            obj.AddField(new FieldConfiguration<float, float>("flapAngleRangeVisual", angleRangeMultiplier));

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

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

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleWing), "OnAttached")]
        public static void PostfixCreate(ModuleWing __instance) {
            if (playerOnly.Value && !CykUtil.IsPlayerTank(__instance))
                return;
            for (int i = 0; i < __instance.m_Aerofoils.Length; i++) {
                var aerofoil = __instance.m_Aerofoils[i];
                Manager.OnObjectAttached(aerofoil);
            }
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleWing), "OnDetaching")]
        public static void PostfixDestroy(ModuleWing __instance) {
            if (playerOnly.Value && !CykUtil.IsPlayerTank(__instance))
                return;
            foreach (var aerofoil in __instance.m_Aerofoils) Manager.OnObjectDetached(aerofoil);
        }

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

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