package jesse.keeblarcraft.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import jesse.keeblarcraft.GuiMgr.ShopKeeper.ShopKeeperHandler;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.packet.s2c.play.SetTradeOffersS2CPacket;
import net.minecraft.screen.ScreenHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ClientPlayNetworkHandler.class)
public abstract class ClientPlayNetworkHandlerMixin {
    @Inject(at = @At("TAIL"), method = "onSetTradeOffers")
    public void onSetTradeOffers(SetTradeOffersS2CPacket packet, CallbackInfo ci, @Local ScreenHandler screenHandler) {
        if (packet.getSyncId() == screenHandler.syncId && screenHandler instanceof ShopKeeperHandler shopKeeperHandler) {
            shopKeeperHandler.SetOffers(packet.getOffers());
            shopKeeperHandler.SetExperienceLevel(packet.getExperience());
            shopKeeperHandler.SetLevelProgress(packet.getLevelProgress());
            shopKeeperHandler.SetLeveled(packet.isLeveled());
            shopKeeperHandler.SetCanRefreshTrades(packet.isRefreshable());
        }
    }
}
