using System;
using System.Collections.Generic;
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 CykaOfIndustry {
	[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<int> excavatorCapacityMultiplier;
		public static ConfigEntry<float> excavatorMineTileIterationsMultiplier;
		public static ConfigEntry<int> truckCapacityMultiplier;
		public static ConfigEntry<int> cargoShipCapacityMultiplier;
		public static ConfigEntry<float> excavatorMiningCapacityMultiplier;
		public static ConfigEntry<int> excavatorMiningAreaMultiplier;
		public static ConfigEntry<float> excavatorMinReachMultiplier;
		public static ConfigEntry<float> excavatorMaxReachMultiplier;
		public static ConfigEntry<float> bufferCapacityMultiplier;
		public static ConfigEntry<float> unityGenerationMultiplier;
		public static ConfigEntry<float> depotTransferSpeedMultiplier;
		public static ConfigEntry<float> worldMineSpeedMultiplier;
		public static ConfigEntry<float> housingCapacityMultiplier;
		public static ConfigEntry<float> dumpDelayMultiplier;
		public static ConfigEntry<float> transportSpeedMultiplier;
		public static ConfigEntry<float> shipyardCargoMultiplier;
		public static ConfigEntry<int> autosaveInterval;
		public static ConfigEntry<string> recipeMultipliers;
		public static Dictionary<string, int> recipeMultipliersDict;

		public static ConfigEntry<bool> debugMode;

		public void Awake() {
			excavatorCapacityMultiplier = Config.Bind("General", "Excavator Capacity Multiplier", 1,
				new ConfigDescription("Excavator Capacity Multiplier"));
			excavatorMineTileIterationsMultiplier = Config.Bind("General", "Excavator Mine Tile Iterations Multiplier",
				1f,
				new ConfigDescription("Excavator Mine Tile Iterations Multiplier"));
			truckCapacityMultiplier = Config.Bind("General", "Truck Capacity Multiplier", 1,
				new ConfigDescription("Truck Capacity Multiplier"));
			cargoShipCapacityMultiplier = Config.Bind("General", "Cargo Ship Capacity Multiplier", 1,
				new ConfigDescription("Cargo Ship Capacity Multiplier"));
			excavatorMiningAreaMultiplier = Config.Bind("General", "Excavator Mining Area Multiplier", 1,
				new ConfigDescription("Excavator Mining Area Multiplier"));
			excavatorMiningCapacityMultiplier = Config.Bind("General", "Excavator Mining Capacity Multiplier", 1f,
				new ConfigDescription("Excavator Mining Capacity Multiplier"));
			excavatorMinReachMultiplier = Config.Bind("General", "Excavator Min Reach Multiplier", 1f,
				new ConfigDescription("Excavator Min Reach Multiplier"));
			excavatorMaxReachMultiplier = Config.Bind("General", "Excavator Max Reach Multiplier", 1f,
				new ConfigDescription("Excavator Max Reach Multiplier"));
			bufferCapacityMultiplier = Config.Bind("General", "Buffer Capacity Multiplier", 1f,
				new ConfigDescription("Buffer Capacity Multiplier"));
			storageCapacityMultiplier = Config.Bind("General", "Storage Capacity Multiplier", 1f,
				new ConfigDescription("Storage Capacity Multiplier"));
			unityGenerationMultiplier = Config.Bind("General", "Unity Generation Multiplier", 1f,
				new ConfigDescription("Unity Generation Multiplier"));
			depotTransferSpeedMultiplier = Config.Bind("General", "Depot Transfer Speed Multiplier", 1f,
				new ConfigDescription("Depot Transfer Speed Multiplier"));
			worldMineSpeedMultiplier = Config.Bind("General", "World Mine Speed Multiplier", 1f,
				new ConfigDescription("World Mine Speed Multiplier"));
			housingCapacityMultiplier = Config.Bind("General", "Housing Capacity Multiplier", 1f,
				new ConfigDescription("Housing Capacity Multiplier"));
			dumpDelayMultiplier = Config.Bind("General", "Dump Delay Multiplier", 1f,
				new ConfigDescription("Dump Delay Multiplier"));
			transportSpeedMultiplier = Config.Bind("General", "Transport Speed Multiplier", 1f,
				new ConfigDescription("Transport Speed Multiplier"));
			shipyardCargoMultiplier = Config.Bind("General", "Shipyard Cargo Multiplier", 1f,
				new ConfigDescription("Shipyard Cargo Multiplier"));
			autosaveInterval = Config.Bind("General", "Autosave Interval", 1,
				new ConfigDescription("Autosave Interval in minutes"));
			recipeMultipliers = Config.Bind("General", "Recipe Multipliers", "dirt:4",
				new ConfigDescription("Recipe Multipliers"));

			recipeMultipliersDict = recipeMultipliers.Value.Split(',')
				.ToDictionary(x => x.Split(':')[0], x => int.Parse(x.Split(':')[1]));
			recipeMultipliers.SettingChanged += (sender, args) => {
				recipeMultipliersDict = recipeMultipliers.Value.Split(',')
					.ToDictionary(x => x.Split(':')[0], x => int.Parse(x.Split(':')[1]));
				Console.WriteLine("IndustrialCyka: Recipe multipliers parsed as:");
				foreach (var kvp in recipeMultipliersDict)
					Console.WriteLine("\tIndustrialCyka: {0} x {1}", kvp.Key, kvp.Value);
			};

			debugMode = Config.Bind("General", "Debug Mode", false, new ConfigDescription("Debug Mode"));

			// shootingSpeedMultiplier.SettingChanged += (sender, args) => WeaponPropertiesManager.DoPatch();
			// energyGenMultiplier.SettingChanged += (sender, args) => GeneratorPropertiesManager.DoPatch();
			// magnetStrenghtMultiplier.SettingChanged += (sender, args) => MagnetPropertiesManager.DoPatch();
			// magnetRadiusMultiplier.SettingChanged += (sender, args) => MagnetPropertiesManager.DoPatch();
			// beamStrenghtMultiplier.SettingChanged += (sender, args) => BeamPropertiesManager.DoPatch();
			// beamRadiusMultiplier.SettingChanged += (sender, args) => BeamPropertiesManager.DoPatch();
			// fuelTankRefillMultiplier.SettingChanged += (sender, args) => FuelPropertiesManager.DoPatch();
			// fuelTankCapacityMultiplier.SettingChanged += (sender, args) => FuelPropertiesManager.DoPatch();
			// wheelTorqueMultiplier.SettingChanged += (sender, args) => WheelPropertiesManager.DoPatch();
			// wheelSpeedMultiplier.SettingChanged += (sender, args) => WheelPropertiesManager.DoPatch();
			// jetThrustMultiplier.SettingChanged += (sender, args) => ThrusterPropertiesManager.DoPatch();
			// minerGroundArea.SettingChanged += (sender, args) => MinerPropertiesManager.DoPatch();
			// minerMiningSpeed.SettingChanged += (sender, args) => MinerPropertiesManager.DoPatch();
			// wirelessChargingPowerPerArcMultiplier.SettingChanged += (sender, args) => WirelessChargerPropertiesManager.DoPatch();
			// wirelessChargingArcFiringIntervalMultiplier.SettingChanged += (sender, args) => WirelessChargerPropertiesManager.DoPatch();
			// wirelessChargingRadiusMultiplier.SettingChanged += (sender, args) => WirelessChargerPropertiesManager.DoPatch();

			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");
			foreach (var method in originalMethods) {
				Logger.LogInfo("Patched " + method.Name);
			}
		}

		public static ConfigEntry<float> storageCapacityMultiplier;
	}
}