package org.helioviewer.jhv.internal_plugins.filter.difference;

import javax.media.opengl.GL2;

import org.helioviewer.viewmodel.view.opengl.shader.GLFragmentShaderProgram;
import org.helioviewer.viewmodel.view.opengl.shader.GLShaderBuilder;
import org.helioviewer.viewmodel.view.opengl.shader.GLShaderBuilder.GLBuildShaderException;
import org.helioviewer.viewmodel.view.opengl.shader.GLTextureCoordinate;

public class DifferenceShader extends GLFragmentShaderProgram {
    private GLTextureCoordinate isDifference;

    private static int ID = 0;
    int mode = -1;
    private int truncationValueRef;
    private GLShaderBuilder builder;

    private double[] truncationValueFloat;

    public void setIsDifference(GL2 gl, float isDifference) {
        this.isDifference.setValue(gl, isDifference);
    }

    public void setTruncationValue(GL2 gl, float truncationValue) {
        this.truncationValueFloat[0] = truncationValue;
    }

    @Override
    public void bind(GL2 gl) {
        super.bind(gl);
        this.bindEnvVars(gl, this.truncationValueRef, truncationValueFloat);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void buildImpl(GLShaderBuilder shaderBuilder) {
        this.builder = shaderBuilder;
        try {
            isDifference = shaderBuilder.addTexCoordParameter(1);
            this.truncationValueRef = shaderBuilder.addEnvParameter("float truncationValue");
            this.truncationValueFloat = this.builder.getEnvParameter(this.truncationValueRef);

            shaderBuilder.getParameterList().add("float4 " + "texcoord4" + " : TEXCOORD4");
            String program = "";
            // program =
            // "\tif((texcoord4.x<0.0||texcoord4.y<0.0||texcoord4.x>diffTextureScaleThetaPhi.x||texcoord4.y>diffTextureScaleThetaPhi.y)){"
            // + "\t\tOUT.color = float4(0.0,1.0,0.0,1.0);" +
            // GLShaderBuilder.LINE_SEP + "\t}" + GLShaderBuilder.LINE_SEP;

            program += "if(isdifference>0.24 && isdifference<0.27){" + shaderBuilder.LINE_SEP + "\toutput.r = output.r - tex2D(differenceImage, texcoord0.xy).r;" + shaderBuilder.LINE_SEP;
            program += "\toutput.r = clamp(output.r,-truncationValue,truncationValue)/truncationValue;" + shaderBuilder.LINE_SEP;
            program += "\toutput.r = (output.r + 1.0f)/2.0f;" + shaderBuilder.LINE_SEP;
            program += "}" + shaderBuilder.LINE_SEP;

            program += "if(isdifference>0.98 && isdifference<1.01){" + shaderBuilder.LINE_SEP + "\toutput.r = output.r - tex2D(differenceImage, texcoord4.xy).r;" + shaderBuilder.LINE_SEP;
            program += "\toutput.r = clamp(output.r,-truncationValue,truncationValue)/truncationValue;" + shaderBuilder.LINE_SEP;
            program += "\toutput.r = (output.r + 1.0f)/2.0f;" + shaderBuilder.LINE_SEP;
            program += "}" + shaderBuilder.LINE_SEP;

            program = program.replaceAll("output", shaderBuilder.useOutputValue("float4", "COLOR"));

            mode = shaderBuilder.addTextureParameter("sampler2D differenceImage");
            ID = (ID + 1) & 15;
            program = program.replace("isdifference", isDifference.getIdentifier(1));
            // program = program.replace("truncationValue",
            // truncationValue.getIdentifier(1));

            shaderBuilder.addMainFragment(program);
        } catch (GLBuildShaderException e) {
            e.printStackTrace();
        }
    }

    public void activateDifferenceTexture(GL2 gl) {
    }
}

@@ -2004,25 +2004,27 @@
 e%3C0.27)%7B%22 + 
-s
+GLS
 haderBuilder
@@ -2095,33 +2095,35 @@
 oord0.xy).r;%22 + 
-s
+GLS
 haderBuilder.LIN
@@ -2227,33 +2227,35 @@
 cationValue;%22 + 
-s
+GLS
 haderBuilder.LIN
@@ -2317,33 +2317,35 @@
  1.0f)/2.0f;%22 + 
-s
+GLS
 haderBuilder.LIN
@@ -2372,33 +2372,35 @@
 rogram += %22%7D%22 + 
-s
+GLS
 haderBuilder.LIN
@@ -2474,25 +2474,27 @@
 e%3C1.01)%7B%22 + 
-s
+GLS
 haderBuilder
@@ -2569,25 +2569,27 @@
 4.xy).r;%22 + 
-s
+GLS
 haderBuilder
@@ -2701,25 +2701,27 @@
 onValue;%22 + 
-s
+GLS
 haderBuilder
@@ -2791,25 +2791,27 @@
 f)/2.0f;%22 + 
-s
+GLS
 haderBuilder
@@ -2850,17 +2850,19 @@
 = %22%7D%22 + 
-s
+GLS
 haderBui

use static reference