package com.leisuretimedock.blasttravelreborn.mixin;

import com.leisuretimedock.blasttravelreborn.content.entity.CannonEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
import net.minecraft.world.entity.Entity;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(ClientPacketListener.class)
public class ClientPlayNetworkHandlerMixin {
	@Shadow @Final private Minecraft minecraft;
	@Unique private Entity blasttravel$cachedMount = null;

	@Inject(method = "handleSetEntityPassengersPacket", locals = LocalCapture.CAPTURE_FAILEXCEPTION,
			at = @At(value = "INVOKE_ASSIGN", ordinal = 0, shift = At.Shift.AFTER, target = "Lnet/minecraft/client/multiplayer/ClientLevel;getEntity(I)Lnet/minecraft/world/entity/Entity;"))
	private void blasttravel$cacheMountedEntity(ClientboundSetPassengersPacket packet, CallbackInfo ci, Entity mounted) {
		blasttravel$cachedMount = mounted;
	}

	@ModifyVariable(method = "handleSetEntityPassengersPacket", index = 9,
			at = @At(value = "INVOKE_ASSIGN", ordinal = 0, shift = At.Shift.AFTER, target = "Lnet/minecraft/network/chat/Component;translatable(Ljava/lang/String;[Ljava/lang/Object;)Lnet/minecraft/network/chat/MutableComponent;"))
	private Component blasttravel$modifyMountMessage(Component old) {
		if (blasttravel$cachedMount instanceof CannonEntity) {
			return Component.translatable("mount.blasttravelreborn.cannon.onboard",
					this.minecraft.options.keyShift.getTranslatedKeyMessage(), this.minecraft.options.keyJump.getTranslatedKeyMessage());
		}
		return old;
	}
}
