#include <gtk/gtk.h>
#include <stdio.h>
#include <iostream>
#include "winsock2.h"
#include "Config.h"
#include <tchar.h>
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "Ws2_32.lib")

/******************************** GTK Widget external definitions ***********************************/

//Registration window
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;

// Loggin window
extern GtkWidget *log_window; // Loggin window
extern GtkWidget *log_vbox; // Login vbox
extern GtkWidget *log_entry; // Nickname
extern GtkWidget *log_entry2; // Password
extern GtkWidget *log_label; // Loggin label

// Buddy window
extern GtkWidget *buddy_window;
extern GtkCellRenderer *buddy_renderer;
extern GtkTreeSortable  *buddy_sortable_model;
extern GtkWidget *buddy_treeView;
extern GtkListStore *buddy_tree_view;
extern GtkTreeViewColumn *buddy_column;
extern GtkWidget *buddy_vbox;
extern GtkTreeSelection *buddy_selection;
extern GtkWidget *buddy_add;
extern GtkWidget *buddy_hide;
extern GtkWidget *buddy_requests;
extern GtkWidget *buddy_footer;
extern GtkWidget *buddy_hfooter;

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

// Buddy functions
extern GdkPixbuf *buddy_pixbuf;
extern GtkTreeIter buddy_iter;

// Add friend widgets
extern GtkWidget *new_friend_window;
extern GtkWidget *new_friend_vbox;
extern GtkWidget *new_friend_username;
extern GtkWidget *new_friend_label;

// Dialog window
extern GtkWidget *dialog_window;

/******************************** Project functions declarations ***********************************/

// Dialogs.h
extern void function_show_error(char msg[100]);
extern void function_show_warning(char msg[100]);
extern void create_dialog_window();

// Functions.h
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.h
extern std::string function_get_std_time();

//Connect.h
extern SOCKET dxconnect();

// Buddyactions.h
extern bool function_make_offline(char nick_name[10]);
extern int function_make_online(char nick_name[10]);
extern void function_create_chat(gint buddy_number);
extern void function_buddy_hide();
extern void signal_create_buddy_adder();
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.h
extern bool create_buddylist();
extern bool crate_requests();

// Channel.h
extern gboolean function_receive_socket( GIOChannel   *channel, GIOCondition  cond);
extern bool create_channel();

// Chat.h
extern bool create_buddy_chat(int buddy_numb);

// Loggining.h
extern void signal_send_loggin();
extern bool create_loggin();

// Logoff.h
extern void signal_close_chat(GtkWidget *chat);
extern void signal_go_offline();
extern void signal_close_requests();
extern void function_exit_client(int code);

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

// Registration.h
extern void signal_send_registration();
extern void create_registration();

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

// Statusbar.h
extern void function_update_statusbar(GtkTextBuffer *buffer, GtkStatusbar  *statusbar);

// FTPUpload.h
extern bool ftp_upload_action(GtkWidget *button, gint buddy_numb);

//FTPDownload.h
extern bool ftp_download_action(char function_remote_addr[FILE_PATH_SIZE], char function_file_name[FILE_NAME_SIZE], int function_buddy_number, GtkTextIter function_end_iter);

// FxPacket.h
extern bool function_process_packet(char fx_cleaned_packet[BUF_SIZE], int fx_packet_size);

// Main.h
extern int current_buddy;
extern int activated_events;
extern char dialog_text[100];

/******************************** Extern variables definitions ***********************************/

//Extern socket variables
extern SOCKET s; // Socket
extern GIOChannel *gich; // Channel variable
extern guint event_id; // Event id

// Pixbuf variables
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;

/******************************** Project structures definitions ***********************************/

// Buddy structure
struct _buddy
{
	bool          online; // Is buddy online ?
	bool          listed; // Is buddy created in buddy_list?
	int           who_sender; // TRUE me FALSE you
	bool          buddy_chat; // Is chat created ?
	GtkTreeIter   *tree_iter; // Iter of row in buddy_list
	char		  nick_name[10]; // Buddy nickname
	GtkWidget     *file_button; // Button in buddy list
	GtkWidget     *window; // Buddy window for chat
	GtkWidget     *scroll; // Buddy window for chat
	GtkWidget     *vbox; // Vbox for chat
	GtkWidget     *hbox; // Vbox for chat
	GtkWidget     *text_view; // Text view for chat
	GtkTextBuffer *buffer; // Buffer for chat
	GtkWidget     *entry; // Entry text for chat
	GtkWidget     *statusbar; // Status bar for chat
	gint          tmp_pos;
};

// Message structure
struct _messages
{
	int chat; // Number of msg
	bool waiting; // Is free ?
	GtkTextIter msg_iter; // End iter
};

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

enum
{
	PIXBUF_COL,
	TEXT_COL,
	TEXT_COL2
};

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

/******************************** Handling variables ***********************************/

extern char my_name[11];
extern bool hidden_offlines;