using System.Linq;
using almost;
using almost.WF;
using BepInEx;
using BepInEx.Configuration;
using com.ootii.Messages;
using HarmonyLib;
using HarmonyLib.Tools;
using MeshCombineStudio;

namespace WeedShop {
    [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<float> moneyMultiplier;
        public static ConfigEntry<float> weedQuantityMultiplier;
        public static ConfigEntry<float> weedQualityMultiplier;

        public void Awake() {
            moneyMultiplier = Config.Bind("General", "Money Multiplier", 2f, new ConfigDescription("Money Multiplier", new AcceptableValueRange<float>(2f, 32f)));
            weedQuantityMultiplier = Config.Bind("General", "Weed Quantity Multiplier", 2f, new ConfigDescription("Weed Quantity Multiplier", new AcceptableValueRange<float>(2f, 8f)));
            weedQualityMultiplier = Config.Bind("General", "Weed Quality Multiplier", 1.5f, new ConfigDescription("Weed Quality Multiplier", new AcceptableValueRange<float>(1.5f, 8f)));
            
            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");
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(WeedFactory), "OnMakeMoney")]
        private void OnMakeMoney(ref IMessage message) {
            Console.Log("OnMakeMoney " + message.Data);
            message.Data = (long)message.Data * moneyMultiplier.Value;
        }
        
        [HarmonyPrefix]
        [HarmonyPatch(typeof(ShopInventory), "AddWeed")]
        public virtual void AddWeed(BI item, ref float rawQuality, ref int quantity) {
            Console.Log("AddWeed " + item + " " + rawQuality + " " + quantity);
            quantity = (int)(quantity * weedQuantityMultiplier.Value);
            rawQuality *= weedQualityMultiplier.Value;
        }
    }
}