package net.minecraft.client.renderer.blockentity;

import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.client.renderer.Sheets;
import net.minecraft.client.resources.model.Material;
import net.minecraft.core.BlockPos;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.entity.BannerPattern;
import com.mojang.datafixers.util.Pair;
import java.util.List;
import net.minecraft.util.Mth;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.world.level.block.WallBannerBlock;
import net.minecraft.core.Direction;
import com.mojang.math.Vector3f;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.BannerBlock;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.world.level.block.entity.BannerBlockEntity;

public class BannerRenderer extends BlockEntityRenderer<BannerBlockEntity> {
    private final ModelPart flag;
    private final ModelPart pole;
    private final ModelPart bar;
    
    public BannerRenderer(final BlockEntityRenderDispatcher dsq) {
        super(dsq);
        this.flag = makeFlag();
        (this.pole = new ModelPart(64, 64, 44, 0)).addBox(-1.0f, -30.0f, -1.0f, 2.0f, 42.0f, 2.0f, 0.0f);
        (this.bar = new ModelPart(64, 64, 0, 42)).addBox(-10.0f, -32.0f, -1.0f, 20.0f, 2.0f, 2.0f, 0.0f);
    }
    
    public static ModelPart makeFlag() {
        final ModelPart dni1 = new ModelPart(64, 64, 0, 0);
        dni1.addBox(-10.0f, 0.0f, -2.0f, 20.0f, 40.0f, 1.0f, 0.0f);
        return dni1;
    }
    
    @Override
    public void render(final BannerBlockEntity bwc, final float float2, final PoseStack cyl, final MultiBufferSource dqt, final int integer5, final int integer6) {
        final List<Pair<BannerPattern, DyeColor>> list8 = bwc.getPatterns();
        if (list8 == null) {
            return;
        }
        final float float3 = 0.6666667f;
        final boolean boolean10 = bwc.getLevel() == null;
        cyl.pushPose();
        long long11;
        if (boolean10) {
            long11 = 0L;
            cyl.translate(0.5, 0.5, 0.5);
            this.pole.visible = true;
        }
        else {
            long11 = bwc.getLevel().getGameTime();
            final BlockState byj13 = bwc.getBlockState();
            if (byj13.getBlock() instanceof BannerBlock) {
                cyl.translate(0.5, 0.5, 0.5);
                final float float4 = -byj13.<Integer>getValue((Property<Integer>)BannerBlock.ROTATION) * 360 / 16.0f;
                cyl.mulPose(Vector3f.YP.rotationDegrees(float4));
                this.pole.visible = true;
            }
            else {
                cyl.translate(0.5, -0.1666666716337204, 0.5);
                final float float4 = -byj13.<Direction>getValue((Property<Direction>)WallBannerBlock.FACING).toYRot();
                cyl.mulPose(Vector3f.YP.rotationDegrees(float4));
                cyl.translate(0.0, -0.3125, -0.4375);
                this.pole.visible = false;
            }
        }
        cyl.pushPose();
        cyl.scale(0.6666667f, -0.6666667f, -0.6666667f);
        final VertexConsumer cyo13 = ModelBakery.BANNER_BASE.buffer(dqt, RenderType::entitySolid);
        this.pole.render(cyl, cyo13, integer5, integer6);
        this.bar.render(cyl, cyo13, integer5, integer6);
        final BlockPos fk14 = bwc.getBlockPos();
        final float float5 = (Math.floorMod(fk14.getX() * 7 + fk14.getY() * 9 + fk14.getZ() * 13 + long11, 100L) + float2) / 100.0f;
        this.flag.xRot = (-0.0125f + 0.01f * Mth.cos(6.2831855f * float5)) * 3.1415927f;
        this.flag.y = -32.0f;
        renderPatterns(cyl, dqt, integer5, integer6, this.flag, ModelBakery.BANNER_BASE, true, list8);
        cyl.popPose();
        cyl.popPose();
    }
    
    public static void renderPatterns(final PoseStack cyl, final MultiBufferSource dqt, final int integer3, final int integer4, final ModelPart dni, final Material eby, final boolean boolean7, final List<Pair<BannerPattern, DyeColor>> list) {
        dni.render(cyl, eby.buffer(dqt, RenderType::entitySolid), integer3, integer4);
        for (int integer5 = 0; integer5 < 17 && integer5 < list.size(); ++integer5) {
            final Pair<BannerPattern, DyeColor> pair10 = list.get(integer5);
            final float[] arr11 = ((DyeColor)pair10.getSecond()).getTextureDiffuseColors();
            final Material eby2 = new Material(boolean7 ? Sheets.BANNER_SHEET : Sheets.SHIELD_SHEET, ((BannerPattern)pair10.getFirst()).location(boolean7));
            dni.render(cyl, eby2.buffer(dqt, RenderType::entityNoOutline), integer3, integer4, arr11[0], arr11[1], arr11[2], 1.0f);
        }
    }
}
