/*************************************************************************************************************
* This file contains main header file
* 2012/10 by Dlxko
* Version 4.0
**************************************************************************************************************/

// Main includes
#include <stdio.h>
#include <iostream>

// Gtk includes
#include <gtk/gtk.h>
#include <glib.h>

// Winsock include
#include "winsock2.h"

// Main includes
#include "../Languages/English.h"
#include "../Sourcefiles/Config.h"

// System includes
#include <time.h>
#include <tchar.h>
#include <urlmon.h>
#include <curl.h>
#include <sys/stat.h>

// Pragmas
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "Ws2_32.lib")





// SingleBodies Controls functions
extern void singlebody_control_registration_shown();
extern void singlebody_control_loggin_shown();
extern void singlebody_control_buddylist_shown();

// SingleBodies Controls widgets
extern void singlebody_control_widget_vbox();
extern void singlebody_control_widget_loggin_username();
extern void singlebody_control_widget_loggin_password();
extern void singlebody_control_widget_loggin_label();
extern void singlebody_control_widget_registration_entry1();
extern void singlebody_control_widget_registration_entry2();
extern void singlebody_control_widget_registration_entry3();
extern void singlebody_control_widget_registration_entry4();
extern void singlebody_control_widget_registration_entry5();
extern void singlebody_control_widget_registration_entry6();
extern void singlebody_control_widget_registration_entry7();
extern void singlebody_control_widget_registration_entry8();
extern void singlebody_control_widget_registration_image();
extern void singlebody_control_widget_registration_separator();
extern void singlebody_control_widget_registration_label();
extern void singlebody_control_widget_registration_button();
extern void singlebody_control_widget_buddylist_create();

// SingleBodies Chat widgets
extern void singlebody_widget_vbox(int function_buddy_number);
extern void singlebody_widget_chat_toolbar(int function_buddy_number);
extern void singlebody_widget_chat_scroll(int function_buddy_number);
extern void singlebody_widget_chat_textview(int function_buddy_number);
extern void singlebody_widget_chat_entry(int function_buddy_number);
extern void singlebody_widget_info_label(int function_buddy_number);
extern void singlebody_widget_history_entry(int function_buddy_number);
extern void singlebody_widget_history_view(int function_buddy_number);
extern void singlebody_widget_history_label(int function_buddy_number);
extern void singlebody_widget_history_label_spaced(int function_buddy_number);
extern void singlebody_widget_history_hbox(int function_buddy_number);
extern void singlebody_widget_history_day(int function_buddy_number);
extern void singlebody_widget_history_month(int function_buddy_number);
extern void singlebody_widget_history_year(int function_buddy_number);

// SingleBodies Controls handlers
extern bool singlebody_loggin_shown;
extern bool singlebody_registration_shown;
extern bool singlebody_buddylist_shown;

extern GtkToolItem *buddy_addremo;
extern GtkToolItem *buddy_showhide;

// SingleBodies Controls loggin
extern GtkWidget *log_vbox;
extern GtkWidget *log_username;
extern GtkWidget *log_password;
extern GtkWidget *log_label;

// SingleBodies Controls buddylist
extern GtkWidget *buddy_toolbar;
extern GtkWidget *buddy_vbox;
extern GtkWidget *buddy_treeView;
extern GtkWidget *buddy_scroll;
extern GtkToolItem *buddy_events;
extern GtkToolItem *buddy_disturb;
extern GtkToolItem *buddy_addremo;
extern GtkToolItem *buddy_showhide;
extern GtkListStore *buddy_tree_view;
extern GtkTreeSortable  *buddy_sortable_model;
extern GtkCellRenderer *buddy_renderer;
extern GtkTreeViewColumn *buddy_column;
extern GtkTreeSelection *buddy_selection;

// SingleBodies Controls registration
extern GtkWidget *reg_entry1; // Name
extern GtkWidget *reg_entry2; // Surname
extern GtkWidget *reg_entry3; // Nickname
extern GtkWidget *reg_entry4; // Location
extern GtkWidget *reg_entry5; // State
extern GtkWidget *reg_entry6; // Age
extern GtkWidget *reg_entry7; // Gender
extern GtkWidget *reg_entry8; // Password
extern GtkWidget *reg_separator; // Separator
extern GtkWidget *reg_button; // Button
extern GtkWidget *reg_label; // Label
extern GtkWidget *reg_image; // Gravatar

// SingleBodies Chat functions
extern void additional_function_chat_single_bodies_delete_oldies(int function_buddy_number);
extern void singlebody_chat_history_shown(GtkWidget *unused_widget, int function_buddy_number);
extern void singlebody_chat_conversation_info_shown(GtkWidget *unused_widget, int function_buddy_number, int function_mode);
extern void singlebody_chat_signal_conversation_info_shown(GtkWidget *unused_widget, int function_buddy_number);
extern void singlebody_chat_history_view_shown(int function_buddy_number);

// Request window
extern GtkWidget *requests_window;
extern GtkWidget *requests_vbox;

// Adder window
extern GtkWidget *new_friend_window;
extern GtkWidget *new_friend_label;


/*********************************************************************************************************************************/


// Loggin
extern void signal_send_loggin();

// Dialogs
extern void function_show_error(char input_message[DIALOG_BUFFER_SIZE]);
extern void function_show_warning(char input_message[DIALOG_BUFFER_SIZE]);
extern void function_show_warning_ftp(char input_message[DIALOG_BUFFER_SIZE], int fx_type);
extern void create_dialog_window();

// Functions
extern void function_midcopy ( char* input, char* output, int start_pos, int stop_pos);
extern int function_convert_packet_lenght(char function_char_lenght[LENGHT_SIZE+1]);
extern int function_get_buddy_number(char nick_name[10]);
extern void function_send_data (char *buffer, int size );

// Time
extern std::string function_get_std_time();

// SHA-512
extern char *function_crypt_sha512(const gchar *normal_password);

//Connect
extern SOCKET dxconnect();

// History
extern void signal_search_history(GtkWidget *unused_button, gint input_buddy_number);
extern void signal_request_history_data(GtkWidget *unused_button, gint input_buddy_number);

// Infos
extern void function_info_request(GtkWidget *unused_button, gint input_buddy_number);
extern void function_show_info_window();
extern void create_info_window(int function_buddy_number);

// Buddyactions
extern int function_make_disturb(char nick_name[DATA_ONE_SIZE+1]);
extern int function_make_away(char nick_name[DATA_ONE_SIZE+1]);
extern bool function_make_offline(char nick_name[DATA_ONE_SIZE+1]);
extern int function_make_online(char nick_name[DATA_ONE_SIZE+1]);
extern void function_buddy_hide();

// Adder
extern void signal_create_buddy_adder();

// Disturb
extern void function_dont_disturb();

// Requests
extern void crate_requests();
extern void signal_show_requests();
extern void signal_requests_ok(GtkWidget *button, gint event_id);
extern void signal_requests_shut(GtkWidget *button, gint event_id);

// BuddyList
extern gint function_compare_rows (GtkTreeModel *input_treemodel, GtkTreeIter  *input_first_iter, GtkTreeIter  *input_second_iter, gpointer userdata);
extern void signal_list_row_activated(GtkTreeView *input_treeview,  GtkTreePath *input_tree_path, GtkTreeViewColumn *input_colum, gpointer userdata);

// Channel
extern bool create_channel();

// Logoff
extern void signal_close_chat(GtkWidget *unused_widget, int function_buddy_number);
extern void signal_go_offline();
extern void signal_close_requests();
extern void function_exit_client(int code);
extern void signal_close_conversation();

// Pixmaps
extern bool create_pixmaps();
extern void function_get_smile(GtkTextBuffer *buffer);

// Registration
extern void signal_send_registration();

// Sendaction
extern void signal_send_message(GtkWidget *entry, gint buddy_numb);

// Statusbar
extern void function_update_statusbar(GtkTextBuffer *input_textview_buffer, GtkStatusbar  *input_statusbar_widget);

// FTPUpload
extern void signal_ftp_upload(GtkWidget *unused_button, gint input_buddy_number);

//FTPDownload
extern void function_ftp_download(char function_remote_addr[FILE_PATH_SIZE], char function_file_name[FILE_NAME_SIZE], int function_buddy_number, GtkTextIter function_end_iter, char function_msg_number[MSG_NUMB_SIZE]);

// FxPacket
extern void function_process_packet(char fx_data_packet[BUF_SIZE], int fx_packet_size);


/*********************************************************************************************************************************/


//Extern socket variables
extern SOCKET s;

// Pixbuf variables
extern GdkPixbuf *px_away;
extern GdkPixbuf *px_disturb;
extern GdkPixbuf *px_online;
extern GdkPixbuf *px_offline;
extern GdkPixbuf *px_received;
extern GdkPixbuf *px_sending;
extern GdkPixbuf *px_send;
extern GdkPixbuf *px_failed;
extern GdkPixbuf *px_receiving;

// Default window icon
extern GdkPixbuf *default_icon;

// Default avatar image
extern GdkPixbuf *default_buddy_pixbuf;

// Saved my name
extern char my_name[DATA_ONE_SIZE+1];

// Sending text buffers
extern char sending_text[BUF_SIZE];
extern int  sending_text_len;
extern char sending_text_len_char[LENGHT_SIZE+1];

// Entry text
extern const gchar *entry_text;

// Dialog text
extern char dialog_text[DIALOG_BUFFER_SIZE];

// Main
extern bool conversation_shown;
extern int conversation_tabs;
extern int current_buddy;
extern int activated_events;
extern bool active_ftp_upload;
extern bool active_ftp_download;
extern GtkWidget *conversation_window;
extern GtkWidget *conversation_notebox;
extern GtkWidget *conversation_notebook;
extern GtkWidget *log_window;


/*********************************************************************************************************************************/


// Buddy structure
struct _buddy
{
	// Buddylist handlers
	bool online;
	bool listed;
	bool deleted;

	// Chat replies handler
	int who_sender;

	// Notebook page number
	int notebook_position;

	// Nickname store
	char nick_name[DATA_ONE_SIZE+1];

	// Chat interface widgets
	GtkWidget *text_view; // DYNAMIC
	GtkWidget *scroll; // DYNAMIC
	GtkWidget *entry; // DYNAMIC
	GtkWidget *vbox; // DYNAMIC
	GtkWidget *toolbar; // DYNAMIC
	GtkWidget *history_hbox;  // DYNAMIC
	GtkWidget *history_day;  // DYNAMIC
	GtkWidget *history_month;  // DYNAMIC
	GtkWidget *history_year; // DYNAMIC
	GtkWidget *history_entry; // DYNAMIC
	GtkWidget *history_view; // DYNAMIC
	GtkWidget *history_label; // DYNAMIC
	GtkWidget *history_label_spaced; // DYNAMIC
	GtkWidget *info_label; // DYNAMIC

	// Chat interface extras
	GtkTextBuffer *buffer;
	GtkTreeIter *tree_iter;
	GtkTextMark *endmark;
	GtkTextIter start_iter;
	GtkTextIter beg_end_iter;
	GtkTextBuffer *history_buffer;

	// Chat menu toolitems
	GtkToolItem   *menu_conversation;
	GtkToolItem   *menu_transfer;
	GtkToolItem   *menu_close;
	GtkToolItem   *menu_buddy_history;

	// Stores for history enries
	const gchar *day_text;
	const gchar *month_text;
	const gchar *year_text;

	// SingleBodies handlers
	bool singlebody_history_shown;
	bool singlebody_history_view_shown;
	bool singlebody_conversation_shown;
	bool singlebody_info_shown;

	// Other handlers
	bool start_iter_loaded;
	bool request_info_status;
	bool apended;	
	bool chat_buffer_created;
	bool history_buffer_created;
};

// Message structure
struct _messages
{
	int chat;
	bool waiting;
	gint char_count;
};

// Event requests structure
struct _req_events
{
	char       id[REQUESTS_ID_CHAR_SIZE];
	int        id_integer;
	bool       active;
	char       nick_name[DATA_ONE_SIZE+1];
	GtkWidget  *vbox;
	GtkWidget  *hbox;
	GtkWidget  *label;
	GtkWidget  *ok_button;
	GtkWidget  *shut_button;
};

// Threeshort enum
enum
{
	STATUS_COLUM,
	NICK_COLUM,
	AVATAR_COLUM,
	STATUS_H_COLUM
};


/*********************************************************************************************************************************/


// Structure variables
extern _buddy		buddy[MAX_BUDDIES];
extern _messages	messages[MAX_MESSAGES];
extern _req_events  req_events[MAX_REQUESTS];