/*************************************************************************************************************
* This file contains server main header
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/

// Global includes
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include "winsock.h"
#include <gtk/gtk.h>
#include <mysql.h>

// My includes
#include "Config.h"

// Pragma
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib,"libmysql.lib")

// Std defines
using std::cout;
using std::endl;
using std::string;

//Server window
extern GtkWidget *server_view;
extern GtkTextBuffer *server_buffer;
extern GtkWidget *server_definfo;
extern GtkWidget *progress1;
extern GtkWidget *server_datalabel;
extern GtkWidget *server_datalabel2;

//Client structure
struct _client
{
	int         return_value;
	bool		connected;
	SOCKET		socket;
	int			client_acess;
	char		nick_name[DATA_ONE_SIZE];

	GIOChannel   *client_gich;
	guint        client_event_id;
};

// Client structure define
extern _client client[MAX_CLIENTS];

// Dialogs
extern void function_show_warning(char msg[WARNING_TMP_SIZE]);
extern void function_show_error(char msg[WARNING_TMP_SIZE]);
extern void function_show_ready(char msg[WARNING_TMP_SIZE]);
extern void function_showgtk_fatal(char msg[WARNING_TMP_SIZE]);
extern void function_showgtk_info(char msg[WARNING_TMP_SIZE]);
extern void function_show_info(char msg[WARNING_TMP_SIZE]);
extern void create_dialog_window();

// Mysql
extern MYSQL_RES *function_mysql_result(char * argument);
extern int function_mysql_returned(char * argument);

// StopStartServer
extern SOCKET server_socket;
extern void function_start_server();
extern void signal_end_server();

//ServerSocket
extern bool create_server_channel();

// AcpDisClient
extern void signal_accept_client();
extern void function_disconnect_client ( _client *current_client );

// RecvData
extern gboolean signal_receive_data(GIOChannel *source,  GIOCondition condition, int client_socket);

//SendData
extern void function_send_data (_client *current_client, char *buffer, int size);

//Offline messages
extern void function_send_offline_messages(_client *current_client);

// Chck friendstate
extern bool function_check_friendstate(_client *counter_client, _client *current_client);

//Chck client
extern bool function_check_client(_client *current_client);

//Buddieupdate
extern void function_budies_update(int buddy);
extern void function_buddy_status_update(int function_buddy_id, int function_type);

// Broad buddies
extern void function_broadcast_buddies ( char *message, _client *current_client, int size);

//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 gboolean function_junk_broadcast(gpointer data);

//Server window
extern void create_server_window();

// Pixmaps
extern bool create_pixmaps();
extern GdkPixbuf *default_icon;

// Main
extern int clients_connected;
extern int bytes_sent;
extern int bytes_received;
extern float mbytes_sent;
extern float mbytes_received;

// ServerInfo
extern void create_serverinfo();

// ProgressClients
extern void function_update_progress_clients();

// History
extern void function_send_history_messages(_client *current_client, char friend_name[DATA_ONE_SIZE], char day[2], char month[2], char year[4]);

// Extern variables
extern char warnings_tmp[WARNING_TMP_SIZE];
extern char buffer_text_len_char[LENGHT_SIZE];
extern int buffer_text_len;
extern char buffer_text[BUFFER_SIZE];
extern char mysql_text[MYSQL_TEXT_SIZE];
extern int mysql_return_value;