package de.frajul.endlessroll.rendering.shader;

import android.content.Context;
import android.opengl.GLES20;

import de.frajul.endlessroll.data.Color4f;
import de.frajul.endlessroll.entities.Entity;
import de.frajul.endlessroll.rendering.MatrixCreator;

/**
 * Created by Julian on 10.08.2016.
 */
public class GuiShader extends ShaderProgram {

    private int location_mvpMatrix;
    private int location_transformationMatrix;
    private int location_useCustomColor;
    private int location_color;
    private int location_alpha;
    private int location_roundValue;
    private int location_visibleAmount;

    public GuiShader(Context context) throws Exception {
        super(context, "shader/guiVertexShader.glsl", "shader/guiFragmentShader.glsl");
    }

    @Override
    protected void loadUniformLocations() {
        location_mvpMatrix = super.getUniformLocation("mvpMatrix");
        location_transformationMatrix = super.getUniformLocation("transformationMatrix");
        location_useCustomColor = super.getUniformLocation("useCustomColor");
        location_color = super.getUniformLocation("color");
        location_alpha = super.getUniformLocation("alpha");
        location_roundValue = super.getUniformLocation("roundValue");
        location_visibleAmount = super.getUniformLocation("visibleAmount");
    }

    public void loadMVPMatrix(MatrixCreator matrixCreator) {
        float[] mvpMatrix = matrixCreator.createGuiModelViewProjectionMatrix();
        GLES20.glUniformMatrix4fv(location_mvpMatrix, 1, false, mvpMatrix, 0);
    }

    public void loadTransformationMatrix(MatrixCreator matrixCreator, Entity entity) {
        float[] transformationMatrix = matrixCreator.createTransformationMatrix(entity);
        GLES20.glUniformMatrix4fv(location_transformationMatrix, 1, false, transformationMatrix, 0);
    }

    public void loadCustomColor(Color4f color) {
        GLES20.glUniform1f(location_useCustomColor, color == null ? 0 : 1);
        if (color != null)
            GLES20.glUniform4f(location_color, color.getR(), color.getG(), color.getB(),
                    color.getA());
    }

    public void loadAlpha(float alpha){
        GLES20.glUniform1f(location_alpha, alpha);
    }

    public void loadRoundValue(float roundValue) {
        GLES20.glUniform1f(location_roundValue, roundValue);
    }

    public void loadVisibleAmount(float visibleAmount) {
        GLES20.glUniform1f(location_visibleAmount, visibleAmount);
    }

}
