#include "addressbook.h"
#include <gdk/gdkkeysyms.h>
#include "dialog.h"
#include "message.h"


/******** Global variable declarations ********/
static GtkWidget *toplevel;
static GtkWidget *layout;
static GtkWidget *vbox;
GdkColormap *defcmap;
GtkStyle *defstyle, *cstyle;
GdkColor black, white;
GdkColor addressbook_grey1 = {0, 0xcfff, 0xcfff, 0xcfff};
GdkColor addressbook_grey2 = {0, 0xefff, 0xefff, 0xefff};
gint defsize;


/******** External function declarations ********/

void move_to_address(gint row, gboolean make_top);
#if (LOOKUP_FILE == 1)
gint lookup_from_file(const gchar *s, gchar *key, gint start_index);
#else
gint lookup_from_list(gchar *s, gchar *key);
#endif
void make_window_header(GtkWidget *vbox);
void make_window_list(GtkWidget *vbox);
void make_event_details();
void make_calendar();
void close_addressbook(GtkWidget *widget, gpointer gdata);
void open_edit_window(gint row, gchar *key);
void show_message(gchar *msg);
gint read_record_details(gchar *ks, addr_info *aip);
gint delete_record_details(addr_info *aip);



/******** Internal function declarations ********/

static void get_system_defaults();
gboolean addressbook_configure_event_callback( GtkWidget *widget, GdkEventConfigure *e, gpointer data );



/******** External function definitions ********/

void addressbook_get_grey_colors(GdkColor *darkgrey, GdkColor *lightgrey)
{
   *darkgrey = addressbook_grey1;
   *lightgrey = addressbook_grey2;
}



GtkWidget *addressbook_get_toplevel_widget()
{
   return toplevel;
}



GtkWidget *addressbook_get_layout_widget()
{
   return layout;
}



void addressbook_show_main_window(gint flg)
{
   if(flg)
   {
      gtk_widget_map(vbox);
   }
   else
   {
      gtk_widget_unmap(vbox);
   }
}



void addressbook_set_gc_black_white(GdkGC *fg, GdkGC *bg)
{
   gdk_gc_set_foreground(fg, &black);
   gdk_gc_set_background(bg, &white);
}



void close_addressbook(GtkWidget *widget, gpointer gdata)
{
   gtk_main_quit();
}



int open_addressbook(int argc, char **argv)
{
   get_system_defaults();

   toplevel = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_container_border_width(GTK_CONTAINER(toplevel), 0);
   gtk_signal_connect(GTK_OBJECT(toplevel), "destroy", \
                      GTK_SIGNAL_FUNC(close_addressbook), NULL);

   gtk_signal_connect(GTK_OBJECT(toplevel), "configure_event", \
                      GTK_SIGNAL_FUNC( addressbook_configure_event_callback), NULL);

   gtk_window_set_title(GTK_WINDOW(toplevel), TOPLEVEL_NM);
   gtk_window_set_policy(GTK_WINDOW(toplevel), TRUE, TRUE, FALSE);
   gtk_widget_set_uposition(GTK_WIDGET(toplevel), TOPLEVEL_X, TOPLEVEL_Y);
   gtk_widget_set_usize(GTK_WIDGET(toplevel), TOPLEVEL_WD, TOPLEVEL_HT);

   /* Need to make the application visible as soon as possible *
    * in order to use dialog popup for error reporting.        */
   gtk_widget_show(toplevel);

   /* Create a fixed box an the main container */
   layout = gtk_fixed_new();
   gtk_widget_show(layout);
   gtk_container_add(GTK_CONTAINER(toplevel), layout);

   /* Create a vertical box for layout control */
   vbox = gtk_vbox_new(FALSE, ALL_SPACE);
   gtk_widget_show(vbox);
   gtk_fixed_put(GTK_FIXED(layout), vbox, 0, 0);

   make_window_header(vbox);

   make_window_list(vbox);

   return 0;
}



/******** Internal function definitions ********/

static void get_system_defaults()
{
   /* Get default colormap and depth */
   defcmap = gdk_colormap_get_system();
   defsize = gdk_colormap_get_system_size();

   /* Get black and white color values */
   gdk_color_black(defcmap, &black);
   gdk_color_white(defcmap, &white);

   /* Allocate colors */
   gdk_color_alloc(defcmap, &addressbook_grey1);
   gdk_color_alloc(defcmap, &addressbook_grey2);

   /* Get default style and make a copy */
   defstyle = gtk_widget_get_default_style();
   cstyle = gtk_style_copy(defstyle);
}



void add_new_record(GtkWidget *widget, gpointer gdata)
{
   open_edit_window(-1, NULL);
}


gboolean addressbook_FindFunction (const gchar *find_string, gpointer data)
{
	gchar key[100];
	gint i = -1;
	gint *start_index = data;

    if(*find_string)
    {
#if (LOOKUP_FILE == 1)
       if((i = lookup_from_file(find_string, key, *start_index)) >= 0)
#else
       if((i = lookup_from_list(find_string, key, *start_index)) >= 0)
#endif
       {
//          open_edit_window(i, key);
          move_to_address(i, FALSE);
		  *start_index = i + 1;
       }
       else
	   {
          MessageBox("Not found");
		  *start_index = 0;
	   }
    }

	return TRUE; // Return FALSE to dismiss the dialog, so we
	//return i == -1; // Return FALSE to dismiss the dialog, so we
					// want to return TRUE on failure here.
}


void lookup_record(GtkWidget *widget, GdkEventKey *event)
{
   static gint start_index = 0;

   FindStringDialog ("Find text:", addressbook_FindFunction, &start_index);
}


void delete_record(gchar *key)
{
   addr_info info;
   
   if(read_record_details(key, &info))
	  delete_record_details(&info);
}


void edit_record(gint row, gchar *key)
{
   open_edit_window(row, key);
}



gboolean
addressbook_configure_event_callback( GtkWidget *widget, GdkEventConfigure *e, gpointer data ) {
   gtk_widget_set_usize( vbox, e->width, e->height );
   return FALSE;
}
