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

import javax.media.opengl.GL;

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(GL gl, float isDifference) {
        this.isDifference.setValue(gl, isDifference);
    }

    public void setTruncationValue(GL gl, float truncationValue) {
        this.truncationValueFloat[0] = truncationValue;
    }
    @Override
    public void bind(GL 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(GL gl) {
    }
}

@@ -927,24 +927,25 @@
 alue;%0A    %7D%0A
+%0A
     @Overrid
@@ -973,16 +973,17 @@
 d(GL gl)
+ 
 %7B%0A      
@@ -1079,24 +1079,25 @@
 oat);%0A    %7D%0A
+%0A
     /**%0A    
@@ -1501,25 +1501,24 @@
 ValueRef);%0A%0A
-%0A
             
@@ -1633,32 +1633,35 @@
  %22%22;%0A           
+ //
  program = %22%5Ctif
@@ -1654,16 +1654,31 @@
 rogram =
+%0A            //
  %22%5Ctif((
@@ -1793,16 +1793,31 @@
 hi.y))%7B%22
+%0A            //
  + %22%5Ct%5Ct
@@ -1855,16 +1855,31 @@
 1.0);%22 +
+%0A            //
  GLShade
@@ -1953,16 +1953,17 @@
 program 
++
 = %22if(is
@@ -2026,36 +2026,16 @@
 LINE_SEP
-%0A                   
  + %22%5Ctou
@@ -2135,32 +2135,33 @@
       program +=
+ 
 %22%5Ctoutput.r = cl
@@ -2265,32 +2265,33 @@
       program +=
+ 
 %22%5Ctoutput.r = (o
@@ -2490,28 +2490,8 @@
 _SEP
-%0A                   
  + %22
@@ -2595,32 +2595,33 @@
       program +=
+ 
 %22%5Ctoutput.r = cl
@@ -2733,16 +2733,17 @@
 ogram +=
+ 
 %22%5Ctoutpu
@@ -3170,16 +3170,17 @@
       //
+ 
 program 
@@ -3215,16 +3215,31 @@
 nValue%22,
+%0A            //
  truncat
@@ -3267,17 +3267,16 @@
 r(1));%0A%0A
-%0A
         

comment out unused code