#include "epdpaint.h"

String pad(String text, int pad_length, char pad_letter) {
  if(text.length() >= pad_length) return text;  // if theres nothing to fill, return the text unmodified
  
  for(int i = 0; text.length()<pad_length; i++) text += pad_letter;  // fill with pad_letter until the text is as long as padlength specifies
  return text;
}
String pad(String text, int pad_length) { return pad(text, pad_length, ' '); }  // just a fallback for pad_letter

/* Tell how many rows of text will be needed at given font-width and box-width for the text given */
int getMaxRows(String text, int width, int letter_width) {
  int lettersPerRow = width / letter_width;
  float maxRows = float(text.length())/float(lettersPerRow);
  int maxRowsFloor = text.length()/lettersPerRow;
  if(maxRows>maxRowsFloor) maxRows = maxRowsFloor+1.0;  // if maxRows is e.g. 5.2, write the next-bigger integer into maxRows

  return maxRows;
}

/* text_of_row_x tells which row shall be given, 0 for first row */
String getRow(String text, int width, int letter_width, int text_of_row_x) {  // width should be a multiple of letter_width to fit perfect
  // calculate needed vars
  int lettersPerRow = width / letter_width;
  int maxRows = getMaxRows(text, width, letter_width);
  text = pad(text, maxRows*lettersPerRow, ' ');

  // some tests
  if(text_of_row_x > maxRows) return String();
  if(text_of_row_x < 0) return String();

  return text.substring(lettersPerRow*text_of_row_x, lettersPerRow*(text_of_row_x+1));
}

// remove special characters (ä,ö,ü,ß)
String replaceSpecialChars(String text) {
  if(text[0] == ' ') { text = text.substring(1,text.length()-1); }
  text.replace("ä", "ae");
  text.replace("Ä", "AE");
  text.replace("&auml;", "ae");
  text.replace("&Auml;", "AE");
  text.replace("ö", "oe");
  text.replace("Ö", "OE");
  text.replace("&ouml;", "oe");
  text.replace("&Ouml;", "OE");
  text.replace("ü", "ue");
  text.replace("Ü", "UE");
  text.replace("&uuml;", "ue");
  text.replace("&Uuml;", "UE");
  text.replace("ß", "ss");
  text.replace("ẞ", "SS");
  text.replace("&szlig;", "ss");

  return text;
}

// show the daily text on the epd
void showDailyText(String losung, String lehrtext, String losungPosition, String lehrtextPosition) {
  // clean the strings for
  losung = replaceSpecialChars(losung);
  lehrtext = replaceSpecialChars(lehrtext);
  losungPosition = replaceSpecialChars(losungPosition);
  lehrtextPosition = replaceSpecialChars(lehrtextPosition);

  // position vars
  int text_x = 0;
  int text_y = 0;

  /* This clears the SRAM of the e-paper display and displays it */
  epd.ClearFrame();
  epd.DisplayFrame();

  // now the drawing init
  unsigned char image[2048];
  Paint paint(image, 176, TITLE_FONT.Height+2);    // width should be the multiple of 8
  
  // display title
  paint.Clear(UNCOLORED);
  int startTitleAtX = round((paint.GetWidth()-strlen(TITLE_TEXT)*TITLE_FONT.Width)/2);
  paint.DrawStringAt(startTitleAtX, 0, TITLE_TEXT, &TITLE_FONT, COLORED);
  paint.DrawHorizontalLine(startTitleAtX+15, TITLE_FONT.Height+1, paint.GetWidth()-(startTitleAtX+15)*2, COLORED);
  text_y = TITLE_PADDING_TOP;
  epd.TransmitPartialData(paint.GetImage(), text_x, text_y, paint.GetWidth(), paint.GetHeight());
  text_y += TITLE_FONT.Height+2;

  // Losung
  paint.SetWidth(TEXT_WIDTH);
  paint.SetHeight(TEXT_FONT.Height);
  text_y += TEXT_PADDING_TOP;

  for(int row = 0; row<getMaxRows(losung, TEXT_WIDTH, TEXT_FONT.Width); row++) {
    String text_of_row = getRow(losung, TEXT_WIDTH, TEXT_FONT.Width, row);

    paint.Clear(COLORED);
    paint.DrawStringAt(0, 0, text_of_row.c_str(), &TEXT_FONT, UNCOLORED);
    epd.TransmitPartialData(paint.GetImage(), EPD_WIDTH-TEXT_WIDTH, text_y, paint.GetWidth(), paint.GetHeight());
    text_y += paint.GetHeight();
    text_y += TEXT_PADDING_BETWEEN_LINES;
  }

  // Losung bible position
  paint.SetWidth(losungPosition.length()*SOURCE_FONT.Width);
  paint.SetHeight(SOURCE_FONT.Height);
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(0, 0, losungPosition.c_str(), &SOURCE_FONT, COLORED);
  epd.TransmitPartialData(paint.GetImage(), EPD_WIDTH-losungPosition.length()*SOURCE_FONT.Width, text_y, paint.GetWidth(), paint.GetHeight());

  // Lehrtext
  paint.SetWidth(TEXT_WIDTH);  // text block width
  paint.SetHeight(TEXT_FONT.Height);
  text_y += TEXT_PADDING_BETWEEN_BLOCKS;  // some space between "losung" block

  for(int row = 0; row<getMaxRows(lehrtext, TEXT_WIDTH, TEXT_FONT.Width); row++) {
    String text_of_row = getRow(lehrtext, TEXT_WIDTH, TEXT_FONT.Width, row);

    paint.Clear(UNCOLORED);
    paint.DrawStringAt(0, 0, text_of_row.c_str(), &TEXT_FONT, COLORED);
    epd.TransmitPartialData(paint.GetImage(), 0, text_y, paint.GetWidth(), paint.GetHeight());
    text_y += paint.GetHeight();
    text_y += TEXT_PADDING_BETWEEN_LINES;
  }

  // Lehrtext bible position
  paint.SetWidth(lehrtextPosition.length()*SOURCE_FONT.Width);
  paint.SetHeight(SOURCE_FONT.Height);
  paint.Clear(COLORED);
  paint.DrawStringAt(0, 0, lehrtextPosition.c_str(), &SOURCE_FONT, UNCOLORED);
  epd.TransmitPartialData(paint.GetImage(), 5, text_y, paint.GetWidth(), paint.GetHeight());


  // display the whole sent data
  epd.DisplayFrame();
}  