package gvclib.client.event;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Vector3f;
import gvclib.item.ItemGunBase;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.ArmedModel;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.client.renderer.entity.layers.RenderLayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;

public class LayerGunBase<T extends LivingEntity, M extends EntityModel<T> & ArmedModel> extends RenderLayer<T, M> {
    public LayerGunBase(LivingEntityRenderer renderbase) {
        super(renderbase);
    }

    public void render(PoseStack p_117204_, MultiBufferSource p_117205_,
                       int p_117206_, T p_117207_, float p_117208_,
                       float p_117209_, float p_117210_, float p_117211_, float p_117212_, float p_117213_) {
        boolean flag = p_117207_.getMainArm() == HumanoidArm.RIGHT;
        ItemStack itemstack = flag ? p_117207_.getOffhandItem() : p_117207_.getMainHandItem();
        ItemStack itemstack1 = flag ? p_117207_.getMainHandItem() : p_117207_.getOffhandItem();
        if (!itemstack.isEmpty() || !itemstack1.isEmpty()) {
            p_117204_.pushPose();
            if (this.getParentModel().young) {
                float f = 0.5F;
                p_117204_.translate(0.0D, 0.75D, 0.0D);
                p_117204_.scale(0.5F, 0.5F, 0.5F);
            }

            this.renderArmWithItem(p_117207_, itemstack1, ItemTransforms.TransformType.THIRD_PERSON_RIGHT_HAND, HumanoidArm.RIGHT, p_117204_, p_117205_, p_117206_);
            this.renderArmWithItem(p_117207_, itemstack, ItemTransforms.TransformType.THIRD_PERSON_LEFT_HAND, HumanoidArm.LEFT, p_117204_, p_117205_, p_117206_);
            p_117204_.popPose();
        }
        /*boolean flag = entitylivingbaseIn.getPrimaryHand() == EnumHandSide.RIGHT;
        ItemStack itemstack = flag ? entitylivingbaseIn.getHeldItemOffhand() : entitylivingbaseIn.getHeldItemMainhand();
        ItemStack itemstack1 = flag ? entitylivingbaseIn.getHeldItemMainhand() : entitylivingbaseIn.getHeldItemOffhand();

        if (!itemstack.isEmpty() || !itemstack1.isEmpty())
        {
            GlStateManager.pushMatrix();

            if (this.livingEntityRenderer.getMainModel().isChild)
            {
                float f = 0.5F;
                GlStateManager.translate(0.0F, 0.625F, 0.0F);
                GlStateManager.rotate(-20.0F, -1.0F, 0.0F, 0.0F);
                GlStateManager.scale(0.5F, 0.5F, 0.5F);
            }

            this.renderHeldItem(entitylivingbaseIn, itemstack1, ItemCameraTransforms.TransformType.THIRD_PERSON_RIGHT_HAND, EnumHandSide.RIGHT);
            this.renderHeldItem(entitylivingbaseIn, itemstack, ItemCameraTransforms.TransformType.THIRD_PERSON_LEFT_HAND, EnumHandSide.LEFT);
            GlStateManager.popMatrix();

        }*/
    }
    protected void renderArmWithItem(LivingEntity entity, ItemStack itemstack, ItemTransforms.TransformType p_117187_,
                                     HumanoidArm p_117188_, PoseStack poseStack, MultiBufferSource multiBufferSource, int p_117191_) {
        if (!itemstack.isEmpty()) {
            /*p_117189_.pushPose();
            this.getParentModel().translateToHand(p_117188_, p_117189_);
            p_117189_.mulPose(Vector3f.XP.rotationDegrees(-90.0F));
            p_117189_.mulPose(Vector3f.YP.rotationDegrees(180.0F));
            boolean flag = p_117188_ == HumanoidArm.LEFT;
            p_117189_.translate((double)((float)(flag ? -1 : 1) / 16.0F), 0.125D, -0.625D);
            this.itemInHandRenderer.renderItem(p_117185_, p_117186_, p_117187_, flag, p_117189_, p_117190_, p_117191_);
            p_117189_.popPose();*/
            poseStack.pushPose();
            if (entity.isCrouching())
            {
                poseStack.translate(0.0F, 0.2F, 0.0F);
            }
        /*    if (p_188358_1_ instanceof EntityGVCLivingBase && p_188358_1_ != null) {
                EntityGVCLivingBase en = (EntityGVCLivingBase) p_188358_1_;
                if (en.getSneak())
                {
                    GlStateManager.translate(0.0F, 0.2F, 0.0F);
                }
            }*/
            // Forge: moved this call down, fixes incorrect offset while sneaking.
            HumanoidModel model = (HumanoidModel)this.getParentModel();
        //    model.leftArm. = false;
        //    model.bipedRightLeg.isHidden = false;
            ((HumanoidModel)this.getParentModel()).translateToHand(p_117188_, poseStack);
            poseStack.mulPose(Vector3f.XP.rotationDegrees(-90.0F));

            //GlStateManager.rotate(180.0F, 0.0F, 1.0F, 0.0F);
            poseStack.scale(0.1875F, 0.1875F, 0.1875F);
            boolean flag = p_117188_ == HumanoidArm.LEFT;
            poseStack.translate((float)((flag ? -1 : 1) / 16.0F) * -5.33F, 0.125F * 1.33F, -0.625F * -4.5F);//-5.33,-3.33,-4.5
            //GlStateManager.translate((float)(flag ? -1 : 1) / 16.0F, 0.125F, -0.625F);
            //Minecraft.getMinecraft().getItemRenderer().renderItemSide(p_188358_1_, p_188358_2_, p_188358_3_, flag);
            if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {//item
                ItemGunBase gun = (ItemGunBase) itemstack.getItem();
                gun.ModelLoad();
                boolean isreload = false;
                /*if (p_188358_1_ instanceof EntityGVCLivingBase && p_188358_1_ != null) {
                    EntityGVCLivingBase en = (EntityGVCLivingBase) p_188358_1_;
                    if (!en.getattacktask())
                    {
                        if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {//item
                            //ItemGunBase gun = (ItemGunBase) itemstack.getItem();
                            if(gun != null && gun.arm_l_posz > -1.0F) {
                                GlStateManager.rotate(80.0F, 0.0F, 1.0F, 0.0F);
                                GlStateManager.rotate(-50.0F, 0.0F, 0.0F, 1.0F);
                            }
                        }
                    }
                    if(en.getRemain_L() <= 0){
                        isreload = true;
                    }
                }*/
                {
                    //GL11.glNewList(gllist, GL11.GL_COMPILE);
                    Minecraft.getInstance().getEntityRenderDispatcher().textureManager.getTexture(new ResourceLocation(gun.obj_tex));
                    VertexConsumer vertexconsumer = multiBufferSource.getBuffer(RenderTypeGun.gunrender(new ResourceLocation(gun.obj_tex)));
                    gun.obj_model.renderPart(vertexconsumer,poseStack, "mat1");//gun.obj_model.renderPart("mat1");
                    gun.obj_model.renderPart(vertexconsumer,poseStack, "mat100");//gun.obj_model.renderPart("mat100");
                    gun.obj_model.renderPart(vertexconsumer,poseStack, "mat2");//gun.obj_model.renderPart("mat2");
                    if (!isreload)gun.obj_model.renderPart(vertexconsumer,poseStack, "mat3"); //gun.obj_model.renderPart("mat3");
               //     Layermat.rendersight(p_188358_1_, itemstack, gun);
               //     Layermat.renderattachment(p_188358_1_, itemstack, gun);
                    gun.obj_model.renderPart(vertexconsumer,poseStack, "mat25");//gun.obj_model.renderPart("mat25");
                    gun.obj_model.renderPart(vertexconsumer,poseStack, "mat31");//gun.obj_model.renderPart("mat31");
                    gun.obj_model.renderPart(vertexconsumer,poseStack, "mat32");//gun.obj_model.renderPart("mat32");
                }
        }
            poseStack.popPose();
    }
    //private void renderHeldItem(LivingEntity p_188358_1_, ItemStack itemstack, ItemCameraTransforms.TransformType p_188358_3_, EnumHandSide handSide)
    {
        /*if (!itemstack.isEmpty())
        {
            GlStateManager.pushMatrix();
            if (p_188358_1_.isSneaking())
            {
                GlStateManager.translate(0.0F, 0.2F, 0.0F);
            }
            if (p_188358_1_ instanceof EntityGVCLivingBase && p_188358_1_ != null) {
                EntityGVCLivingBase en = (EntityGVCLivingBase) p_188358_1_;
                if (en.getSneak())
                {
                    GlStateManager.translate(0.0F, 0.2F, 0.0F);
                }
            }
            // Forge: moved this call down, fixes incorrect offset while sneaking.
            ModelBiped model = (ModelBiped)this.livingEntityRenderer.getMainModel();
            model.bipedLeftArm.isHidden = false;
            model.bipedRightLeg.isHidden = false;
            ((ModelBiped)this.livingEntityRenderer.getMainModel()).postRenderArm(0.0625F, handSide);
            GlStateManager.rotate(-90.0F, 1.0F, 0.0F, 0.0F);

            //GlStateManager.rotate(180.0F, 0.0F, 1.0F, 0.0F);
            GL11.glScalef(0.1875F, 0.1875F, 0.1875F);
            boolean flag = handSide == EnumHandSide.LEFT;
            GlStateManager.translate((float)((flag ? -1 : 1) / 16.0F) * -5.33F, 0.125F * 1.33F, -0.625F * -4.5F);//-5.33,-3.33,-4.5
            //GlStateManager.translate((float)(flag ? -1 : 1) / 16.0F, 0.125F, -0.625F);
            //Minecraft.getMinecraft().getItemRenderer().renderItemSide(p_188358_1_, p_188358_2_, p_188358_3_, flag);
            if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {//item
                ItemGunBase gun = (ItemGunBase) itemstack.getItem();
                gun.ModelLoad();
                boolean isreload = false;
                if (p_188358_1_ instanceof EntityGVCLivingBase && p_188358_1_ != null) {
                    EntityGVCLivingBase en = (EntityGVCLivingBase) p_188358_1_;
                    if (!en.getattacktask())
                    {
                        if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {//item
                            //ItemGunBase gun = (ItemGunBase) itemstack.getItem();
                            if(gun != null && gun.arm_l_posz > -1.0F) {
                                GlStateManager.rotate(80.0F, 0.0F, 1.0F, 0.0F);
                                GlStateManager.rotate(-50.0F, 0.0F, 0.0F, 1.0F);
                            }
                        }
                    }
                    if(en.getRemain_L() <= 0){
                        isreload = true;
                    }
                }
                {
                    //GL11.glNewList(gllist, GL11.GL_COMPILE);
                    Minecraft.getMinecraft().renderEngine.bindTexture(new ResourceLocation(gun.obj_tex));
                    gun.obj_model.renderPart("mat1");
                    gun.obj_model.renderPart("mat100");
                    gun.obj_model.renderPart("mat2");
                    if(!isreload)gun.obj_model.renderPart("mat3");
                    Layermat.rendersight( p_188358_1_,  itemstack,  gun);
                    Layermat.renderattachment( p_188358_1_,  itemstack,  gun);
                    gun.obj_model.renderPart("mat25");
                    gun.obj_model.renderPart("mat31");
                    gun.obj_model.renderPart("mat32");/**
                    //GL11.glEndList();
                }
                //GL11.glCallList(gllist);

            }
            GlStateManager.popMatrix();
        }*/
    }}

}
