#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gd.h>
#include <string.h>
#include "gdfonts.h"
#include "gdfontt.h"
#include "gdfontl.h"
#include "gdfontl.h"
#include "gdfontmb.h"
//#include "dates.h"
#include "calc.h"

main() {
        gdImagePtr im;
        FILE *out;
        FILE *temp;
	char linestring[100];
	char copystring[100];
        char *token;  
        char *line;
        char *search = " ";
	char workstring[25];
	char datestring[25];
	char labelstring[15];	
	char lastday[3];
	strcpy(lastday, "31");
	//double results[372][2];
	double coord1;
	double coord2;
	double coord3;
	double month_end = 100;
	double month_start = 1;
	int stagger = 0;

	struct results {
		double cash;
		double stock;
		double deposit;
		char date[12];
	};
	struct results myresults[375];
	

	int i = 0;
	int j = 0;
	int emptyvarstr;

	im = gdImageCreate(3800,1200);
        //allocate colors
        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 yellow = gdImageColorAllocate(im, 255, 255, 0);
        int orange = gdImageColorAllocate(im, 255, 128, 0);
        int purple = gdImageColorAllocate(im, 255, 0, 255);
        int brown = gdImageColorAllocate(im, 168, 95, 0);
        int grey = gdImageColorAllocate(im, 128, 128, 128);
	int custom = gdImageColorAllocate(im, 15, 15, 15);
	//int custom2 = gdImageColorAllocate(im, 100, 100, 100);
	int color = black;

	//gdImageFilledRectangle(im, 0, 0, 3800, 1200, black);

	int tenth = 0;
	for(i=0; i<11; i++) {
		emptyvarstr = sprintf( labelstring, "%d", 15000-tenth );
		tenth += 1500;
                gdImageString(im, gdFontMediumBold,   20   ,   i*85  , labelstring , color);
                gdImageLine(im, 40  , i*85, 3750   , i*85, color); 
	}

	for(i=0; i<375; i++) {
		myresults[i].cash = -1;
		myresults[i].stock = -1;
		myresults[i].deposit = 0;
		strcpy(myresults[i].date, "NULL");
	}

                myresults[20].cash = 0;
                myresults[20].stock = 0;
                myresults[20].deposit = 100;
                strcpy(myresults[20].date, "2009-01-20");

		i = DateCalc("2009-03-24");
                myresults[i].deposit = 30;
		i = DateCalc("2009-06-08");
                myresults[i].deposit = 3200;
		i = DateCalc("2009-07-27");
                myresults[i].deposit = 2000;
		i = DateCalc("2009-08-20");
                myresults[i].deposit = -1600;
		i = DateCalc("2009-10-26");
                myresults[i].deposit = 200;
                i = DateCalc("2009-12-02");
                myresults[i].deposit = 150;



        if( ( temp = fopen( "temp.txt", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening temp.txt\n" );
                exit( 1 );
        }

	//Long Cash as of 2009-03-11: 90.00
	//Long Stock as of 2009-03-12: 95.94
        while( fgets(linestring, sizeof(linestring), temp) != NULL)
        {


		strcpy(copystring, linestring);				 
		token = strtok(copystring, search);
		token = strtok(NULL, search); //type
		if(strcmp(token, "Cash") == 0) {
			j = 0;
		}else{
			j = 1;
		}
		token = strtok(NULL, search);
		token = strtok(NULL, search);
		token = strtok(NULL, search); //date
		strcpy(workstring, token);
		i = DateCalc(&workstring);
		strcpy(myresults[i].date, workstring); 		

		token = strtok(NULL, search);	//dollar amount	
		if(j == 0) {
			myresults[i].cash = atof(token);
		}else if(j ==1) {
			myresults[i].stock = atof(token);
		}
		//printf("token: %s\n", token);
		
		//i++;
		//i=0;

		printf("i: %d j: %d res: %lf %lf\n", i, j, myresults[i].cash, myresults[i].stock);

	}
	fclose(temp);

	//plot the chart

	gdImageLine(im, 50, 10,50,851, color);
	gdImageLine(im, 50, 851,3750,851, color);

	for(i=0; i<375; i++) {

		//results[i][j]
		if(myresults[i].cash >= 0) {
			coord1 = 850*(myresults[i].cash/15000);
			coord1 = 850 - coord1;
			//gdImageLine(im, i*10  , 850 , i*10   , coord1 , white);			
                	//gdImageLine(im, i*10-1, 850 , i*10-1 , coord1 , white);
                	//gdImageLine(im, i*10-2, 850 , i*10-2 , coord1 , white);
			gdImageLine(im, i*10-3, 850 , i*10-3 , coord1 , green);
			gdImageLine(im, i*10-4, 850 , i*10-4 , coord1 , green);
                        gdImageLine(im, i*10-5, 850 , i*10-5 , coord1 , green);
                        gdImageLine(im, i*10-6, 850 , i*10-6 , coord1 , green);
                        //gdImageLine(im, i*10-7, 850 , i*10-7 , coord1 , white);
                        //gdImageLine(im, i*10-8, 850 , i*10-8 , coord1 , white);
                        //gdImageLine(im, i*10-9, 850 , i*10-9 , coord1 , white);
		}else{
			coord1 = 850;
		}

		if(myresults[i].stock >= 0) {
			coord2 = 850*(myresults[i].stock/15000);
			coord2 = coord1 - coord2;
                	//gdImageLine(im, i*10  , coord1, i*10   , coord2, white);
                	//gdImageLine(im, i*10-1, coord1, i*10-1 , coord2, white);
			//gdImageLine(im, i*10-2, coord1, i*10-2 , coord2, blue);
			gdImageLine(im, i*10-3, coord1, i*10-3 , coord2, blue);
			gdImageLine(im, i*10-4, coord1, i*10-4 , coord2, blue);
                        gdImageLine(im, i*10-5, coord1, i*10-5 , coord2, blue);
                        gdImageLine(im, i*10-6, coord1, i*10-6 , coord2, blue);
                        //gdImageLine(im, i*10-7, coord1, i*10-7 , coord2, blue);
                        //gdImageLine(im, i*10-8, coord1, i*10-8 , coord2, white);
                        //gdImageLine(im, i*10-9, coord1, i*10-9 , coord2, white);
		}
		if(myresults[i].deposit > 0) {
			coord3 = 850*(myresults[i].deposit/15000);
			coord3 = coord2 - coord3;
			
                        gdImageLine(im, i*10-3, coord2, i*10-3 , coord3, custom);
                        gdImageLine(im, i*10-4, coord2, i*10-4 , coord3, custom);
                        gdImageLine(im, i*10-5, coord2, i*10-5 , coord3, custom);
                        gdImageLine(im, i*10-6, coord2, i*10-6 , coord3, custom); 


		}else if(myresults[i].deposit < 0) {
                        coord3 = 850*(myresults[i].deposit/15000);
                        coord3 = coord2 - coord3;
                
			//gdImageLine(im, i*10-2, coord2, i*10-2 , coord3, red);
                        gdImageLine(im, i*10-3, coord2, i*10-3 , coord3, red);
                        gdImageLine(im, i*10-4, coord2, i*10-4 , coord3, red);
                        gdImageLine(im, i*10-5, coord2, i*10-5 , coord3, red);
                        gdImageLine(im, i*10-6, coord2, i*10-6 , coord3, red);
			//gdImageLine(im, i*10-7, coord2, i*10-7 , coord3, red);

		}

		if(strcmp(myresults[i].date, "NULL") != 0 && i > 19) {
			GetMonth(&myresults[i].date);
			//if(i%31 == 0 && strcmp(myresults[i].date, "NULL") != 0) {
			if(atoi(day) < atoi(lastday)) {
				//GetMonth(&myresults[i].date);
				gdImageString(im, gdFontMediumBold,   i*10-4   ,   950  , month , red);
				//GetMonth(&myresults[i].date);
				gdImageLine(im, i*10-4  , 925, i*10-4   , 950, red);
				//if(month_end < 
				//month_start =
				j=i; 				
				do{
					j--;
					//month_end = myresults[i-1].cash + myresults[i-1].stock; 
					//for(j=i;strcmp(myresults[j].date, "NULL") == 0; j--) {
					month_end = myresults[j].cash + myresults[j].stock;
					//j--;
				}while(strcmp(myresults[j].date, "NULL") == 0);

				

				if(month_start < month_end) {
					
					emptyvarstr = sprintf( labelstring, "+%.02lf%%",(month_end-month_start)/month_start * 100);
					gdImageString(im, gdFontMediumBold,   i*10-140   ,   1000  , labelstring , red);
				}else{
					emptyvarstr = sprintf( labelstring, "-%.02lf%%",(month_start-month_end)/month_end * 100);
					gdImageString(im, gdFontMediumBold,   i*10-140   ,   1000  , labelstring , red);
				}
				month_start = month_end;
			
			}
			strcpy(lastday, day);
		}

		if(strcmp(myresults[i].date, "NULL") != 0) {
			//strcpy(lastday, day);
			strcpy(labelstring, day);
			//emptyvarstr = sprintf( labelstring, "%d", (i/31)+1 );
			gdImageString(im, gdFontSmall,   i*10-9   ,   875+stagger  , labelstring , color);
			gdImageLine(im, i*10-4  , 850, i*10-4   , 875+stagger, color);
			if(stagger == 25) {
				stagger = 0;
			}else{
				stagger = 25;
			}


/*			if(stagger == 0) {
				stagger = 25;
			}else if(stagger == 25) {
				stagger = 50;
			}else if(stagger == 50) {
				stagger = 75;
			}else if(stagger == 75) {
				stagger = 100;
			}else if(stagger == 100) {
				stagger = 125;
			}else if(stagger == 125) {
				stagger = 150;
			}else if(stagger == 150) {
				stagger = 0;
			}

*/
		}

	}


        gdImageFilledRectangle(im, 1500, 975, 1520, 995, green);
        gdImageString(im, gdFontSmall,   1525   ,   985  , "Cash" , color);

        gdImageFilledRectangle(im, 1600, 975, 1620, 995, blue);
        gdImageString(im, gdFontSmall,   1625   ,   985  , "Securities" , color);

        gdImageFilledRectangle(im, 1700, 975, 1720, 995, custom);
        gdImageString(im, gdFontSmall,   1725   ,   985  , "Deposit" , color);

        gdImageFilledRectangle(im, 1800, 975, 1820, 995, red);
        gdImageString(im, gdFontSmall,   1825   ,   985  , "Withdrawl" , color);

        gdImageFilledRectangle(im, 1900, 975, 1920, 995, brown);
        gdImageString(im, gdFontSmall,   1925   ,   985  , "Commissions/Fees" , color);



        //plot total withdrawl/deposit
        
        //coord1 = 850-(850*(5530/15000));
        //coord1 = ceil(coord1);
	//coord1 = 850-313;
	//coord2 = 850-91;
	//-1301
	//5,680.00
	//-1,600.00
	//

//950
	//coord1 =  850 - (850*(5680/15000));
	coord1 = 528;

	//coord1 = 850-(380-91-67);
        gdImageFilledRectangle(im, 3740, 850, 3760, coord1, custom);
        //gdImageLine(im, 40  , i*85, 3750   , i*85, black);
        //gdImageString(im, gdFontSmall,   625   ,   985  , "ytd total" , black);
                
        //coord1 = 850-(850*(1600/15000));
        //coord1 = ceil(coord1);
	coord2 = 91;
	coord2 += coord1;
        gdImageFilledRectangle(im, 3740, coord1, 3760, coord2, red);
	gdImageLine(im, 3750  , 850, 3750   , 950, red);
        gdImageString(im, gdFontMediumBold,   3710   ,   950  , "YTD Totals" , red);


	coord3 = 74;
	coord3 += coord2;
        gdImageFilledRectangle(im, 3740, coord2, 3760, coord3, brown);
        //gdImageLine(im, 3710  , 850, 3710   , 950, red);  
        //gdImageString(im, gdFontLarge,   3700   ,   950  , "ytd totals" , red);





	out = fopen("graph.png", "w");
	gdImageInterlace(im, 1);
	gdImagePng(im, out);
	fclose(out);
	gdImageDestroy(im);

	
	return 0;

}
