#ifdef FR_COMBAT_INFO
#include <ctype.h>
#include "client_serv.h"
#include "global.h"
#include "stats.h"
#include "multiplayer.h"
#include "errors.h"
#include "actors.h"
#include "text.h"
#include "counters.h"
#include "client_serv.h"
#include "asc.h"
#include "themes.h"
#include "interface.h"

int console_coup_critiques=1;
int console_chance_critique=0;
int console_degat_feu=1;
int console_degat_froid=1;
int console_degat_magie=1;
int console_degat_lumiere=1;
int console_degat_poison=0;
int flottant_coup_critiques=1;
int flottant_chance_critique=0;
int flottant_degat_feu=0;
int flottant_degat_froid=0;
int flottant_degat_magie=0;
int flottant_degat_lumiere=0;
int flottant_degat_poison=0;

couleurs_rvb couleur_degat_feu = {1.0, 0.1, 0.1};
couleurs_rvb couleur_degat_froid = {0.2, 0.2, 0.8};
couleurs_rvb couleur_degat_magie = {0.9, 0.7, 0.3};
couleurs_rvb couleur_degat_lumiere = {0.95, 0.33, 1.f};
couleurs_rvb couleur_degat_poison = {0.0, 0.7, 0.0};

#define LOG_INFO_COMBAT(color, buff) put_colored_text_in_buffer(color, CHAT_COMBAT, (const Uint8*)buff, -1)
#define ADD_FLOATING_MSG(act, buff, rgb) add_floating_message(act, buff, FLOATINGMESSAGE_NORTH, rgb.rouge, rgb.vert, rgb.bleu, 1500)

int info_combat_console=0;
int info_combat_float_msg=0;

//@TRINITA 2010 supp des espaces
char *trim (char * );

void combat_info_type(Uint16 actor_id, Uint8 type, Sint32 data)
{
	char buff[128] = {0};
	actor *act = get_actor_ptr_from_id(actor_id);
	char *actor_name;

	if(act == NULL)
	{
		log_error("INFO_COMBAT : %d is invalid actor_id!", actor_id);
		return;
	}
   	actor_name = strip_actor_name(act->actor_name);
   	actor_name = trim(actor_name);

	switch(type)
	{
		case COUP_CRITIQUE:
			if(info_combat_float_msg && flottant_coup_critiques)
			{
				snprintf(buff, sizeof(buff)-1, "Crit:%u", data);
				add_floating_message(actor_id, buff, FLOATINGMESSAGE_NORTH, 0.7, 0.0, 0.0, 1500);
			}
			if(info_combat_console && console_coup_critiques)
			{
				snprintf(buff, sizeof(buff)-1, "%s reçoit un coup critique de %u dégâts.", actor_name, (Uint8)data);
				LOG_INFO_COMBAT(c_red1, buff);
			}

			if(actor_id == yourself)
			{
				increment_combat_info_counter("Critiques reçus", 1);
				set_max_combat_counter("Max critique reçu", data);
			}
		break;
		case CHANCE_CRITIQUE:
			if(actor_id == yourself)
			{
				increment_combat_info_counter("Chance critique", 1);
			}
			if(info_combat_float_msg && flottant_chance_critique)
			{
				snprintf(buff, sizeof(buff)-1, "Touche!");
				add_floating_message(actor_id, buff, FLOATINGMESSAGE_NORTH, 0.0, 0.3, 0.7, 1500);
			}
			if(info_combat_console && console_chance_critique)
			{
				snprintf(buff, sizeof(buff)-1, "%s ignore la défense de son adversaire!", actor_name);
				LOG_INFO_COMBAT(c_red3, buff);
			}
		break;
		case CHANCE_ESQUIVE:
		/*	if(info_combat_float_msg)
			{
				snprintf(buff, sizeof(buff)-1, "Esq!");
				add_floating_message(actor_id, buff, FLOATINGMESSAGE_NORTH, 0.7, 0.7, 0.0, 1500);
			}
			if(info_combat_console)
			{
				snprintf(buff, sizeof(buff)-1, "%s esquive le coup!", act->actor_name);
				LOG_TO_CONSOLE(c_green1, buff);
			}
		*/
		break;
		case BOUCLIER_PERSEE:
			/*if(info_combat_console)
			{
				snprintf(buff, sizeof(buff)-1, "%s est victime du bouclier de persé!", actor_name);
				LOG_INFO_COMBAT(c_red1, buff);
			}*/
		break;
		case DEFENSE_MALUS:
		break;
		case DEGAT_FROID:
			if(actor_id == yourself)
			{
				set_max_combat_counter("Max dégât froid reçu", data);
			}
			if(info_combat_console && console_degat_froid)
			{
				snprintf(buff, sizeof(buff)-1, "%s reçoit %d dégâts de froid.", actor_name, data);
				LOG_INFO_COMBAT(c_blue1, buff);
			}
			if(info_combat_float_msg && flottant_degat_froid)
			{
				snprintf(buff, sizeof(buff)-1, "Froid:%d", data);
				ADD_FLOATING_MSG(actor_id, buff, couleur_degat_froid);
			}
		break;
		case DEGAT_CHAUD:
			if(actor_id == yourself)
			{
				set_max_combat_counter("Max dégât chaud reçu", data);
			}
			if(info_combat_console && console_degat_feu)
			{
				snprintf(buff, sizeof(buff)-1, "%s reçoit %d dégâts de chaleur.", actor_name, data);
				LOG_INFO_COMBAT(c_red2, buff);
			}
			if(info_combat_float_msg && flottant_degat_feu)
			{
				snprintf(buff, sizeof(buff)-1, "Feu:%d", data);
				ADD_FLOATING_MSG(actor_id, buff, couleur_degat_feu);
			}
		break;
		case DEGAT_MAGIE:
			if(actor_id == yourself)
			{
				set_max_combat_counter("Max dégât magie reçu", data);
			}
			if(info_combat_console && console_degat_magie)
			{
				snprintf(buff, sizeof(buff)-1, "%s reçoit %d dégâts de magie.", actor_name, data);
				LOG_INFO_COMBAT(c_yellow1, buff);
			}
			if(info_combat_float_msg && flottant_degat_magie)
			{
				snprintf(buff, sizeof(buff)-1, "Magie:%d", data);
				ADD_FLOATING_MSG(actor_id, buff, couleur_degat_magie);
			}
		break;
		case DEGAT_LUMIERE:
			if(actor_id == yourself)
			{
				set_max_combat_counter("Max dégât lumière reçu", data);
			}
			if(info_combat_console && console_degat_lumiere)
			{
				snprintf(buff, sizeof(buff)-1, "%s reçoit %d dégâts de lumière.", actor_name, data);
				LOG_INFO_COMBAT(c_purple1, buff);
			}
			if(info_combat_float_msg && flottant_degat_lumiere)
			{
				snprintf(buff, sizeof(buff)-1, "Lumi:%d", data);
				ADD_FLOATING_MSG(actor_id, buff, couleur_degat_lumiere);
			}
		break;
		case DEGAT_POISON:
		if(actor_id == yourself)
			{
				increment_combat_info_counter("Doses poison reçus en combat", data);
				set_max_combat_counter("Dose de poison maximal reçue", data);
			}
			if(info_combat_console && console_degat_poison)
			{
				snprintf(buff, sizeof(buff)-1, "%s reçoit %d doses de poison.", actor_name, data);
				LOG_INFO_COMBAT(c_green1, buff);
			}
			if(info_combat_float_msg && flottant_degat_poison)
			{
				snprintf(buff, sizeof(buff)-1, "Poison:%d", data);
				ADD_FLOATING_MSG(actor_id, buff, couleur_degat_poison);
			}
		break;
		default:
		log_error("[PROTO] INFO_COMBAT : malformed packet! (unknown type : %u)", type);
	}
}

// http://www.developpez.net/forums/d8335/c-cpp/c/equivalent-fonction-trim/
char *trim (char *str)
{
      char *ibuf, *obuf;

      if (str)
      {
            for (ibuf = obuf = str; *ibuf; )
            {
                  while (*ibuf && (isspace (*ibuf)))
                        ibuf++;
                  if (*ibuf && (obuf != str))
                        *(obuf++) = ' ';
                  while (*ibuf && (!isspace (*ibuf)))
                        *(obuf++) = *(ibuf++);
            }
            *obuf = '\0';
      }
      return (str);
}

#endif //FR_COMBAT_INFO
