#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

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

char phenomenon_list[][128] = {
    "urn:ogc:def:phenomenon:OGC:1.0.30:RelativeHumidity",
    "urn:ogc:def:phenomenon:OGC:1.0.30:WindDirection",
    "urn:ogc:def:phenomenon:OGC:1.0.30:WindSpeed",
    "urn:ogc:def:phenomenon:OGC:1.0.30:AirPressure",
    "urn:ogc:def:phenomenon:OGC:1.0.30:DewPoint",
    "urn:ogc:def:phenomenon:OGC:1.0.30:AirTemperature",
    "urn:ogc:def:phenomenon:OGC:1.0.30:RainIntensity",
    "urn:ogc:def:phenomenon:OGC:1.0.30:HailIntensity",
    ""};

int init(void * param)
{
	return 0;
}

typedef struct senso_t
{
    unsigned long long int id;
    struct tm t;
//    double lat, lon, alt;
    int phenomenon;
    double value;
} senso_t;


unsigned int process(unsigned char * data, unsigned int length, unsigned long long int * id, time_t * tm, unsigned int * status, unsigned char * result, unsigned int * pbb_status, double * lat, double * lon, double * alt)
{
    struct senso_t senso;
    
    char * pos, * next;
    char name[256];
    char * value;
    int len;
    int phenomenon;
    
    pos = strchr(data, '?');
    if (pos == NULL)
    {
	printf("Senso_http: No ? in GET\n");
	return 0;
    }
    pos++;
    
    while (next = strpbrk(pos, "& "), next != NULL)
    {
	len = next - pos;
	if (len >= 256)
	{
	    printf("Senso_http: GET value too long (> 256)\n");
	    continue;
	}
	memcpy(name, pos, len);
	name[len] = 0;
	
	if (value = strchr(name, '='), value != NULL)
	{
	    *value = 0;
	    value++;

	    if (strcmp(name, "Operation") == 0)
	    {
		if (strcmp(value, "InsertObservation") != 0)
		{
		    printf("Senso_http: bad operation %s\n", value);
    		    return 0;
		}
	    }
	    
	    if (strcmp(name, "value") == 0)
	    {
		senso.value = atof(value);
	    }
	    if (strcmp(name, "phenomenon_id") == 0)
	    {
		for (phenomenon = 0; (phenomenon_list[phenomenon][0] != 0) && (strcmp(value, phenomenon_list[phenomenon]) != 0); phenomenon++) ;
		
		if (phenomenon_list[phenomenon][0] != 0)
		    senso.phenomenon = phenomenon;
		else
		    senso.phenomenon = -1;
	    }
	    if (strcmp(name, "sensor_name") == 0)
	    {
		senso.id = atol(value);		//todo: staci atol? (delka typu)
	    }
	    if (strcmp(name, "date") == 0)
	    {
		sscanf(value, "%d-%d-%d+%d:%d:%d", 
		    &senso.t.tm_year, &senso.t.tm_mon, &senso.t.tm_mday, &senso.t.tm_hour, &senso.t.tm_min, &senso.t.tm_sec);
		senso.t.tm_year -= 1900;
	        senso.t.tm_mon -= 1;
	    }
	    
	}
	
	pos = next + 1;
    }
    
    printf("Senso_http: %llu, on %04d-%02d-%02d %02d:%02d:%02d\n", senso.id, senso.t.tm_year + 1900, senso.t.tm_mon + 1, senso.t.tm_mday, senso.t.tm_hour, senso.t.tm_min, senso.t.tm_sec);
    printf("Senso_http: %s  = %f\n", phenomenon_list[senso.phenomenon], senso.value);

    *id = senso.id;
    *tm = mktime(&(senso.t));
    *status = 0;
    *lat = *lon = 0.0;
    *alt = 999999.0;
    *pbb_status = PBB_ON;
    result[0] = 0;
    if (senso.phenomenon >= 0)
    {
	for (phenomenon = 0; phenomenon < 16; phenomenon++)
	{
	    if (phenomenon == senso.phenomenon)
    		sprintf(result + strlen(result), "'%d'", (int)(senso.value * 1000));
	    else
		sprintf(result + strlen(result), "NULL");
	    if (phenomenon != 15)
		sprintf(result + strlen(result), ",");
	}
	return 16;
    }
    return 0;
}
