/*  This file is part of lscomp.

    lscomp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    lscomp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with lsnet.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __GTK_FILESYSTEM_H__
#define __GTK_FILESYSTEM_H__ 1


void load_portfolio(gchar *filename){
	FILE* fp;
	gchar linestring[25*9];
	int i=0;
	int j;
	const char *token;
	const char *search = " ";
	int cols = 0;
	int rows = 0;

	//ensure the correct number of cols/rows
	memset ((void*) linestring, '\0', sizeof (linestring));
	if( ( fp = fopen(filename, "r" ) ) == NULL ) {
		fprintf( stderr, "Error opening %s\n", filename );
		return;
	}
	while( fgets(linestring, sizeof(linestring), fp) != NULL){
		if(rows == 0){
			//count cols
			token = strtok(linestring, search);
			while(token != NULL){
				token = strtok(NULL, search);
				cols++;
			}
			cols -= 3;
		}
		rows++;
		
	}
	rows -= 1;
	printf("rows: %d cols: %d\n", rows, cols);	
	fclose(fp);

	gtk_spin_button_set_value(GTK_SPIN_BUTTON(Port_bt.button1), (gdouble)rows);
	Port_win.port_rows = rows;
	//Port_win.port_cols = Port_win.last_cols;

	port_adj_row();

	gtk_spin_button_set_value(GTK_SPIN_BUTTON(Port_bt.button2), (gdouble)cols);
	Port_win.port_cols = cols;

	port_adj_col();


	memset ((void*) linestring, '\0', 25*9);
	if( ( fp = fopen(filename, "r" ) ) == NULL ) {
		fprintf( stderr, "Error opening %s\n", filename );
		return;
	}
	while( fgets(linestring, sizeof(linestring), fp) != NULL){
		linestring[strlen(linestring)-1] = '\0';

		token = strtok(linestring, search);
		//ignore first 3 "Ticker Shares Price "
		if(i==0){
			token = strtok(NULL, search);
			token = strtok(NULL, search);
			token = strtok(NULL, search);
		}else{
			//get pos_entries
			for(j=0; j<3; j++){
				gtk_entry_set_text(GTK_ENTRY(Port_win.pos_entries[i][j]), token);
				token = strtok(NULL, search);
			}
		}
		//get entries
		for(j=0; j<Port_win.port_cols; j++){
			
			gtk_entry_set_text(GTK_ENTRY(Port_win.entries[i][j]), token);
			token = strtok(NULL, search);
		}	
		i++;
		//memset ((void*) linestring, '\0', sizeof (linestring));
	}
	fclose(fp);
	//clear any excess feilds
	//while(i < Port_win.port_rows+1){
		
	//}


}

void save_portfolio(gchar *filename){
	FILE* fp;
	gchar linestring[25*9];
	int i;
	int j;
	int k = 0;
	int all_space = 0;
	memset ((void*) linestring, '\0', sizeof (linestring));
	strcpy(linestring, "Ticker Shares Price ");	

	if ( (fp = fopen(filename, "w")) ){
		for(i=0; i<Port_win.port_rows+1; i++){
			for(j=0; j<3; j++){
				if(i != 0){
					strcat(linestring, gtk_entry_get_text(GTK_ENTRY(Port_win.pos_entries[i][j])) );
					strcat(linestring, " ");
					
				}
				//all_space = 1;
				//for(k=0; linestring[k] != '\0'; k++){
				//	if(linestring[k] != ' ')
				//		all_space = 0;
				//}
				//if
			}
			if(i != 0){
				for(k=0; linestring[k] != '\0'; k++){
					if(linestring[k] != ' ')
						all_space = 0;
				}
			}
			if(!all_space){
				for(j=0; j<Port_win.port_cols; j++){
					strcat(linestring, gtk_entry_get_text(GTK_ENTRY(Port_win.entries[i][j])) );
					if(j != Port_win.port_cols-1){
						strcat(linestring, " ");
					}
				}

				fprintf(fp, "%s\n", linestring );

			}
			all_space = 1;
			memset ((void*) linestring, '\0', 25*9);
		}

	}
	//g_free(linestring);
	fclose(fp);
}

#endif
