#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <time.h>
#include "mxml-2.10/mxml.h"

#include "../../status.h"
#include "../../feeder.h"

#define __USE_GNU
/*
typedef struct measurement_t
{
	unsigned long unit_id;
	unsigned int sensor_id;
	time_t time;
	double value;
	struct measurement_t * next;
} measurement_t;

struct measurement_t * measurement_list;
*/

static void (*feederLog) (int priority, const char *fmt, ...);

int setLog(void *func)
{
    feederLog = func;
    return 0;
}

static FILE * f = NULL;
static mxml_node_t * gpxTree, * gpxNode;

unsigned int timeout(void * lib_data, int sock, unsigned char * data)
{
    (void)lib_data;
    (void)sock;
    (void)data;

    f = fopen("b.gpx", "r");
    if (f == NULL)
    {
	feederLog(LOG_ERR, "gpx: can not open input file (b.gpx)\n");
	return -1;
    }

    gpxTree = mxmlLoadFile(NULL, f, MXML_TEXT_CALLBACK);
    gpxNode = gpxTree;
    //todo:ceck
    fclose(f);
    return 0;
}

int getval(char * line, const char * name, char * val)
{
    char * s, * se;

    s = strstr(line, name);
    s = strchr(s, ' ');
    s++;
    se = strchr(s, ',');
    memcpy(val, s, se - s);
    val[se-s] = 0;
    feederLog(LOG_DEBUG, "Value %s:%s\n", name, val);
    return 0;
}

unsigned int
process(void *lib_data, int sock, unsigned char *data,
        unsigned int length, unsigned long long int *id, time_t * tm,
        double *result_array, uint64_t * sensors, unsigned int *type)
{
    unsigned int res;
/*    static size_t len = 0;
    static char * line = NULL;
    ssize_t size;
    char name[256];*/
    mxml_node_t * node_time;
    struct tm t;
    int ws;

    (void)lib_data;
    (void)sock;
    (void)data;
    (void)length;

    feederLog(LOG_DEBUG, "gpx: process\n");



    if (gpxTree == NULL)
    {
    	feederLog(LOG_ERR, "gpx: gpx not parsed\n");

	return -1;
    }
    

    feederLog(LOG_DEBUG, "gpx: searching\n");
    
    gpxNode = mxmlFindElement(gpxNode, gpxTree, "wpt", NULL, NULL, MXML_DESCEND);
    if (gpxNode != NULL)
    {
	feederLog(LOG_DEBUG, "gpx: found point lat %s\n", mxmlElementGetAttr(gpxNode, "lat"));
	feederLog(LOG_DEBUG, "gpx: found point lon %s\n", mxmlElementGetAttr(gpxNode, "lon"));
	
	result_array[0] = atof(mxmlElementGetAttr(gpxNode, "lat"));
	result_array[1] = atof(mxmlElementGetAttr(gpxNode, "lon"));

	sensors[0] = sensors[1] = 0x10;
	*type = VALUES_TYPE_POS;
	    
	node_time = mxmlFindElement(gpxNode, gpxTree, "time", NULL, NULL,  MXML_DESCEND);
	if (node_time)
	{
	    strptime(mxmlGetText(node_time, &ws), "%Y-%m-%dT%H:%M:%SZ", &t);
	    *tm = mktime(&t);
	    *id = 354330030514288;
	}
	else
	    *tm = 0; 
	res = 2;

    }
    else 
	res = 0;
    return res;
    /*
    while (size = getline(&line, &len, f), size >= 0)
    {
	if (strstr(line, "Saving sensor") != NULL)
	{
	    feederLog(LOG_DEBUG, "mortlog: %s\n", line);

	    getval(line, "id", name);
	    sensors[0] = atoi(name);

	    getval(line, "sensor", name);
	    feederLog(LOG_DEBUG, "mortlog: sensor %s\n", name);

	    
	    getval(line, "timestamp", name);
	    *tm = atoi(name);
	    getval(line, "value", name);
	    result_array[0] = atof(name);
	    getval(line, "device", name);
	    sensors[0] += (atoi(name) >> 8);
	    
	    *id = 105000000 + (atoi(name) & 0xFF);//todo: id must be configured by conf file, its not part of log file
	
	    
	//    feederLog(LOG_DEBUG, "mortlog: readline %f\n", result_array[0]);
	    *type = VALUES_TYPE_OBS;
	    
	    return 1;
	}
    }
    fclose(f);

    res = 0;
    return res;*/
}

int init(void *param)
{
    (void)param;

    feederLog(LOG_DEBUG, "mortlog: init\n");

    return 0;
}
