package de.frajul.endlessroll.rendering.shader;

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

import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.Entity;
import de.frajul.endlessroll.entities.textures.Texture;
import de.frajul.endlessroll.main.game.Camera;
import de.frajul.endlessroll.rendering.MatrixCreator;

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

    private int location_mvpMatrix;
    private int location_transformationMatrix;
    private int location_alpha;
    private int location_texAtlasSize;
    private int location_gridSize;
    private int location_deadly;
    private int location_floating;

    public ObstacleShader(Context context) throws Exception {
        super(context, "shader/entityVertexShader.glsl", "shader/obstacleFragmentShader.glsl");
    }

    @Override
    protected void loadUniformLocations() {
        location_mvpMatrix = super.getUniformLocation("mvpMatrix");
        location_transformationMatrix = super.getUniformLocation("transformationMatrix");
        location_alpha = super.getUniformLocation("alpha");
        location_texAtlasSize = super.getUniformLocation("texAtlasSize");
        location_gridSize = super.getUniformLocation("gridSize");
        location_deadly = super.getUniformLocation("deadly");
        location_floating = super.getUniformLocation("floating");
    }

    public void loadMVPMatrix(MatrixCreator matrixCreator, Camera camera) {
        float[] mvpMatrix = matrixCreator.createModelViewProjectionMatrix(camera);
        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 loadAlpha(float alpha) {
        GLES20.glUniform1f(location_alpha, alpha);
    }

    public void loadTextureAtlasInfos(Texture texture) {
        GLES20.glUniform2f(location_texAtlasSize, texture.getAtlasWidth(),
                texture.getAtlasHeight());
    }

    public void loadGridSize(Vector gridSize) {
        GLES20.glUniform2f(location_gridSize, gridSize.getX(), gridSize.getY());
    }

    public void loadDeadly(boolean deadly) {
        GLES20.glUniform1f(location_deadly, deadly ? 1 : 0);
    }

    public void loadFloating(boolean floating) {
        GLES20.glUniform1f(location_floating, floating ? 1 : 0);
    }

}
