#include engine/kmeans/include.glsl

in vec2 uv;
in vec2 uvn;

out vec4 C;

float Bayer2(vec2 a) {
    a = floor(a);
    return fract(a.x / 2. + a.y * a.y * .75);
}

#define Bayer4(a)   (Bayer2 (.5 *(a)) * .25 + Bayer2(a))
#define Bayer8(a)   (Bayer4 (.5 *(a)) * .25 + Bayer2(a))
#define Bayer16(a)  (Bayer8 (.5 *(a)) * .25 + Bayer2(a))
#define Bayer32(a)  (Bayer16(.5 *(a)) * .25 + Bayer2(a))
#define Bayer64(a)  (Bayer32(.5 *(a)) * .25 + Bayer2(a))

void main() {
//    seed = 125125u;
//    seed += 15u + id*5;
//    let k = &buff.k[id];
//    int id = int(gl_GlobalInvocationID.x);
//    seed = 1241245u;
//    seed -= 15u + id*5;
    
//    C = uvn.xyxy;
    bool dbg = dbg_frag(ivec2(200));
    C = texture(textures[input_tex], uvn);
    vec3 col = vec3(0);
    

        vec2 uv = floor(U/float(dith_sc))*float(dith_sc)/R; // TOREMOVE R
//        uv.x -= 0.5;
//        uv.y = 1. - uv.y;

        {

        
        }
        // uv = floor(uv/float(dith_sc)) * float(dith_sc);

        col = sample_input_image(uv).rgb;
        float dith_amt = DITH_AMT*1./float(K_CNT);
        // col += ;
        // var dith = (textureLoad(channel1,vec2i(id.xy/dith_sc) % 8,0).x*2. - 1.) * dith_amt;
        float dith = Bayer8(floor(U/float(dith_sc)));
//        dith *= ;
    
//        dith *= dith_amt;

        float closest_k = 100000.0;
        float second_closest_k = 100000.0;
        int k_idx = 0;
        int second_k_idx = 0;
        vec3 k_col = vec3(0);
        vec3 k2_col = vec3(0);
        for(int i = 0; i < K_CNT; i++){
            K k = buff.k[i];
    
            // let dist_to_k = length(col - k.pos);
            float dist_to_k = colour_dist(
                to_space(col) 
                , k.pos
            );

            if(dbg){
//                print(dist_to_k);
            }
            if(dist_to_k < closest_k){
                // second_k_idx = k_idx;
                // second_closest_k = closest_k;
                // k2_col = k_col;
                k_idx = i;
                closest_k = dist_to_k;
                 k_col = k.pos;
//                k_col = k.col;
            }
        }

        for(int i = 0; i < K_CNT; i++){
            K k = buff.k[i];
    
            // let dist_to_k = length(col - k.pos);
            float dist_to_k = colour_dist(
                to_space(col) 
                ,  k.pos
            );
            if(dist_to_k < second_closest_k && i != k_idx){
                // second_k_idx = k_idx;
                // second_closest_k = closest_k;
                // k2_col = k_col;
                second_k_idx = i;
                second_closest_k = dist_to_k;
//                k2_col = k.col;
                k2_col = k.pos;
            }
        }

        float dist_between_cols = colour_dist( k_col  , k2_col );
//        k_col += dith;
        if(colour_dist( to_space(col), k_col )/dist_between_cols < dith*1.0){
            col = k_col;
        } else {
            col = k2_col;
        }
        // to_space(col)
        // if(col + colour_dist(to_space(col)) * dist_between_cols)
        
        // col += dith
        

        // col = k_col;
            // col = oklch_to_srgb(col);
        col = from_space(col);
//    
//    col = sample_input_image(uv).rgb;
    C.rgb = col;
}
