#include <stdio.h>

#define FILE_NAME_IN	"t6_SacramentocrimeJanuary2006.csv"
#define FILE_NAME_OUT	"t6_SacramentocrimePerHour.csv"
#define SIZE_ADDRESS	80
#define SIZE_CRIMEDESCR	80
#define SIZE_LINE	200
#define DAYS_MONTH	31
#define DAYS_WEEK	7
#define HOURS_DAY	24
#define DISTRICTS_SACRAMENTO	6

#define READ_OK		0

typedef struct {
	int	d,m,y,h,min;	// day, month, year, hour, minutes
}t_date;

typedef struct {
	int	num;
	char	letter;
}t_beat;

typedef struct {
	float	latitude;
	float	longitude;
} t_coor;

typedef struct {
	t_date	cdatetime;
	char	address[SIZE_ADDRESS];
	int	district;
	t_beat	beat;
	int	grid;
	char	crimedescr[SIZE_CRIMEDESCR];
	int	ucr_ncic_code;
	t_coor	coor;
} t_crime;

void print_crime(t_crime c) {
	printf("%02d/%02d/%02d %02d:%02d,\"%s\",%d,%d%c,%d,\"%s\",%d,%f,%f\n",
		c.cdatetime.d, c.cdatetime.m, c.cdatetime.y,
		c.cdatetime.h, c.cdatetime.min,
		c.address, c.district, c.beat.num, c.beat.letter,
		c.grid, c.crimedescr, c.ucr_ncic_code,
		c.coor.latitude, c.coor.longitude);
}

int read_crime(FILE *fd, t_crime *c) {
	int items;

// cdatetime,address,district,beat,grid,crimedescr,ucr_ncic_code,latitude,longitude
// 1/1/06 0:00,3108 OCCIDENTAL DR,3,3C        ,1115,10851(A)VC TAKE VEH W/O OWNER,2404,38.55042047,-121.3914158
	items = fscanf(fd, "%d/%d/%d %d:%d,%[^,],%d,%d%c%*[^,],%d,%[^,],%d,%f,%f\n",
		&c->cdatetime.m, &c->cdatetime.d, &c->cdatetime.y,
		&c->cdatetime.h, &c->cdatetime.min,
		c->address, &c->district, &c->beat.num, &c->beat.letter,
		&c->grid, c->crimedescr, &c->ucr_ncic_code,
		&c->coor.latitude, &c->coor.longitude);
	if (items == EOF) {
		return EOF; // EOF acostuma a estar definit a -1 a stdio.h
	} else if (items == 14) { // OK
		return READ_OK; // OK
	} else if (items == 7) { // Beat missing
		items = fscanf(fd, ",%d,%[^,],%d,%f,%f\n",
			&c->grid, c->crimedescr, &c->ucr_ncic_code,
			&c->coor.latitude, &c->coor.longitude);
		if (items == 5) {
			return READ_OK; // OK
		} else {
			fscanf(fd, "%*[^\n]"); // purgar la resta de linea amb errors
			return items; // ERR
		}
	} else {
		fscanf(fd, "%*[^\n]"); // purgar la resta de linea amb errors
		return items; // ERR
	}
}

void press_enter() {
	printf("Press ENTER key to Continue\n");
	scanf("%*c");
}

int main() {
	FILE *fd, *fd_out;
	char line[SIZE_LINE];
	t_crime c;
	int count = 0, line_num = 1;
	int district[DISTRICTS_SACRAMENTO] = {0};
	int day[DAYS_MONTH] = {0};
	int hour[DAYS_WEEK][HOURS_DAY] = {{0}};
	int sum_hour, sum_day;
	int i, h, d, err;

	fd = fopen(FILE_NAME_IN, "r");
	if (fd==NULL) {
		return -1;
	}

	fscanf(fd, "%s", line); // Llegir capcelera
	// printf("%s\n", line);

	while((err=read_crime(fd, &c)) != EOF) { // Parentesis!!!
		line_num++;
		if (err == READ_OK) {
			// print_crime(c);
			count ++;
			district[c.district-1]++;
			day[c.cdatetime.d-1]++;
			// 1.1.2006 fue Domingo
			hour[(c.cdatetime.d+5)%7][c.cdatetime.h]++;
		} else {
			printf("ERR %d in line %d\n", err, line_num);
		}
	}
	fclose(fd);

	printf("Total Count: %d\n", count);

	printf("Per District:\n");
	for(i=0;i<DISTRICTS_SACRAMENTO;i++) {
		printf("%d:\t%5d\n", i+1, district[i]);
	}
	press_enter();

	printf("Per Day:\n");
	for(i=0;i<DAYS_MONTH;i++) {
		printf("%2d: %d crimes\n", i+1, day[i]);
	}
	press_enter();

	fd_out = fopen(FILE_NAME_OUT, "w");
	if (fd_out==NULL) {
		return -1;
	}

	printf("Per Hour and Day of the Week:\n");
	printf("Hour\tMon\tTue\tWed\tThu\tFri\tSat\tSun\tSum\n");
	fprintf(fd_out, "Hour,Mon, Tue, Wed, Thu, Fri, Sat, Sun, Sum\n");
	for(h=0;h<HOURS_DAY;h++) {
		printf("%02d\t", h);
		fprintf(fd_out,"%2d,", h);
		for(d=0,sum_hour=0;d<DAYS_WEEK;d++) {
			printf("%4d\t", hour[d][h]);
			fprintf(fd_out, "%4d,", hour[d][h]);
			sum_hour += hour[d][h];
		}
		printf("%4d\n", sum_hour);
		fprintf(fd_out, "%4d\n", sum_hour);
	}
	printf("Sum");
	fprintf(fd_out, "Sum");
	for(d=0;d<DAYS_WEEK;d++) {
		for(h=0,sum_day=0;h<HOURS_DAY;h++) {
			sum_day += hour[d][h];
		}
		printf("\t%4d", sum_day);
		fprintf(fd_out, ",%4d", sum_day);
	}
	printf("\t%d\n", count);
	fprintf(fd_out, ",%d\n", count);

	fclose(fd_out);

	printf("\n\nS'ha creat el fitxer %s\n", FILE_NAME_OUT);
	printf("Pots obrir-lo amb una fulla de calcul\n");
	return 0;
}

