package com.atsuishio.superbwarfare.client.model.item;

import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.client.AnimationHelper;
import com.atsuishio.superbwarfare.client.overlay.CrossHairOverlay;
import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.item.gun.special.TaserItem;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import software.bernie.geckolib.animation.AnimationState;
import software.bernie.geckolib.cache.object.GeoBone;

public class TaserItemModel extends CustomGunModel<TaserItem> {

    @Override
    public ResourceLocation getAnimationResource(TaserItem animatable) {
        return Mod.loc("animations/taser.animation.json");
    }

    @Override
    public ResourceLocation getModelResource(TaserItem animatable) {
        return Mod.loc("geo/taser.geo.json");
    }

    @Override
    public ResourceLocation getTextureResource(TaserItem animatable) {
        return Mod.loc("textures/item/taser.png");
    }

    @Override
    public ResourceLocation getLODModelResource(TaserItem animatable) {
        return Mod.loc("geo/lod/taser.geo.json");
    }

    @Override
    public ResourceLocation getLODTextureResource(TaserItem animatable) {
        return Mod.loc("textures/item/taser.png");
    }

    @Override
    public void setCustomAnimations(TaserItem animatable, long instanceId, AnimationState<TaserItem> animationState) {
        Player player = Minecraft.getInstance().player;
        if (player == null) return;
        ItemStack stack = player.getMainHandItem();
        if (shouldCancelRender(stack, animationState)) return;

        GeoBone gun = getAnimationProcessor().getBone("bone");
        GeoBone shen = getAnimationProcessor().getBone("shen");

        float times = 0.6f * (float) Math.min(Minecraft.getInstance().getTimer().getRealtimeDeltaTicks(), 0.8);
        double zt = ClientEventHandler.zoomTime;
        double zp = ClientEventHandler.zoomPos;
        double zpz = ClientEventHandler.zoomPosZ;

        double fpz = ClientEventHandler.firePosZ * 13 * times;
        double fp = ClientEventHandler.firePos;
        double fr = ClientEventHandler.fireRot;

        shen.setPosX((float) (0.75f * ClientEventHandler.recoilHorizon * fpz * fp));
        shen.setPosY((float) (-0.03f * fp - 0.06f * fr));
        shen.setPosZ((float) (0.725 * fp + 0.34f * fr + 0.45 * fpz));
        shen.setRotX((float) (0.03f * fp + 0.02f * fr + 0.02f * fpz));
        shen.setRotY((float) (0.07f * ClientEventHandler.recoilHorizon * fpz));
        shen.setRotZ((float) ((0.08f + 0.1 * fr) * ClientEventHandler.recoilHorizon));

        shen.setPosX((float) (shen.getPosX() * (1 - 0.5 * zt)));
        shen.setPosY((float) (shen.getPosY() * (1 + 0.2 * zt)));
        shen.setPosZ((float) (shen.getPosZ() * (1 - 0.3 * zt)));
        shen.setRotX((float) (shen.getRotX() * (1 - 0.9 * zt)));
        shen.setRotY((float) (shen.getRotY() * (1 - 0.9 * zt)));
        shen.setRotZ((float) (shen.getRotZ() * (1 - 0.9 * zt)));

        CrossHairOverlay.gunRot = shen.getRotZ();

        gun.setPosX(1.82f * (float) zp);
        gun.setPosY(1.3f * (float) zp - (float) (0.3f * zpz));
        gun.setPosZ((float) zp + (float) (0.5f * zpz));
        gun.setRotZ((float) (0.05f * zpz));

        ClientEventHandler.gunRootMove(getAnimationProcessor());

        GeoBone camera = getAnimationProcessor().getBone("camera");
        GeoBone main = getAnimationProcessor().getBone("0");

        float numR = (float) (1 - 0.72 * zt);
        float numP = (float) (1 - 0.68 * zt);

        AnimationHelper.handleReloadShakeAnimation(stack, main, camera, numR, numP);
        ClientEventHandler.handleReloadShake(Mth.RAD_TO_DEG * camera.getRotX(), Mth.RAD_TO_DEG * camera.getRotY(), Mth.RAD_TO_DEG * camera.getRotZ());
    }
}
