using System;
using System.Collections.Generic;
using BepInEx.Configuration;
using HarmonyLib;

namespace BanquetForCyka {
    [HarmonyPatch]
    public class ModuleBoosterManager {
        private static readonly MultipliedObjectManager<FanJet> FanManager =
            new MultipliedObjectManager<FanJet>(ConfigureFanThruster);
        private static readonly MultipliedObjectManager<BoosterJet> JetManager =
            new MultipliedObjectManager<BoosterJet>(ConfigureJetThruster);

        private static ConfigEntry<bool> playerOnly;
        private static ConfigEntry<float> fanThrustMultiplier;
        private static ConfigEntry<float> jetThrustMultiplier;

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

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

            fanThrustMultiplier =
                config.Bind("Booster", "Fan Thrust Multiplier", 1f,
                            new ConfigDescription("Fan Thrust Multiplier", new AcceptableValueRange<float>(min, max)));
            fanThrustMultiplier.SettingChanged += (sender, args) => DoPatch();

            jetThrustMultiplier =
                config.Bind("Booster", "Jet Thrust Multiplier", 1f,
                            new ConfigDescription("Jet Thrust Multiplier", new AcceptableValueRange<float>(min, max)));
            jetThrustMultiplier.SettingChanged += (sender, args) => DoPatch();
        }

        private static void ConfigureFanThruster(MultipliedObject<FanJet> obj) {
            obj.AddField(new FieldConfiguration<float, float>("m_Force", fanThrustMultiplier, ShouldApply));
        }

        private static void ConfigureJetThruster(MultipliedObject<BoosterJet> obj) {
            obj.AddField(new FieldConfiguration<float, float>("m_Force", jetThrustMultiplier, ShouldApply));
        }

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

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleBooster), "OnAttached")]
        public static void PostfixCreate(ModuleBooster __instance) {
            var trav = Traverse.Create(__instance);
            var fans = trav.Field("fans").GetValue<List<FanJet>>();
            var jets = trav.Field("jets").GetValue<List<BoosterJet>>();

            foreach (var fan in fans) FanManager.OnObjectAttached(fan);
            foreach (var jet in jets) JetManager.OnObjectAttached(jet);
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(ModuleBooster), "OnDetaching")]
        public static void PostfixDestroy(ModuleBooster __instance) {
            var trav = Traverse.Create(__instance);
            var fans = trav.Field("fans").GetValue<List<FanJet>>();
            var jets = trav.Field("jets").GetValue<List<BoosterJet>>();

            foreach (var fan in fans) FanManager.OnObjectDetached(fan);
            foreach (var jet in jets) JetManager.OnObjectDetached(jet);
        }

        private static void DoPatch() {
            FanManager.ApplyAll();
            JetManager.ApplyAll();
        }

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