using Aki.Reflection.Patching;
using BepInEx;
using EFT.InventoryLogic;
using System.Reflection;

namespace IcyClawz.ItemSellPrice
{
    [BepInPlugin("com.IcyClawz.ItemSellPrice", "IcyClawz.ItemSellPrice", "1.0.2")]
    public class Plugin : BaseUnityPlugin
    {
        private void Awake()
        {
            new TraderPatch().Enable();
            new ItemPatch().Enable();
            new AmmoPatch().Enable();
            new GrenadePatch().Enable();
            new SecureContainerPatch().Enable();
        }
    }

    public class TraderPatch : ModulePatch
    {
        protected override MethodBase GetTargetMethod() =>
            typeof(TraderClass).GetConstructors()[0];
        
        [PatchPostfix]
        private static void PatchPostfix(ref TraderClass __instance)
        {
            __instance.UpdateSupplyData();
        }
    }

    public class ItemPatch : ModulePatch
    {
        protected override MethodBase GetTargetMethod() =>
            typeof(Item).GetConstructors()[0];

        [PatchPostfix]
        private static void PatchPostfix(ref Item __instance)
        {
            __instance.AddTraderOfferAttribute();
        }
    }

    public class AmmoPatch : ModulePatch
    {
        protected override MethodBase GetTargetMethod() =>
            typeof(BulletClass).GetConstructors()[0];

        [PatchPostfix]
        private static void PatchPostfix(ref BulletClass __instance)
        {
            __instance.AddTraderOfferAttribute();
        }
    }

    public class GrenadePatch : ModulePatch
    {
        protected override MethodBase GetTargetMethod() =>
            typeof(GrenadeClass).GetConstructors()[0];

        [PatchPostfix]
        private static void PatchPostfix(ref GrenadeClass __instance)
        {
            __instance.AddTraderOfferAttribute();
        }
    }

    public class SecureContainerPatch : ModulePatch
    {
        protected override MethodBase GetTargetMethod() =>
            typeof(ItemContainerClass).GetConstructors()[0];

        [PatchPostfix]
        private static void PatchPostfix(ref ItemContainerClass __instance)
        {
            __instance.AddTraderOfferAttribute();
        }
    }
}