package net.minecraft.client.resources;

import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import net.minecraft.Util;
import com.google.common.collect.Maps;
import java.util.HashMap;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import java.util.Collection;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import java.io.ByteArrayInputStream;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.server.packs.PackType;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.List;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.properties.ChestType;
import com.mojang.datafixers.util.Pair;
import java.util.Map;
import net.minecraft.server.packs.Pack;

public class PackAdapterV4 implements Pack {
    private static final Map<String, Pair<ChestType, ResourceLocation>> CHESTS;
    private static final List<String> PATTERNS;
    private static final Set<String> SHIELDS;
    private static final Set<String> BANNERS;
    public static final ResourceLocation SHIELD_BASE;
    public static final ResourceLocation BANNER_BASE;
    public static final ResourceLocation OLD_IRON_GOLEM_LOCATION;
    private final Pack pack;
    
    public PackAdapterV4(final Pack ye) {
        this.pack = ye;
    }
    
    @Override
    public InputStream getRootResource(final String string) throws IOException {
        return this.pack.getRootResource(string);
    }
    
    @Override
    public boolean hasResource(final PackType yf, final ResourceLocation sm) {
        if (!"minecraft".equals(sm.getNamespace())) {
            return this.pack.hasResource(yf, sm);
        }
        final String string4 = sm.getPath();
        if ("textures/misc/enchanted_item_glint.png".equals(string4)) {
            return false;
        }
        if ("textures/entity/iron_golem/iron_golem.png".equals(string4)) {
            return this.pack.hasResource(yf, PackAdapterV4.OLD_IRON_GOLEM_LOCATION);
        }
        if ("textures/entity/conduit/wind.png".equals(string4) || "textures/entity/conduit/wind_vertical.png".equals(string4)) {
            return false;
        }
        if (PackAdapterV4.SHIELDS.contains(string4)) {
            return this.pack.hasResource(yf, PackAdapterV4.SHIELD_BASE) && this.pack.hasResource(yf, sm);
        }
        if (PackAdapterV4.BANNERS.contains(string4)) {
            return this.pack.hasResource(yf, PackAdapterV4.BANNER_BASE) && this.pack.hasResource(yf, sm);
        }
        final Pair<ChestType, ResourceLocation> pair5 = PackAdapterV4.CHESTS.get(string4);
        return (pair5 != null && this.pack.hasResource(yf, (ResourceLocation)pair5.getSecond())) || this.pack.hasResource(yf, sm);
    }
    
    @Override
    public InputStream getResource(final PackType yf, final ResourceLocation sm) throws IOException {
        if (!"minecraft".equals(sm.getNamespace())) {
            return this.pack.getResource(yf, sm);
        }
        final String string4 = sm.getPath();
        if ("textures/entity/iron_golem/iron_golem.png".equals(string4)) {
            return this.pack.getResource(yf, PackAdapterV4.OLD_IRON_GOLEM_LOCATION);
        }
        if (PackAdapterV4.SHIELDS.contains(string4)) {
            final InputStream inputStream5 = fixPattern(this.pack.getResource(yf, PackAdapterV4.SHIELD_BASE), this.pack.getResource(yf, sm), 64, 2, 2, 12, 22);
            if (inputStream5 != null) {
                return inputStream5;
            }
        }
        else if (PackAdapterV4.BANNERS.contains(string4)) {
            final InputStream inputStream5 = fixPattern(this.pack.getResource(yf, PackAdapterV4.BANNER_BASE), this.pack.getResource(yf, sm), 64, 0, 0, 42, 41);
            if (inputStream5 != null) {
                return inputStream5;
            }
        }
        else {
            if ("textures/entity/enderdragon/dragon.png".equals(string4) || "textures/entity/enderdragon/dragon_exploding.png".equals(string4)) {
                try (final NativeImage cxr5 = NativeImage.read(this.pack.getResource(yf, sm))) {
                    for (int integer7 = cxr5.getWidth() / 256, integer8 = 88 * integer7; integer8 < 200 * integer7; ++integer8) {
                        for (int integer9 = 56 * integer7; integer9 < 112 * integer7; ++integer9) {
                            cxr5.setPixelRGBA(integer9, integer8, 0);
                        }
                    }
                    return new ByteArrayInputStream(cxr5.asByteArray());
                }
            }
            if ("textures/entity/conduit/closed_eye.png".equals(string4) || "textures/entity/conduit/open_eye.png".equals(string4)) {
                return fixConduitEyeTexture(this.pack.getResource(yf, sm));
            }
            final Pair<ChestType, ResourceLocation> pair5 = PackAdapterV4.CHESTS.get(string4);
            if (pair5 != null) {
                final ChestType bzc6 = (ChestType)pair5.getFirst();
                final InputStream inputStream6 = this.pack.getResource(yf, (ResourceLocation)pair5.getSecond());
                if (bzc6 == ChestType.SINGLE) {
                    return fixSingleChest(inputStream6);
                }
                if (bzc6 == ChestType.LEFT) {
                    return fixLeftChest(inputStream6);
                }
                if (bzc6 == ChestType.RIGHT) {
                    return fixRightChest(inputStream6);
                }
            }
        }
        return this.pack.getResource(yf, sm);
    }
    
    @Nullable
    public static InputStream fixPattern(final InputStream inputStream1, final InputStream inputStream2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7) throws IOException {
        try (final NativeImage cxr8 = NativeImage.read(inputStream1);
             final NativeImage cxr9 = NativeImage.read(inputStream2)) {
            final int integer8 = cxr8.getWidth();
            final int integer9 = cxr8.getHeight();
            if (integer8 == cxr9.getWidth() && integer9 == cxr9.getHeight()) {
                try (final NativeImage cxr10 = new NativeImage(integer8, integer9, true)) {
                    for (int integer10 = integer8 / integer3, integer11 = integer5 * integer10; integer11 < integer7 * integer10; ++integer11) {
                        for (int integer12 = integer4 * integer10; integer12 < integer6 * integer10; ++integer12) {
                            final int integer13 = NativeImage.getR(cxr9.getPixelRGBA(integer12, integer11));
                            final int integer14 = cxr8.getPixelRGBA(integer12, integer11);
                            cxr10.setPixelRGBA(integer12, integer11, NativeImage.combine(integer13, NativeImage.getB(integer14), NativeImage.getG(integer14), NativeImage.getR(integer14)));
                        }
                    }
                    return new ByteArrayInputStream(cxr10.asByteArray());
                }
            }
        }
        return null;
    }
    
    public static InputStream fixConduitEyeTexture(final InputStream inputStream) throws IOException {
        try (final NativeImage cxr2 = NativeImage.read(inputStream)) {
            final int integer4 = cxr2.getWidth();
            final int integer5 = cxr2.getHeight();
            try (final NativeImage cxr3 = new NativeImage(2 * integer4, 2 * integer5, true)) {
                copyRect(cxr2, cxr3, 0, 0, 0, 0, integer4, integer5, 1, false, false);
                return new ByteArrayInputStream(cxr3.asByteArray());
            }
        }
    }
    
    public static InputStream fixLeftChest(final InputStream inputStream) throws IOException {
        try (final NativeImage cxr2 = NativeImage.read(inputStream)) {
            final int integer4 = cxr2.getWidth();
            final int integer5 = cxr2.getHeight();
            try (final NativeImage cxr3 = new NativeImage(integer4 / 2, integer5, true)) {
                final int integer6 = integer5 / 64;
                copyRect(cxr2, cxr3, 29, 0, 29, 0, 15, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 59, 0, 14, 0, 15, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 29, 14, 43, 14, 15, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 44, 14, 29, 14, 14, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 58, 14, 14, 14, 15, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 29, 19, 29, 19, 15, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 59, 19, 14, 19, 15, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 29, 33, 43, 33, 15, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 44, 33, 29, 33, 14, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 58, 33, 14, 33, 15, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 2, 0, 2, 0, 1, 1, integer6, false, true);
                copyRect(cxr2, cxr3, 4, 0, 1, 0, 1, 1, integer6, false, true);
                copyRect(cxr2, cxr3, 2, 1, 3, 1, 1, 4, integer6, true, true);
                copyRect(cxr2, cxr3, 3, 1, 2, 1, 1, 4, integer6, true, true);
                copyRect(cxr2, cxr3, 4, 1, 1, 1, 1, 4, integer6, true, true);
                return new ByteArrayInputStream(cxr3.asByteArray());
            }
        }
    }
    
    public static InputStream fixRightChest(final InputStream inputStream) throws IOException {
        try (final NativeImage cxr2 = NativeImage.read(inputStream)) {
            final int integer4 = cxr2.getWidth();
            final int integer5 = cxr2.getHeight();
            try (final NativeImage cxr3 = new NativeImage(integer4 / 2, integer5, true)) {
                final int integer6 = integer5 / 64;
                copyRect(cxr2, cxr3, 14, 0, 29, 0, 15, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 44, 0, 14, 0, 15, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 0, 14, 0, 14, 14, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 14, 14, 43, 14, 15, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 73, 14, 14, 14, 15, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 14, 19, 29, 19, 15, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 44, 19, 14, 19, 15, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 0, 33, 0, 33, 14, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 14, 33, 43, 33, 15, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 73, 33, 14, 33, 15, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 1, 0, 2, 0, 1, 1, integer6, false, true);
                copyRect(cxr2, cxr3, 3, 0, 1, 0, 1, 1, integer6, false, true);
                copyRect(cxr2, cxr3, 0, 1, 0, 1, 1, 4, integer6, true, true);
                copyRect(cxr2, cxr3, 1, 1, 3, 1, 1, 4, integer6, true, true);
                copyRect(cxr2, cxr3, 5, 1, 1, 1, 1, 4, integer6, true, true);
                return new ByteArrayInputStream(cxr3.asByteArray());
            }
        }
    }
    
    public static InputStream fixSingleChest(final InputStream inputStream) throws IOException {
        try (final NativeImage cxr2 = NativeImage.read(inputStream)) {
            final int integer4 = cxr2.getWidth();
            final int integer5 = cxr2.getHeight();
            try (final NativeImage cxr3 = new NativeImage(integer4, integer5, true)) {
                final int integer6 = integer5 / 64;
                copyRect(cxr2, cxr3, 14, 0, 28, 0, 14, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 28, 0, 14, 0, 14, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 0, 14, 0, 14, 14, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 14, 14, 42, 14, 14, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 28, 14, 28, 14, 14, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 42, 14, 14, 14, 14, 5, integer6, true, true);
                copyRect(cxr2, cxr3, 14, 19, 28, 19, 14, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 28, 19, 14, 19, 14, 14, integer6, false, true);
                copyRect(cxr2, cxr3, 0, 33, 0, 33, 14, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 14, 33, 42, 33, 14, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 28, 33, 28, 33, 14, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 42, 33, 14, 33, 14, 10, integer6, true, true);
                copyRect(cxr2, cxr3, 1, 0, 3, 0, 2, 1, integer6, false, true);
                copyRect(cxr2, cxr3, 3, 0, 1, 0, 2, 1, integer6, false, true);
                copyRect(cxr2, cxr3, 0, 1, 0, 1, 1, 4, integer6, true, true);
                copyRect(cxr2, cxr3, 1, 1, 4, 1, 2, 4, integer6, true, true);
                copyRect(cxr2, cxr3, 3, 1, 3, 1, 1, 4, integer6, true, true);
                copyRect(cxr2, cxr3, 4, 1, 1, 1, 2, 4, integer6, true, true);
                return new ByteArrayInputStream(cxr3.asByteArray());
            }
        }
    }
    
    @Override
    public Collection<ResourceLocation> getResources(final PackType yf, final String string2, final String string3, final int integer, final Predicate<String> predicate) {
        return this.pack.getResources(yf, string2, string3, integer, predicate);
    }
    
    @Override
    public Set<String> getNamespaces(final PackType yf) {
        return this.pack.getNamespaces(yf);
    }
    
    @Nullable
    @Override
    public <T> T getMetadataSection(final MetadataSectionSerializer<T> yi) throws IOException {
        return this.pack.<T>getMetadataSection(yi);
    }
    
    @Override
    public String getName() {
        return this.pack.getName();
    }
    
    @Override
    public void close() throws IOException {
        this.pack.close();
    }
    
    private static void copyRect(final NativeImage cxr1, final NativeImage cxr2, int integer3, int integer4, int integer5, int integer6, int integer7, int integer8, final int integer9, final boolean boolean10, final boolean boolean11) {
        integer8 *= integer9;
        integer7 *= integer9;
        integer5 *= integer9;
        integer6 *= integer9;
        integer3 *= integer9;
        integer4 *= integer9;
        for (int integer10 = 0; integer10 < integer8; ++integer10) {
            for (int integer11 = 0; integer11 < integer7; ++integer11) {
                cxr2.setPixelRGBA(integer5 + integer11, integer6 + integer10, cxr1.getPixelRGBA(integer3 + (boolean10 ? (integer7 - 1 - integer11) : integer11), integer4 + (boolean11 ? (integer8 - 1 - integer10) : integer10)));
            }
        }
    }
    
    static {
        final Pair value;
        final String key;
        final Pair value2;
        final String key2;
        final Pair value3;
        final String key3;
        final Pair value4;
        final String key4;
        final Pair value5;
        final String key5;
        final Pair value6;
        final String key6;
        final Pair value7;
        final String key7;
        final Pair value8;
        final String key8;
        final Pair value9;
        final String key9;
        final Pair value10;
        final String key10;
        CHESTS = Util.<Map<String, Pair<ChestType, ResourceLocation>>>make(Maps.newHashMap(), hashMap -> {
            new Pair(ChestType.LEFT, new ResourceLocation("textures/entity/chest/normal_double.png"));
            hashMap.put(key, value);
            new Pair(ChestType.RIGHT, new ResourceLocation("textures/entity/chest/normal_double.png"));
            hashMap.put(key2, value2);
            new Pair(ChestType.SINGLE, new ResourceLocation("textures/entity/chest/normal.png"));
            hashMap.put(key3, value3);
            new Pair(ChestType.LEFT, new ResourceLocation("textures/entity/chest/trapped_double.png"));
            hashMap.put(key4, value4);
            new Pair(ChestType.RIGHT, new ResourceLocation("textures/entity/chest/trapped_double.png"));
            hashMap.put(key5, value5);
            new Pair(ChestType.SINGLE, new ResourceLocation("textures/entity/chest/trapped.png"));
            hashMap.put(key6, value6);
            new Pair(ChestType.LEFT, new ResourceLocation("textures/entity/chest/christmas_double.png"));
            hashMap.put(key7, value7);
            new Pair(ChestType.RIGHT, new ResourceLocation("textures/entity/chest/christmas_double.png"));
            hashMap.put(key8, value8);
            new Pair(ChestType.SINGLE, new ResourceLocation("textures/entity/chest/christmas.png"));
            hashMap.put(key9, value9);
            new Pair(ChestType.SINGLE, new ResourceLocation("textures/entity/chest/ender.png"));
            hashMap.put(key10, value10);
            return;
        });
        PATTERNS = Lists.<String>newArrayList("base", "border", "bricks", "circle", "creeper", "cross", "curly_border", "diagonal_left", "diagonal_right", "diagonal_up_left", "diagonal_up_right", "flower", "globe", "gradient", "gradient_up", "half_horizontal", "half_horizontal_bottom", "half_vertical", "half_vertical_right", "mojang", "rhombus", "skull", "small_stripes", "square_bottom_left", "square_bottom_right", "square_top_left", "square_top_right", "straight_cross", "stripe_bottom", "stripe_center", "stripe_downleft", "stripe_downright", "stripe_left", "stripe_middle", "stripe_right", "stripe_top", "triangle_bottom", "triangle_top", "triangles_bottom", "triangles_top");
        SHIELDS = PackAdapterV4.PATTERNS.stream().map(string -> "textures/entity/shield/" + string + ".png").collect(Collectors.toSet());
        BANNERS = PackAdapterV4.PATTERNS.stream().map(string -> "textures/entity/banner/" + string + ".png").collect(Collectors.toSet());
        SHIELD_BASE = new ResourceLocation("textures/entity/shield_base.png");
        BANNER_BASE = new ResourceLocation("textures/entity/banner_base.png");
        OLD_IRON_GOLEM_LOCATION = new ResourceLocation("textures/entity/iron_golem.png");
    }
}
