/*
    lscomp is a light weight portfolio/resource suite.
    Copyright (C) 2010  sterling pickens

    This program 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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include "gtk_structs.h"
#include "gtk_funcs.h"
#include <gtk/gtk.h>
#include <cairo/cairo.h>


int main(int argc, char **argv){
	gtk_init (&argc, &argv);
	guint i = 0;
	guint j = 0;
	gint k = 0;
	Port_win.port_rows = 4;
	Port_win.port_cols = 10;
	Port_win.last_rows = 4;
	Port_win.last_cols = 10;
	Port_win.commission = 0;
	//gint ret_index = -1;
	const gchar *port_label = "Portfolio";
	const gchar *tool_label = "Tools";
	gchar *R_value = malloc(sizeof(char)*15);

	struct MainWindow mw_cont;
	struct NoteBook nb_cont;

//	struct PortTab Port_win;
	struct ToolTab Tool_win;
//	struct ButtonTable Port_bt;
	struct ButtonTable Tool_bt;
	Port_win.use_R = 0;

	mw_cont.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_container_set_border_width (GTK_CONTAINER(mw_cont.window), 2);
	gtk_window_set_default_size(GTK_WINDOW(mw_cont.window),800,600);
	gtk_window_set_title(GTK_WINDOW(mw_cont.window), "lscomp v0.1.2");
	//gtk_widget_set_parent_window(GTK_WIDGET(mw_cont.window), mw_cont.gdkwindow);
	nb_cont.book        = gtk_notebook_new();
	nb_cont.page_label1 = gtk_label_new(port_label);
	nb_cont.page_label2 = gtk_label_new(tool_label);

	Port_win.pane   = gtk_hpaned_new();
	//Tool_win.pane = gtk_hpaned_new();

	gtk_paned_set_position(GTK_PANED(Port_win.pane), 600);
	//gtk_paned_set_position(GTK_PANED(Tool_win.pane), 600);

//	Port_win.drawing_area   = gtk_drawing_area_new();
//	Tool_win.drawing_area = gtk_drawing_area_new();

	Port_win.scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	Tool_win.scrolledwin = gtk_scrolled_window_new(NULL, NULL);

	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Port_win.scrolledwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Tool_win.scrolledwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	Port_win.main_table = gtk_table_new(1, 2, FALSE);
	Port_win.out_table = gtk_table_new(Port_win.port_rows, Port_win.port_cols, TRUE);
	Port_win.pos_frame = gtk_frame_new("Position");
	//gtk_frame_set_label_align(GTK_FRAME(Port_win.pos_frame), 0.5, 0.5);
	Port_win.pos_table = gtk_table_new(Port_win.port_rows, 3, TRUE);
	//Port_win.pos_lab1 = gtk_label_new("Ticker");
	//Port_win.pos_lab2 = gtk_label_new("Shares");
	//Port_win.pos_lab3 = gtk_label_new("Price");
	Port_win.pos_labels = malloc(sizeof(GtkWidget *)*3);
	Port_win.pos_labels[0] = gtk_label_new("Ticker");
	Port_win.pos_labels[1] = gtk_label_new("Shares");
	Port_win.pos_labels[2] = gtk_label_new("Price");

	Port_win.out_frame = gtk_frame_new("prices for $ change");

	//Tool_win.table = gtk_table_new(2, 13, TRUE);

	//10 * PANGO_SCALE
	//pango_font_description_set_size

	Port_win.font_desc = pango_font_description_new();
	Port_win.font_size = 12;
	pango_font_description_set_size(Port_win.font_desc, Port_win.font_size * PANGO_SCALE );

	Port_win.pos_entries = malloc(sizeof(GtkWidget ***) * (Port_win.port_rows+1));
	Port_win.entries = malloc(sizeof(GtkWidget ***) * (Port_win.port_rows+1));
	for(i=0; i<=Port_win.port_rows; i++){
		Port_win.pos_entries[i] = malloc(sizeof(GtkWidget **) * 3);
		Port_win.entries[i] = malloc(sizeof(GtkWidget **) * Port_win.port_cols);
	}
	
	i=0;
	for(j=0; j<3; j++){
		gtk_widget_modify_font(GTK_WIDGET(Port_win.pos_labels[j]), Port_win.font_desc);
		gtk_table_attach(GTK_TABLE(Port_win.pos_table), Port_win.pos_labels[j], j, j+1, i, i+1,
			GTK_EXPAND, GTK_EXPAND, 0, 0);
	}
	k = -10;
	for(j=0; j<Port_win.port_cols; j++){
		Port_win.entries[i][j] = gtk_entry_new();
		k+=10;
		if(sprintf(R_value,"%d", k) < 0){
			printf("sprintf Error!\n");
			exit(1);
		}
		//itoa(k, (char *)R_value, 10);
		gtk_entry_set_text(GTK_ENTRY(Port_win.entries[i][j]), (const gchar *)R_value);

		gtk_widget_modify_font(Port_win.entries[i][j], Port_win.font_desc);
		gtk_entry_set_width_chars(GTK_ENTRY(Port_win.entries[i][j]), 14);
		gtk_table_attach(GTK_TABLE(Port_win.out_table), Port_win.entries[i][j], j, j+1, i, i+1,
			GTK_EXPAND, GTK_EXPAND, 0, 0);
	}

	for(i=1; i<=Port_win.port_rows; i++){
		for(j=0; j<3; j++){
			Port_win.pos_entries[i][j] = gtk_entry_new();
			gtk_widget_modify_font(Port_win.pos_entries[i][j], Port_win.font_desc);
			gtk_entry_set_width_chars(GTK_ENTRY(Port_win.pos_entries[i][j]), 14);
			gtk_table_attach(GTK_TABLE(Port_win.pos_table), Port_win.pos_entries[i][j], j, j+1, i, i+1,
				GTK_EXPAND, GTK_EXPAND, 0, 0);
		}
		for(j=0; j<Port_win.port_cols; j++){
			Port_win.entries[i][j] = gtk_entry_new();
			gtk_widget_modify_font(Port_win.entries[i][j], Port_win.font_desc);
			gtk_entry_set_width_chars(GTK_ENTRY(Port_win.entries[i][j]), 14);
			gtk_table_attach(GTK_TABLE(Port_win.out_table), Port_win.entries[i][j], j, j+1, i, i+1, 
				GTK_EXPAND, GTK_EXPAND, 0, 0);
		}
	}
	gtk_container_add (GTK_CONTAINER(Port_win.pos_frame), GTK_WIDGET(Port_win.pos_table));
	gtk_container_add (GTK_CONTAINER(Port_win.out_frame), GTK_WIDGET(Port_win.out_table));
	gtk_table_attach(GTK_TABLE(Port_win.main_table), Port_win.pos_frame,  0, 1, 0, 1,
		GTK_SHRINK, GTK_SHRINK, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_win.main_table), Port_win.out_frame,  1, 2, 0, 1,
		GTK_SHRINK, GTK_SHRINK, 0, 0);



	Tool_win.main_table = gtk_table_new(1, 3, TRUE);
	Tool_win.comp_table = gtk_table_new(3, 5, FALSE);
	Tool_win.comp_s_table = gtk_table_new(3, 5, FALSE);
	Tool_win.recoup_table = gtk_table_new(3, 2, FALSE);

	Tool_win.comp_frame = gtk_frame_new("Compound");
	Tool_win.comp_s_frame = gtk_frame_new("Compound Series");
	Tool_win.recoup_frame = gtk_frame_new("Recoup");


	Tool_win.comp_button   = gtk_button_new_with_label("Calc");
	Tool_win.comp_s_button = gtk_button_new_with_label("Calc");
	Tool_win.recoup_button = gtk_button_new_with_label("Calc");

	Tool_win.comp_labels[0] = gtk_label_new("Start Principle: ");
	Tool_win.comp_labels[1] = gtk_label_new("Rate: ");
	Tool_win.comp_labels[2] = gtk_label_new("End Product: ");
	Tool_win.comp_labels[3] = gtk_label_new("Compounds: ");
	for(i=0; i<4; i++){
		Tool_win.comp_entries[i] = gtk_entry_new();
		gtk_entry_set_width_chars(GTK_ENTRY(Tool_win.comp_entries[i]), 9);
		gtk_table_attach(GTK_TABLE(Tool_win.comp_table), Tool_win.comp_labels[i],  
			0, 1, i, i+1, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
		gtk_table_attach(GTK_TABLE(Tool_win.comp_table), Tool_win.comp_entries[i],  
			1, 2, i, i+1, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
	}

	Tool_win.comp_s_labels[0] = gtk_label_new("Start Principle: ");
	Tool_win.comp_s_labels[1] = gtk_label_new("Rate/s: ");
	Tool_win.comp_s_labels[2] = gtk_label_new("End Product: ");
	for(i=0; i<3; i++){
		Tool_win.comp_s_entries[i] = gtk_entry_new();
		gtk_entry_set_width_chars(GTK_ENTRY(Tool_win.comp_s_entries[i]), 9);
		gtk_table_attach(GTK_TABLE(Tool_win.comp_s_table), Tool_win.comp_s_labels[i],
			0, 1, i, i+1, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
		gtk_table_attach(GTK_TABLE(Tool_win.comp_s_table), Tool_win.comp_s_entries[i],
			1, 2, i, i+1, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
	}
	Tool_win.recoup_labels[0] = gtk_label_new("% Loss: ");
	Tool_win.recoup_labels[1] = gtk_label_new("% to Recoup: ");
	for(i=0; i<2; i++){
		Tool_win.recoup_entries[i] = gtk_entry_new();
		gtk_entry_set_width_chars(GTK_ENTRY(Tool_win.recoup_entries[i]), 9);
		gtk_table_attach(GTK_TABLE(Tool_win.recoup_table), Tool_win.recoup_labels[i],
			0, 1, i, i+1, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
		gtk_table_attach(GTK_TABLE(Tool_win.recoup_table), Tool_win.recoup_entries[i],
			1, 2, i, i+1, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
	}

	gtk_table_attach(GTK_TABLE(Tool_win.comp_table),   Tool_win.comp_button,  2, 3, 3, 4, 
		GTK_SHRINK, GTK_SHRINK, 0, 0);
	gtk_table_attach(GTK_TABLE(Tool_win.comp_s_table), Tool_win.comp_s_button,  2, 3, 2, 3, 
		GTK_SHRINK, GTK_SHRINK, 0, 0);
	gtk_table_attach(GTK_TABLE(Tool_win.recoup_table), Tool_win.recoup_button,  2, 3, 1, 2, 
		GTK_SHRINK, GTK_SHRINK, 0, 0);


	gtk_container_add (GTK_CONTAINER(Tool_win.comp_frame), GTK_WIDGET(Tool_win.comp_table));
	gtk_container_add (GTK_CONTAINER(Tool_win.comp_s_frame), GTK_WIDGET(Tool_win.comp_s_table));
	gtk_container_add (GTK_CONTAINER(Tool_win.recoup_frame), GTK_WIDGET(Tool_win.recoup_table));


	gtk_table_attach(GTK_TABLE(Tool_win.main_table), Tool_win.comp_frame,  0, 1, 0, 1, 
		GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
	gtk_table_attach(GTK_TABLE(Tool_win.main_table), Tool_win.comp_s_frame,  0, 1, 1, 2,
		GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
	gtk_table_attach(GTK_TABLE(Tool_win.main_table), Tool_win.recoup_frame,  0, 1, 2, 3, 
		GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);

	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(Tool_win.scrolledwin), Tool_win.main_table);


	Port_bt.table = gtk_table_new(10, 2, TRUE);
	Tool_bt.table   = gtk_table_new(3, 1, TRUE);

//gtk_widget_set_size_request(GTK_WIDGET(Port_bt.table), 0, 0);
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(Port_win.scrolledwin), Port_win.main_table);

	Port_bt.spin_adj1 = gtk_adjustment_new(Port_win.port_rows, 0.0, 25.0, 1.0, 1.0, 0.0);
	Port_bt.spin_adj2 = gtk_adjustment_new(Port_win.port_cols, 1.0, 25.0, 1.0, 1.0, 0.0);
	Port_bt.spin_adj3 = gtk_adjustment_new(Port_win.font_size, 1.0, 100.0, 1.0, 1.0, 0.0);
	Port_bt.spin_adj4 = gtk_adjustment_new(Port_win.commission, 0.00, 1000.00, 0.01, 0.25, 0.00);

	Port_bt.button1 = gtk_spin_button_new(GTK_ADJUSTMENT(Port_bt.spin_adj1), 1, 1);
	Port_bt.button2 = gtk_spin_button_new(GTK_ADJUSTMENT(Port_bt.spin_adj2), 1, 1);
	Port_bt.button3 = gtk_spin_button_new(GTK_ADJUSTMENT(Port_bt.spin_adj3), 1, 1);
	Port_bt.button4 = gtk_spin_button_new(GTK_ADJUSTMENT(Port_bt.spin_adj4), 0.01, 2);
	Port_bt.button5 = gtk_button_new_with_label("Calc");
	Port_bt.button6 = gtk_button_new_with_label("Load file");
	Port_bt.button7 = gtk_button_new_with_label("Save file");
	//Port_bt.button7 = gtk_spin_button_new(GTK_ADJUSTMENT(Port_bt.spin_adj4), 1, 1);

//gtk_widget_set_size_request(GTK_WIDGET(Port_bt.button1), 0, 0);
//gtk_widget_set_size_request(GTK_WIDGET(Port_bt.button2), 0, 0);
//gtk_widget_set_size_request(GTK_WIDGET(Port_bt.button3), 0, 0);
//gtk_widget_set_default_size(GTK_WIDGET(Port_bt.button1), 0, 0);
//gtk_widget_set_default_size(GTK_WIDGET(Port_bt.button2), 0, 0);
//gtk_widget_set_default_size(GTK_WIDGET(Port_bt.button3), 0, 0);
	Port_bt.label1 = gtk_label_new("Positions");
	Port_bt.label2 = gtk_label_new("Fields");
	Port_bt.label3 = gtk_label_new("Font Size");
	Port_bt.label4 = gtk_label_new("Roundtrip Commission");
	//Port_bt.file_load = gtk_file_chooser_dialog_new("load file", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, NULL);
/*
	Tool_bt.button1 = gtk_button_new_with_label("test1");
	Tool_bt.button2 = gtk_button_new_with_label("test2");
	Tool_bt.button3 = gtk_button_new_with_label("test3");
gtk_widget_set_size_request(GTK_WIDGET(Tool_bt.button1), 0, 0);
gtk_widget_set_size_request(GTK_WIDGET(Tool_bt.button2), 0, 0);
gtk_widget_set_size_request(GTK_WIDGET(Tool_bt.button3), 0, 0);
*/
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.label1,  0, 1, 0, 1,  GTK_EXPAND|GTK_FILL,
		GTK_EXPAND|GTK_FILL, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.label2,  0, 1, 1, 2,  GTK_EXPAND|GTK_FILL,
		GTK_EXPAND|GTK_FILL, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.label3,  0, 1, 2, 3,  GTK_EXPAND|GTK_FILL,
		GTK_EXPAND|GTK_FILL, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.label4,  0, 1, 3, 4,  GTK_EXPAND|GTK_FILL,
		GTK_EXPAND|GTK_FILL, 0, 0);

	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.button1, 1, 2, 0, 1,  GTK_EXPAND|GTK_FILL,
		GTK_EXPAND|GTK_FILL, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.button2, 1, 2, 1, 2,  GTK_EXPAND|GTK_FILL,
		GTK_EXPAND|GTK_FILL, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.button3, 1, 2, 2, 3,  GTK_EXPAND|GTK_FILL,
		GTK_EXPAND|GTK_FILL, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.button4, 1, 2, 3, 4,  GTK_SHRINK|GTK_EXPAND,
		GTK_EXPAND|GTK_FILL, 0, 0);
/*
0 1 7 8
0 1 9 10
  2 9 10
1 2 9 10
*/
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.button5, 0, 1, 7, 8, GTK_SHRINK|GTK_EXPAND,
		GTK_SHRINK|GTK_EXPAND, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.button6, 0, 1, 9, 10, GTK_SHRINK|GTK_EXPAND,
		GTK_SHRINK|GTK_EXPAND, 0, 0);
	gtk_table_attach(GTK_TABLE(Port_bt.table), Port_bt.button7, 1, 2, 9, 10, GTK_SHRINK|GTK_EXPAND,
		GTK_SHRINK|GTK_EXPAND, 0, 0);

//	gtk_table_attach(GTK_TABLE(Tool_bt.table), Tool_bt.button1,   0, 1, 0, 1, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
//	gtk_table_attach(GTK_TABLE(Tool_bt.table), Tool_bt.button2,   0, 1, 1, 2, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
//	gtk_table_attach(GTK_TABLE(Tool_bt.table), Tool_bt.button3,   0, 1, 2, 3, GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);

	gtk_paned_pack1(GTK_PANED(Port_win.pane), GTK_WIDGET(Port_win.scrolledwin),TRUE, TRUE);
	gtk_paned_pack2(GTK_PANED(Port_win.pane), GTK_WIDGET(Port_bt.table),        TRUE, TRUE);

	//gtk_paned_pack1(GTK_PANED(Tool_win.pane), GTK_WIDGET(Tool_win.scrolledwin),  TRUE, TRUE);
	//gtk_paned_pack2(GTK_PANED(Tool_win.pane), GTK_WIDGET(Tool_bt.table),          TRUE, TRUE);



	gtk_notebook_append_page(GTK_NOTEBOOK(nb_cont.book), GTK_WIDGET(Port_win.pane), GTK_WIDGET(nb_cont.page_label1));
	gtk_notebook_append_page(GTK_NOTEBOOK(nb_cont.book), GTK_WIDGET(Tool_win.scrolledwin),   GTK_WIDGET(nb_cont.page_label2));

	gtk_container_add (GTK_CONTAINER(mw_cont.window), GTK_WIDGET(nb_cont.book));

	gtk_widget_show_all(GTK_WIDGET(mw_cont.window));

/*
void (*pt2_on_destroy)(GtkWidget *widget, gpointer data) = &on_destroy;
void (*pt2_on_comp)(GtkWidget *widget, gpointer data) = &on_comp;
void (*pt2_on_comp_s)(GtkWidget *widget, gpointer data) = &on_comp_s;
void (*pt2_on_recoup)(GtkWidget *widget, gpointer data) = &on_recoup;
void (*pt2_on_file_load)(GtkWidget *widget, gpointer data) = &on_file_load;
void (*pt2_on_file_save)(GtkWidget *widget, gpointer data) = &on_file_save;
void (*pt2_on_font_chng)(GtkWidget *widget, gpointer data) = &on_font_chng;
void (*pt2_on_row_chng)(GtkWidget *widget, gpointer data) = &on_row_chng;
void (*pt2_on_col_chng)(GtkWidget *widget, gpointer data) = &on_col_chng;
void (*pt2_on_calculate)(GtkWidget *widget, gpointer data) = &on_calculate;
*/



	g_signal_connect(GTK_SPIN_BUTTON(Port_bt.button1), "value-changed", G_CALLBACK(on_row_chng),  mw_cont.window);
	g_signal_connect(GTK_SPIN_BUTTON(Port_bt.button2), "value-changed", G_CALLBACK(on_col_chng),  mw_cont.window);
	g_signal_connect(GTK_SPIN_BUTTON(Port_bt.button3), "value-changed", G_CALLBACK(on_font_chng), mw_cont.window);
	g_signal_connect(GTK_SPIN_BUTTON(Port_bt.button4), "value-changed", G_CALLBACK(on_commission_chng), mw_cont.window);

	g_signal_connect(GTK_BUTTON(Port_bt.button5), "button-release-event", G_CALLBACK(on_calculate), mw_cont.window);
	g_signal_connect(GTK_BUTTON(Port_bt.button6), "button-release-event", G_CALLBACK(on_file_load), mw_cont.window);
	g_signal_connect(GTK_BUTTON(Port_bt.button7), "button-release-event", G_CALLBACK(on_file_save), mw_cont.window);

	g_signal_connect(GTK_BUTTON(Tool_win.comp_button), "button-release-event", G_CALLBACK(on_comp), (gpointer)&Tool_win);
	g_signal_connect(GTK_BUTTON(Tool_win.comp_s_button), "button-release-event", G_CALLBACK(on_comp_s), (gpointer)&Tool_win);
	g_signal_connect(GTK_BUTTON(Tool_win.recoup_button), "button-release-event", G_CALLBACK(on_recoup), (gpointer)&Tool_win);
	g_signal_connect(GTK_WINDOW(mw_cont.window), "destroy", G_CALLBACK(on_destroy), NULL);

	gtk_main ();

	return 0;
}
