#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>

#include "record.h"
#include "euclidean_distance.h"
#include "coord_query.h"

struct naive_data {
    struct record *rs;
    int n;
};

struct naive_data* mk_naive(struct record* rs, int n) {
    struct naive_data* data = malloc(sizeof(struct naive_data*));
    data->rs = rs;
    data->n = n;

    return data;
}

void free_naive(struct naive_data* data) {
    free(data);
}

const struct record* lookup_naive(struct naive_data *data, double lon, double lat) {
    double search_coords[2] = {lon, lat};
    struct record* closest_point = &data->rs[0];

    double compare_coords[2] = {(closest_point)->lon, (closest_point)->lat};
    double dist = calc_euclidean(search_coords, compare_coords);
    double closest_dist = dist;

    for (int i = 1 ; i < data->n ; i++) {
        double compare_coords[2] = {(&data->rs[i])->lon, (&data->rs[i])->lat};
        double dist = calc_euclidean(search_coords, compare_coords);

        if (dist < closest_dist) {
            closest_point = &data->rs[i];
            closest_dist = dist;
        }
    }

    return closest_point;
}

int main(int argc, char** argv) {
    return coord_query_loop(argc, argv,
                                                    (mk_index_fn)mk_naive,
                                                    (free_index_fn)free_naive,
                                                    (lookup_fn)lookup_naive);
}
