//#define _XOPEN_SOURCE //todo:??????
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <time.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;

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

    f = fopen("log.csv", "r");//todo: zadat confem nebo parametrem
    if (f == NULL)
    {
	feederLog(LOG_ERR, "csv: can not open input file (log.csv)\n");
	return -1;
    }

    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;
}


typedef struct meta_u_t
{
    uint64_t id;
    double multi;
} meta_u_t;

typedef struct sensobtinymeta_t
{
    struct meta_u_t u[32];
} sensobtinymeta_t;

static struct sensobtinymeta_t metas[24] =//todo: konfigurovat toto a casovou zonu devicy v conf, nebo to bude posilat noda
{
    {{{100000000,1.0}}},						//0
    {{{340340092,1.0},{340350004,1.0},{360200000,1.0}}},		//1
    {{{0,1.0}}},
    //{{{360200000,1.0}}},						//2
    {{{340070001,1.0},{410050001,1.0},{460010001,1.0}}},		//3
    {{{480020001,1.0},{490010001,1.0}}},				//4
    {{{470020001,1.0},{470010001,1.0}}},				//5
    {{{540090004,10.0},{550020004,10.0},{340370004,10.0}}},	//6
    
    {{{0,0.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;
    int size;
    char * sep;
    uint8_t i, j;
    struct tm time;
    int meta;
    uint32_t sensor_id;

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

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

    if (f == NULL)
    {
    	feederLog(LOG_ERR, "csv: file not opened\n");

	return -1;
    }
    
    j = 0;
    while (size = getline(&line, &len, f), size >= 0)
    {
        feederLog(LOG_DEBUG, "csv: %s", line);

	i = 0;
	sep = line;
	while (sep != NULL)
	{
	    switch (i)
	    {
		case 0:
		    if (strptime(sep, "%Y/%m/%d %H:%M:%S", &time) != NULL)
			*tm = timegm(&time);
		    else
			*tm = 0;
		    break;
		case 1:
		    break;
		case 2:
		    meta = -1;
		    sensor_id = atoi(sep);
		    if (sensor_id / 10000  == 16001)
			meta = sensor_id % 10000;
		    else
			sensors[0] = sensor_id;
		    break;
		default:
		    if (meta >= 0)
		    {
			if (metas[meta].u[i - 3].id != 0)
			{
			    sensors[i - 3] = metas[meta].u[i - 3].id;
			    result_array[i - 3] = atof(sep);
			    j++;
			}
		    }
		    else
		    {
			result_array[0] = atof(sep);
			j = 1;
		    }
		    break;
	    }
	    sep = strchr(sep, ';');
	    if (sep != NULL)
	    	sep += 1;
	    i++;
	}
//	*id = 1305167549149707;//MJ6 todo: musime jinak
//	*id = 1305167549144045;//MJ1 todo: musime jinak
	*id = 1305167549180936;//MJ4 nahradni
	*type = VALUES_TYPE_OBS;
	
/*
	    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;
*/	if (j > 0)
    	    return j;
    }
    fclose(f);

    res = 0;
    return res;
}

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

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

    return 0;
}
