using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using HarmonyLib.Tools;

// TODO: Make shield and repair bigger
// TODO: Maybe make props faster, thrusters work fine

namespace TerraTech {
    [BepInPlugin(PluginGuid, PluginName, PluginVersion)]
    public class Main : BaseUnityPlugin {
        private const string PluginGuid = "CykaMod";
        private const string PluginName = "CykaMod";
        private const string PluginVersion = "1.0.0";

        public static ConfigEntry<bool> debug;
        public static ConfigEntry<bool> debugBuyAll;
        public static ConfigEntry<bool> debugMultiBuy;
        public static ConfigEntry<int> multiBuyAmount;

        public static ConfigEntry<float> xpMultiplier;
        public static ConfigEntry<float> moneyMultiplier;
        public static ConfigEntry<float> heartbeatIntervalMultiplier;
        public static ConfigEntry<float> seekingProjectileVisionConeAngleMultiplier;
        public static ConfigEntry<float> seekingProjectileVisionRangeMultiplier;
        public static ConfigEntry<float> seekingProjectileTurningSpeedMultiplier;
        public static ConfigEntry<bool> projectileExplodeOnStick;

        public void Awake() {
            debug = Config.Bind("General", "Debug", false);
            debugBuyAll = Config.Bind("General", "Debug Buy All", false);
            debugMultiBuy = Config.Bind("General", "Debug Multi Buy", false);
            multiBuyAmount =
                Config.Bind("General", "Multi Buy Amount", 10,
                            new ConfigDescription("Multi Buy Amount", new AcceptableValueRange<int>(1, 10000)));
            float min = 0.01f;
            float max = 32f;

            ModuleWingManager.Setup(Config);
            ModuleBoosterManager.Setup(Config);
            ModuleShieldGeneratorManager.Setup(Config);
            ModuleWeaponGunManager.Setup(Config);
            ModuleEnergyManager.Setup(Config);
            ModuleEnergyStoreManager.Setup(Config);
            ModuleGyroManager.Setup(Config);
            ModuleItemHolderManager.Setup(Config);
            ModuleItemProducerManager.Setup(Config);
            ModuleRemoteChargerManager.Setup(Config);
            ModuleWheelsManager.Setup(Config);
            TankBeamManager.Setup(Config);
            ModuleWeaponManager.Setup(Config);
            ModuleHeartManager.Setup(Config);
            ModuleFuelTankManager.Setup(Config);
            TankManager.Setup(Config);

            xpMultiplier =
                Config.Bind("General", "XP Multiplier", 1f,
                            new ConfigDescription("XP Multiplier", new AcceptableValueRange<float>(min, max)));
            moneyMultiplier =
                Config.Bind("General", "Money Multiplier", 1f,
                            new ConfigDescription("Money Multiplier", new AcceptableValueRange<float>(min, max)));
            heartbeatIntervalMultiplier = Config.Bind(
                "General", "Heartbeat Interval Multiplier", 1f,
                new ConfigDescription("Heartbeat Interval Multiplier", new AcceptableValueRange<float>(min, max)));

            seekingProjectileVisionConeAngleMultiplier =
                Config.Bind("Weapons", "Seeking Projectile Cone Vision Angle Multiplier", 1f,
                            new ConfigDescription("Seeking Projectile Cone Vision Angle Multiplier",
                                                  new AcceptableValueRange<float>(min, max)));
            seekingProjectileVisionRangeMultiplier =
                Config.Bind("Weapons", "Seeking Projectile Vision Range Multiplier", 1f,
                            new ConfigDescription("Seeking Projectile Vision Range Multiplier",
                                                  new AcceptableValueRange<float>(min, max)));
            seekingProjectileTurningSpeedMultiplier =
                Config.Bind("Weapons", "Seeking Projectile Turn Speed Multiplier", 1f,
                            new ConfigDescription("Seeking Projectile Turn Speed Multiplier",
                                                  new AcceptableValueRange<float>(min, max)));
            projectileExplodeOnStick = Config.Bind(
                "Weapons", "Projectile Explode On Stick", false,
                new ConfigDescription("Projectile Explode On Stick", new AcceptableValueRange<bool>(false, true)));

            Logger.LogInfo("Cyka mod loaded");
            HarmonyFileLog.Enabled = true;
            Harmony harmony = new Harmony(PluginGuid);
            harmony.PatchAll();
            var originalMethods = harmony.GetPatchedMethods();
            Logger.LogInfo("Patched " + originalMethods.Count() + " methods");
        }
    }
}
