
#include "string.h"
#include "fonts.h"

#include <string>
#include <deque>



int compare_string(const char *string1, const char *string2) {
  int size1, size2, b, ind;
  size1 = strlen(string1);
  size2 = strlen(string2);
  b = 1;
  if ((size1 == size2) && (b==1)) {
    ind = 0;
    while ((ind != size1)) {
     if ((string1[ind] != 0) && (string2[ind] != 0)) {
      if (string1[ind] != string2[ind]) {
        b=0;
      }
     }
      ++ind;
    }
  }
  else {
    b=0;
  }
  return b;
}

std::deque<std::string> turn_to_paragraph(GameFont *f, std::string msg, int w) {
  int size, indexw, indexp, ind, ind2, strsize, sizew, n;
  char str[5012];
  strcpy(str, msg.c_str());
  std::string words[1024];
  std::string word;
  std::string line;
  std::deque<std::string> v;
  bool is_jumper;
  is_jumper=false;
  ind=0;
  while (ind != msg.size()) {
    if (msg[ind] == '^') is_jumper = true;
    ind += 1;
  }
  n = f->Width(msg);
  if ((n < w) && (!is_jumper)) {
    v.push_back(msg);
    return v;
  }
  size=indexw=indexp=0;
  strsize=strlen(str);
  word = "";
  ind=ind2=0;
  while (ind != msg.size()) {
    if (str[ind] == ' ') {
      word += str[ind];
      ind2=0;
      words[indexw] = word;
      word = "";
      indexw += 1;
    }
    else {
      word += str[ind];
      ind2+=1;
    }
    ++ind;
  }
  words[indexw] = word;
  indexw += 1;
  sizew=0;
  ind=0;
  line = "";
  while (ind != indexw) {
    n = f->Width(words[ind]);
    if ((sizew + n < w) && (words[ind] != "^ ")) {
      line += words[ind];
      sizew += n;
    }
    else {
      sizew = 0;
      indexp += 1;
      v.push_back(line);
      line = "";
      if (words[ind] != "^ ") line += words[ind];
      sizew += n;
    }
    ++ind;
  }
  v.push_back(line);
  return v;
}




void turn_lowspaces_to_spaces(char *str) {
  int ind, size;
  size = strlen(str);
  ind=0;
  while (ind != size) {
    if (str[ind] == '_')  {
      str[ind] = ' ';
    }
    ++ind;
  }
}

int string_includes(char *str, char *obj) {
  int size1, size2, ind, ind2, b;
  size1=strlen(str);
  size2=strlen(obj);
  ind=0;
  ind2=0;
  b=0;
  while (ind != size1) {
    if (str[ind] == obj[ind2]) {
      ++ind2;
      if (ind2 == size2) {
        b=1;
        break;
      }
    }
    ++ind;
  }
  return b;
}



int c_char_to_i(char c) {
  int b;
  if (c == '0') b = 0;
  if (c == '1') b = 1;
  if (c == '2') b = 2;
  if (c == '3') b = 3;
  if (c == '4') b = 4;
  if (c == '5') b = 5;
  if (c == '6') b = 6;
  if (c == '7') b = 7;
  if (c == '8') b = 8;
  if (c == '9') b = 9;
  return b;
}

char c_i_to_char(int c) {
  char b;
  if (c == 0) b = '0';
  if (c == 1) b = '1';
  if (c == 2) b = '2';
  if (c == 3) b = '3';
  if (c == 4) b = '4';
  if (c == 5) b = '5';
  if (c == 6) b = '6';
  if (c == 7) b = '7';
  if (c == 8) b = '8';
  if (c == 9) b = '9';
  return b;
}

int integer_size(int n) {
  int s;
  s = 1;
  if (n >= 10) s+= 1;
  if (n >= 100) s+= 1;
  if (n >= 1000) s+= 1;
  if (n >= 10000) s+= 1;
  if (n >= 100000) s+= 1;
  if (n >= 1000000) s+= 1;
  if (n >= 10000000) s+= 1;
  return s;
}

char *c_i_to_str(int c, int size) {
  char *b; int ind, temp, power;
  static char size1[] = "d";
  static char size2[] = "dd";
  static char size3[] = "ddd";
  static char size4[] = "dddd";
  static char size5[] = "ddddd";
  char *str;
  power = 1;
  ind = 0;
  if (size == 1) str = size1;
  if (size == 2) str = size2;
  if (size == 3) str = size3;
  if (size == 4) str = size4;
  if (size == 5) str = size5;
  b = (char *) malloc(sizeof(char) * strlen(str));
  strcpy(b, str);
  if (size == 1) {
    b[0] = c_i_to_char(c);
  }
  if (size == 2) {
    b[0] = c_i_to_char(c / 10);
    b[1] = c_i_to_char(c % 10);
  }
  if (size == 3) {
    b[0] = c_i_to_char(c / 100);
    b[1] = c_i_to_char((c % 100) / 10);
    b[2] = c_i_to_char(((c % 100) % 10));
  }
  if (size == 4) {
    b[0] = c_i_to_char(c / 1000);
    b[1] = c_i_to_char((c % 1000) / 100);
    b[2] = c_i_to_char(((c % 1000) % 100) / 10);
    b[3] = c_i_to_char(((c % 1000) % 100) % 10);
  }
  if (size == 5) {
    b[0] = c_i_to_char(c / 10000);
    b[1] = c_i_to_char((c % 10000) / 1000);
    b[2] = c_i_to_char(((c % 10000) % 1000) / 100);
    b[3] = c_i_to_char((((c % 10000) % 1000) % 100) / 10);
    b[4] = c_i_to_char((((c % 10000) % 1000) % 100) % 10);
  }
  return b;
}


bool compare_wildcard(std::string str1, std::string str2) {
  int ind;
  for (ind=0;ind!=str1.size();ind+=1) {
    if (str1[ind] != str2[ind]) return false;
  }
  return true;
}



const ALLEGRO_COLOR CHAR_COLOR_A=al_map_rgb(0, 0, 0);
const ALLEGRO_COLOR CHAR_COLOR_B=al_map_rgb(255, 255, 255);
const ALLEGRO_COLOR CHAR_COLOR_C=al_map_rgb(255, 0, 0);
const ALLEGRO_COLOR CHAR_COLOR_D=al_map_rgb(0, 255, 0);
const ALLEGRO_COLOR CHAR_COLOR_E=al_map_rgb(0, 0, 255);
const ALLEGRO_COLOR CHAR_COLOR_F=al_map_rgb(75, 75, 75);
const ALLEGRO_COLOR CHAR_COLOR_G=al_map_rgb(200, 200, 200);
const ALLEGRO_COLOR CHAR_COLOR_H=al_map_rgb(255, 255, 0);
const ALLEGRO_COLOR CHAR_COLOR_I=al_map_rgb(255, 0, 255);
const ALLEGRO_COLOR CHAR_COLOR_J=al_map_rgb(0, 255, 255);
const ALLEGRO_COLOR CHAR_COLOR_K=al_map_rgb(0, 125, 125);
const ALLEGRO_COLOR CHAR_COLOR_L=al_map_rgb(125, 125, 0);
const ALLEGRO_COLOR CHAR_COLOR_M=al_map_rgb(255, 225, 200);
const ALLEGRO_COLOR CHAR_COLOR_N=al_map_rgb(25, 75, 150);



ALLEGRO_COLOR char_to_color(char c) {
  if (c == 'A') return CHAR_COLOR_A;
  if (c == 'B') return CHAR_COLOR_B;
  if (c == 'C') return CHAR_COLOR_C;
  if (c == 'D') return CHAR_COLOR_D;
  if (c == 'E') return CHAR_COLOR_E;
  if (c == 'F') return CHAR_COLOR_F;
  if (c == 'G') return CHAR_COLOR_G;
  if (c == 'H') return CHAR_COLOR_H;
  if (c == 'I') return CHAR_COLOR_I;
  if (c == 'J') return CHAR_COLOR_J;
  if (c == 'K') return CHAR_COLOR_K;
  if (c == 'L') return CHAR_COLOR_L;
  if (c == 'M') return CHAR_COLOR_M;
  if (c == 'N') return CHAR_COLOR_N;
}
