#include <stdio.h>
#include <stdlib.h>
#include <gd.h>
#include "gdfonts.h"
#include "gdfontmb.h"
#include <math.h>

#define TESTED 17
#define MAX_VOLT 2000
#define MAX_CLOCK 2000
#define LINE_GRANULARITY 25

struct pairs{
	int voltage;
	int clock;
};

const struct pairs Pairs[TESTED] = {{800, 852}, {865, 1000}, {875, 1200}, {875, 1300}, {885, 1325}, {885, 1350}, {895, 1375}, {920, 1400}, {945, 1425}, {965, 1450}, {965, 1455}, {990, 1475}, {1010, 1500}, {1030, 1525}, {1060, 1550}, {1090, 1575}, {1115, 1600}};


int main(int argc, char **argv){
	gdImagePtr im;
	FILE *out;
	int width               = 1024;
	int height              = 1024;
	//int pixels_per_day      = 1;
	//int pixels_whitespace   = 1;
	//double starting_balance = 0;
	char buf[1024];
	const char *output_file = "scaling.png";
	int white = gdImageColorAllocate(im, 255, 255, 255);
	int black = gdImageColorAllocate(im, 0,   0,   0);
	int red   = gdImageColorAllocate(im, 255, 0,   0);
	int blue  = gdImageColorAllocate(im, 0,   0,   255);
	int green = gdImageColorAllocate(im, 0,   255, 0);
	int styleDashed[9] = {blue, blue, blue, green, green, green, red, red, red};
/*	styleDashed[0] = blue;
	styleDashed[1] = blue;
	styleDashed[2] = blue;
	styleDashed[3] = green;
	styleDashed[4] = green;
	styleDashed[5] = green;
	styleDashed[6] = red;
	styleDashed[7] = red;
	styleDashed[8] = red;*/
	gdImageSetStyle(im, styleDashed, 9);

	char chart_label_a[12];

double balance_tenth = ceil(max_balance) / 10;
double height_tenth = (height-200)/10;

	int emptyvarstr;
	int downnum = height-100;
	int overnum = 0;

	printf("output_file: %s\n", output_file);

	//draw chart boundaries/labels on image
	gdImageLine(im, 100, 10, 100, height-100, black);
	gdImageLine(im, 100, height-100, width-100, height-100, black);

	gdImageString(im, gdFontMediumBold,   width-85   ,   height-85        , "Clock" , black);
	gdImageString(im, gdFontMediumBold,   0   ,   10        , "Voltage" , black);

	for(line_num=0; line_num<11; line_num++){
		gdImageLine(im, 50, downnum, width-85, downnum, gdStyled);
		emptyvarstr = sprintf(chart_label_a, "$%.02lf", current_balance);
		gdImageString(im, gdFontMediumBold, 5, downnum, chart_label_a , black);
		downnum -= LINE_GRANULARITY;
		overnum += LINE_GRANULARITY;
	}


	//plot data from transactions/days on image
	unsigned int day_count;
	unsigned int days_cur_month;
	unsigned int start_pixel_month;
	unsigned int end_pixel_month;
	unsigned int start_pixel_year;
	unsigned int end_pixel_year;
	int x1, y1, x2, y2;
	unsigned int days_from_start;
	//date_start
	char months[] = "JFMAMJJASOND";
	char tmp_char[2];
	//char *months[]={"J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"};
	unsigned int last_year = 0;
	unsigned int last_month = 0;
	unsigned int last_m_X = 100;

	y1 = height - 100;
	for(day_count=0; day_count<total_days; day_count++){
		date_tmp.day    = transactions[day_count].date.day;

		date_tmp.month  = transactions[day_count].date.month;

		date_tmp.year   = transactions[day_count].date.year;
		days_from_start = Total_Days(&date_start, &date_tmp);
		x1 = 100 + days_from_start * (pixels_per_day+pixels_whitespace);
		x2 = x1 - pixels_per_day;
		y2 = y1 - (height-200)*transactions[day_count].balance/max_balance;

		gdImageFilledRectangle(im, x1, y1, x2, y2, green);
		if(last_month != date_tmp.month){
			gdImageLine(im, x1, y1, x1, height-75, red);
			last_month = date_tmp.month;
			tmp_char[0] = months[last_month-1];
			tmp_char[1] = '\0';
			gdImageString(im, gdFontMediumBold,   x1,   height-75, tmp_char, red);
		}
		if(last_year != date_tmp.year){
			gdImageLine(im, x1, height-75, x1, height-50, blue);
			last_year = date_tmp.year;
			emptyvarstr = sprintf(chart_label_a, "%d", last_year);
			gdImageString(im, gdFontMediumBold,   x1,   height-50, chart_label_a, blue);
		}
	}

/*

	days_cur_month = Days_In_Month(&date_start) - date_start.date.day;
	for(day_count=0; day_count<days_cur_month; day_count++){

		//allocate labels on chart for each month/year

		//plot each day on chart


	for(day_count=0; day_count<total_days; day_count++){
		days

	}
*/

	//write out image
	out = fopen(output_file, "w");
	gdImageInterlace(im, 1);
	gdImagePng(im, out);
	fclose(out); 
	gdImageDestroy(im);
	free(input_file);
	free(output_file);
	free(transactions);

	return 0;
}
