package com.leisuretimedock.blasttravelreborn.mixin;

import com.leisuretimedock.blasttravelreborn.util.PlayerEntityDuck;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
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;

@Mixin(PlayerModel.class)
public abstract class PlayerEntityModelMixin<T extends LivingEntity> extends HumanoidModel<T> {
	@Unique private LivingEntity blasttravel$cached;

	public PlayerEntityModelMixin(ModelPart modelPart) {
		super(modelPart);
	}

	@Inject(method = "setupAnim*", at = @At("HEAD"))
	private void blasttravel$cacheEntity(T entity, float f, float g, float h, float i, float j, CallbackInfo ci) {
		this.blasttravel$cached = entity;
	}

	@Inject(method = "setupAnim*", at = @At("TAIL"))
	private void blasttravel$setHeadAngle(T entity, float f, float g, float h, float i, float j, CallbackInfo ci) {
		if (entity instanceof PlayerEntityDuck duck && duck.blasttravel$inCannonFlight()) {
			this.head.setRotation(-0.5f * Mth.PI, 0, 0);
			this.hat.setRotation(-0.5f * Mth.PI, 0, 0);
		}
	}

	@ModifyVariable(method = "setupAnim*", at = @At("HEAD"), index = 2, argsOnly = true)
	private float blasttravel$overrideLimbAngles(float old) {
		if (this.blasttravel$cached instanceof PlayerEntityDuck duck && duck.blasttravel$inCannonFlight()) {
			this.blasttravel$cached = null;
			return 0.75f * Mth.PI;
		}
		return old;
	}
}
