package com.raoulvdberge.refinedstorage.render.model;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.model.BakedQuad;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.util.Direction;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.data.EmptyModelData;
import net.minecraftforge.client.model.data.IModelData;

import javax.annotation.Nullable;
import java.util.*;

public class FullbrightBakedModel extends DelegateBakedModel {
    private static final LoadingCache<CacheKey, List<BakedQuad>> CACHE = CacheBuilder.newBuilder().build(new CacheLoader<CacheKey, List<BakedQuad>>() {
        @Override
        public List<BakedQuad> load(CacheKey key) {
            return transformQuads(key.base.getQuads(key.state, key.side, key.random, EmptyModelData.INSTANCE), key.textures);
        }
    });

    private final Set<ResourceLocation> textures;
    private final boolean doCaching;

    public FullbrightBakedModel(IBakedModel base, boolean doCaching, ResourceLocation... textures) {
        super(base);

        this.textures = new HashSet<>(Arrays.asList(textures));
        this.doCaching = doCaching;
    }

    private boolean canRenderFullbright() {
        // return ForgeConfig.CLIENT.forgeLightPipelineEnabled.get();
        return true;
    }

    @Override
    public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData data) {
        if (state == null || !canRenderFullbright()) {
            return base.getQuads(state, side, rand, data);
        }

        if (!doCaching) {
            return transformQuads(base.getQuads(state, side, rand, data), textures);
        }

        return CACHE.getUnchecked(new CacheKey(base, textures, rand, state, side));
    }

    private static List<BakedQuad> transformQuads(List<BakedQuad> oldQuads, Set<ResourceLocation> textures) {
        List<BakedQuad> quads = new ArrayList<>(oldQuads);

        for (int i = 0; i < quads.size(); ++i) {
            BakedQuad quad = quads.get(i);

            if (textures.contains(quad.getSprite().getName())) {
                quads.set(i, transformQuad(quad));
            }
        }

        return quads;
    }

    private static BakedQuad transformQuad(BakedQuad quad) {
        int[] vertexData = quad.getVertexData();

        vertexData[6] = 0xF000F000;

        return quad;
    }

    private class CacheKey {
        private IBakedModel base;
        private Set<ResourceLocation> textures;
        private Random random;
        private BlockState state;
        private Direction side;

        public CacheKey(IBakedModel base, Set<ResourceLocation> textures, Random random, BlockState state, Direction side) {
            this.base = base;
            this.textures = textures;
            this.random = random;
            this.state = state;
            this.side = side;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }

            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            CacheKey cacheKey = (CacheKey) o;

            if (cacheKey.side != side) {
                return false;
            }

            if (!state.equals(cacheKey.state)) {
                return false;
            }

            return true;
        }

        @Override
        public int hashCode() {
            return state.hashCode() + (31 * (side != null ? side.hashCode() : 0));
        }
    }
}

@@ -1336,150 +1336,8 @@
  %7D%0A%0A
-    private boolean canRenderFullbright() %7B%0A        // return ForgeConfig.CLIENT.forgeLightPipelineEnabled.get();%0A        return true;%0A    %7D%0A%0A
     
@@ -1497,34 +1497,8 @@
 null
- %7C%7C !canRenderFullbright()
 ) %7B%0A
@@ -2321,19 +2321,81 @@
 exData()
+.clone()
 ;%0A%0A
+        // Set lighting to fullbright on all vertices%0A
         
@@ -2416,38 +2416,351 @@
 = 0x
+00
 F000F0
-00;%0A%0A        return quad
+;%0A        vertexData%5B6 + 8%5D = 0x00F000F0;%0A        vertexData%5B6 + 8 + 8%5D = 0x00F000F0;%0A        vertexData%5B6 + 8 + 8 + 8%5D = 0x00F000F0;%0A%0A        return new BakedQuad(%0A            vertexData,%0A            quad.getTintIndex(),%0A            quad.getFace(),%0A            quad.getSprite(),%0A            quad.shouldApplyDiffuseLighting()%0A        )
 ;%0A  

Fix fullbrightness completely