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

namespace TerraTech {
    [HarmonyPatch]
    public class ModuleBoosterManager {
        private static readonly MultipliedObjectManager<FanJet> fanManager =
            new MultipliedObjectManager<FanJet>(ConfigureFanThruster);
        private static readonly MultipliedObjectManager<BoosterJet> jetManager =
            new MultipliedObjectManager<BoosterJet>(ConfigureJetThruster);

        public static ConfigEntry<bool> playerOnly;
        public static ConfigEntry<float> fanThrustMultiplier;
        public 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")]
        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")]
        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);
        }

        public static void DoPatch() {
            fanManager.ApplyAll();
            jetManager.ApplyAll();
        }
    }
}
