#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "dbase.h"

#define DAYSEC 86400

//fake data
#define DayOfWeek 1
#define DayOfMonth 2


int timestr2sec(char* timstr){

	int sec=0;
	sec += 3600*(atoi(timstr + 11*sizeof(char)));
	sec += 60*(atoi(timstr + 14*sizeof(char)));
	sec += 1*(atoi(timstr + 17*sizeof(char)));

	return(sec);

}
int GetDay(int gettday){
time_t rawtime;
struct tm *info;

time( &rawtime );
info = localtime( &rawtime );

if(gettday==DayOfMonth)
return(info->tm_mday);
else
return(info->tm_wday);
}

int main(int argc, char** argv){



int work=0;
int stop=0;
int time=0;
int lunch=0;
int other=0;
char lastact = ' ';
char act;
int usercounter=0;
int userid=0;
int row=0;
char day = GetDay(DayOfMonth);
char dayofweek = GetDay(DayOfWeek);

char* data;

DBase_Open("/sdcard/test.db");
DBase_AttachDaily("/var/tmp/daily");
DBase_TodayIsFreeday();



//For all users
while(userid=DBase_ResolveUserID(usercounter++)){

	DBase_SetUserID(userid);
	row=0;
	while(data=DBase_GetAttendRow()){ //For each row
		act = data[1];
		time = timestr2sec(data+(2*sizeof(char)));
		printf("act: %c time: %s \n",data[1],data+(2*sizeof(char)));
		//Counting algorhytm

				if(row==0){
				if(act=='*')
					work+=(DAYSEC-time);
				stop=time;
				lastact=act;	
			}
			else{
				if(act==lastact) break;
				if(act!='*'){
					if(act=='3')
						lunch+=(stop-time);
					else if(act!='#')
						other+=(stop-time);

						stop=time;			
				}
				else{
					work+=(stop-time);
					stop=time;		
				}
			lastact=act;
	

			}


				row++;
		}

	//Write to dbase
	
DBase_InsertStats(dayofweek,day,userid, work, lunch, other);
	

printf("%u Prace: %02u:%02u Obed: %02u:%02u Ostatni: %02u:%02u Celkem %02u:%02u \n",userid,work/3600,((work%3600))/60,lunch/3600,lunch%3600/60,other/3600,other%3600/60,(work+lunch+other)/3600,(work+lunch+other)%3600/60);
work=0;
lunch=0;
other =0;

}




return(0);
}
