package com.minelittlepony.client.render;

import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.RenderPhase;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;

import com.mojang.blaze3d.systems.RenderSystem;

import java.util.function.BiFunction;

public abstract class MagicGlow extends RenderPhase {
    private MagicGlow(String name, Runnable beginAction, Runnable endAction) {
        super(name, beginAction, endAction);
    }

    private static final RenderLayer MAGIC = RenderLayer.of("mlp_magic_glow", VertexFormats.POSITION_COLOR_LIGHT, VertexFormat.DrawMode.QUADS, 256, RenderLayer.MultiPhaseParameters.builder()
            .shader(EYES_SHADER)
            .writeMaskState(COLOR_MASK)
            .depthTest(LEQUAL_DEPTH_TEST)
            .transparency(LIGHTNING_TRANSPARENCY)
            .lightmap(DISABLE_LIGHTMAP)
            .cull(DISABLE_CULLING)
            .build(false));

    private static final BiFunction<Identifier, Integer, RenderLayer> TINTED_LAYER = Util.memoize((texture, color) -> {
        return RenderLayer.of("mlp_tint_layer", VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL, VertexFormat.DrawMode.QUADS, 256, true, true, RenderLayer.MultiPhaseParameters.builder()
                .texture(new Color(texture, color))
                .shader(EYES_SHADER)
                .writeMaskState(COLOR_MASK)
                .depthTest(LEQUAL_DEPTH_TEST)
                .transparency(LIGHTNING_TRANSPARENCY)
                .lightmap(DISABLE_LIGHTMAP)
                .cull(DISABLE_CULLING)
                .build(true));
    });

    public static RenderLayer getRenderLayer() {
        return MAGIC;
    }

    public static RenderLayer getTintedTexturedLayer(Identifier texture, float red, float green, float blue, float alpha) {
        return TINTED_LAYER.apply(texture, com.minelittlepony.common.util.Color.argbToHex(alpha, red, green, blue));
    }

    private static class Color extends Texture {

        private final float red;
        private final float green;
        private final float blue;
        private final float alpha;

        public Color(Identifier texture, int color) {
            super(texture, false, false);
            this.red = com.minelittlepony.common.util.Color.r(color);
            this.green = com.minelittlepony.common.util.Color.g(color);
            this.blue = com.minelittlepony.common.util.Color.b(color);
            this.alpha = com.minelittlepony.common.util.Color.a(color);
        }

        @Override
        public void startDrawing() {
            RenderSystem.setShaderColor(red, green, blue, alpha);
            super.startDrawing();
        }

        @Override
        public void endDrawing() {
            super.endDrawing();
            RenderSystem.setShaderColor(1, 1, 1, 1);
        }

        @Override
        public boolean equals(Object other) {
            return super.equals(other)
                    && ((Color)other).red == red
                    && ((Color)other).green == green
                    && ((Color)other).blue == blue
                    && ((Color)other).alpha == alpha;
        }
    }
}

@@ -349,16 +349,61 @@
 rSystem;
+%0Aimport com.minelittlepony.common.util.Color;
 %0A%0Aimport
@@ -1430,16 +1430,18 @@
 ew Color
+ed
 (texture
@@ -2003,39 +2003,8 @@
 re, 
-com.minelittlepony.common.util.
 Colo
@@ -2079,16 +2079,18 @@
 ss Color
+ed
  extends
@@ -2259,16 +2259,18 @@
 ic Color
+ed
 (Identif
@@ -2364,39 +2364,8 @@
 d = 
-com.minelittlepony.common.util.
 Colo
@@ -2405,39 +2405,8 @@
 n = 
-com.minelittlepony.common.util.
 Colo
@@ -2445,39 +2445,8 @@
 e = 
-com.minelittlepony.common.util.
 Colo
@@ -2486,39 +2486,8 @@
 a = 
-com.minelittlepony.common.util.
 Colo
@@ -2949,32 +2949,34 @@
       && ((Color
+ed
 )other).red == r
@@ -3000,32 +3000,34 @@
       && ((Color
+ed
 )other).green ==
@@ -3055,32 +3055,34 @@
       && ((Color
+ed
 )other).blue == 
@@ -3116,16 +3116,18 @@
  ((Color
+ed
 )other).

Fix naming collission between MagicGlow.Color and util.Color