precision mediump float;

varying vec2 textureCoordinate;

uniform sampler2D inputImageTexture;

uniform vec2 iResolution;
uniform float iTime;

#define PI 3.14159265359

vec2 rotate(vec2 uv, vec2 centerP, float rotation, float ratio) {
    vec2 ori = vec2(uv.x - centerP.x, (uv.y - centerP.y) * ratio);
    return vec2(ori.x * cos(rotation) - ori.y * sin(rotation), (ori.x * sin(rotation) + ori.y * cos(rotation)) / ratio) + centerP;
}

vec2 scale(vec2 uv, vec2 centerP, vec2 scalePro, float ratio) {
    vec2 ori = vec2(uv.x - centerP.x, (uv.y - centerP.y) * ratio);
    return vec2(ori.x, ori.y/ratio)/scalePro + centerP;
}

vec2 transform(vec2 uv, float params[5], float ratio) {
    vec2 pos = uv;
    pos -= (vec2(params[2], -0.5) + vec2(-0.5, params[3]));
    pos = scale(pos, vec2(0.5), vec2(params[0], params[1]), ratio);
    pos = rotate(pos, vec2(0.5), -params[4]/180.0*PI, ratio);
    return pos;
}

int inside(vec2 uv, vec4 edge) {
    return (uv.x >= edge.x && uv.x <= (edge.x + edge.z) && uv.y >= edge.y && uv.y <= (edge.y + edge.w)) ? 1 : 0;
}

void main(void) {
    vec2 uv = textureCoordinate;

    float period = 1.0;
    float xOffset = sin(iTime * PI * 2.0/period) * 45.0/iResolution.x;
    float time = mod(iTime, period);
    float rotation;

    if (time < period/4.0) {
        rotation = sin(time * PI/period/1.5) * PI / 150.0;
    } else if (time < period/4.0 + 0.2) {
        rotation = sin(PI / 6.0) * PI / 150.0;
    } else {
        rotation = sin(PI / 6.0 + (time - period/4.0 - 0.2) * PI * 5.0/(period*4.5 - 1.2)) * PI / 150.0;
    }

    if (inside(uv, vec4(0.0, 0.0, 1.0, 1.0)) == 1) {
        vec2 st = vec2(uv.x + xOffset, uv.y);
        st = rotate(st, vec2(0.5), -rotation, iResolution.y/iResolution.x);
        st = vec2(abs(mod(st.x + 1.0, 2.0) - 1.0), abs(mod(st.y + 1.0, 2.0) - 1.0));

        gl_FragColor = texture2D(inputImageTexture, st);
    }
}