#include "metalShaderTypes.h"
#include "alpha.h"
#include "textAnimationHelper.h"

typedef struct {
    int direction;
    float easeInOut;
} fxVars;

#define moveAmount 1.

constant vec4 deltaTransTable[9] = {
    vec4(-moveAmount, moveAmount, 0.0, 0.0),
    vec4( 0.0, moveAmount, 0.0, 0.0),
    vec4( moveAmount, moveAmount, 0.0, 0.0),
    vec4(-moveAmount, 0.0, 0.0, 0.0),
    vec4( 0.0, 0.0, 0.0, 0.0),
    vec4( moveAmount, 0.0, 0.0, 0.0),
    vec4(-moveAmount,-moveAmount,0.0, 0.0),
    vec4( 0.0,-moveAmount,0.0, 0.0),
    vec4( moveAmount,-moveAmount,0.0, 0.0)
};

constant vec4 transOffsetTable[9] = {
    vec4(-moveAmount, moveAmount, 0.0, 0.0),
    vec4( 0.0, moveAmount, 0.0, 0.0),
    vec4( moveAmount, moveAmount, 0.0, 0.0),
    vec4(-moveAmount, 0.0, 0.0, 0.0),
    vec4( 0.0, 0.0, 0.0, 0.0),
    vec4( moveAmount, 0.0, 0.0, 0.0),
    vec4(-moveAmount,-moveAmount,0.0, 0.0),
    vec4( 0.0,-moveAmount,0.0, 0.0),
    vec4( moveAmount,-moveAmount,0.0, 0.0)
};


vertex vertexOut vertexFunc(uint vertexID [[ vertex_id ]],const device txShaderVers* in[[ buffer(0) ]],constant txShaderUniforms& uniforms[[ buffer(3) ]],constant fxVars& vars[[ buffer(2)]])
{
    vertexOut out;

    out.normPos=in[vertexID].pos.xy;

    // increase offset by 1 pixel on each side to account for scissor rect being padded up as well
    vec2 onePixel = 2.0 / uniforms.windowSize;
    vec2 offset = vec2(uniforms.srcBounds.z - uniforms.srcBounds.x + onePixel.x,
                       uniforms.srcBounds.w - uniforms.srcBounds.y + onePixel.y) * 2.0;
    vec4 deltaTrans = deltaTransTable[vars.direction] * vec4(offset, 0, 0) * uniforms.worldViewProj;
    vec4 transOffset = transOffsetTable[vars.direction] * vec4(offset, 0, 0) * uniforms.worldViewProj;

    mat4 translation=mat4(
                          1.0, 0.0, 0.0, 0.00,
                          0.0, 1.0, 0.0, 0.00,
                          0.0, 0.0, 1.0, 0.00,
                          0.0, 0.0, 0.0, 1.00);

    vec4 tmpPos = vec4(in[vertexID].pos.xyz, 1.0) * uniforms.worldViewProj;
    if(uniforms.transitionState<=1)
    {
        transOffset-=deltaTrans*uniforms.segmentStart[vars.direction];

        translation[0][3]+=transOffset.x;
        translation[1][3]+=transOffset.y;
        translation[2][3]+=transOffset.z;
    }
    else if(uniforms.transitionState==2)
    {
        vec4 deltaOffset = -deltaTrans* (1.0 - uniforms.segmentStart[vars.direction]);

        translation[0][3]+=deltaOffset.x;
        translation[1][3]+=deltaOffset.y;
        translation[2][3]+=deltaOffset.z;
    }
    out.position=tmpPos*translation;

    out.tex1=in[vertexID].tex1;
    out.tex2=in[vertexID].tex2;
    out.texMask=in[vertexID].texMask;
    return out;
}

fragment float4 fragmentFunc(vertexOut input [[stage_in]],
                             texture2d<half> inputTex0    [[ texture(0) ]],
                             texture2d<half> inputTex1    [[ texture(1) ]],
                             texture2d<half> inputTexMask [[ texture(2) ]],
                             constant txShaderUniforms& u     [[ buffer(2) ]],
                             constant subFXVars& subVars      [[buffer(3)]])
{
    float4 outColor = getColor(inputTex0, input.tex1);
    float alpha = u.baseAlpha;

#ifdef IS_MEDIA_FILL
    alpha *= outColor.a;
    outColor = float4(inputTex1.sample(linearSampler, input.tex2));
#endif

#ifdef USE_MASK
    alpha *= float(inputTexMask.sample(linearSampler, input.texMask).a);
#endif

    multiplyOpacity(outColor, alpha);

#ifdef SHOW_SEGMENT_OUTLINE
    showSegmentOutline(outColor);
#endif

    return outColor;
}
