#include "TabViewer.h"

void TabViewer::loadDataFromFile() {
  inputF.open(fileName,std::ios::in);

  colCount = 0;
  int wordsLength = 0;

  phs = START;

  int tempSum = 0;
    
  std::string temp = "";
  std::string word;

  if(inputF.is_open()) {
    while(inputF.good()) {
      switch(phs) {
        case START:
          temp += myGetChar();
          if(temp == START_TAG) {
            phs = FIRST_ROW;
          }
          break;
        case FIRST_ROW:
          loadRow(true);

          if(colCount == 0) {
            writeOutput();
            exit(0);
          }

          rowWidth = rowWidth - (colCount+1);

          if(rowWidth < colCount) {
            std::cout << "Not enough width";
            exit(1);
          }

          for(int i = 0; i < colCount; i++)
            wordsLength += tableData[0][i].length();

          colLengths = new size_t[colCount];

          for(int i = 0; i < colCount - 1; i++) {
            colLengths[i] = (int)(((float)tableData[0][i].length() / (float)wordsLength) * rowWidth);
            tempSum += colLengths[i];
          }

          colLengths[colCount - 1] = rowWidth - tempSum;

          phs = INSIDE;
          break;
        case INSIDE:
          loadRow(false);
          break;
        default:
          myGetChar();
      }
    }
    if(phs != DONE) {
      std::cout << "Bad file format";
      exit(1);
    }
  } else {
    std::cout << "Error processing input file.";
    exit(1);
  }

  inputF.close();
}

void TabViewer::loadRow(bool first) {
  std::vector<std::string> row; 
  std::string temp = "";
  std::string word;
  while(temp != ROW_START && temp != END_TAG)
    temp += myGetChar();

  if(temp == END_TAG) {
    phs = DONE;
    return;
  }

  temp = "";

  while(temp != ROW_END) {
    while(temp.find(COL_END) == std::string::npos && temp != ROW_END) {
      temp += myGetChar();
    }
    if(temp == ROW_END) {
      temp = "";
      break;
    }
    if(first)
      colCount++;
    word = temp.substr(COL_START.length(),temp.length()-COL_END.length()-COL_START.length());
    temp = "";
    row.insert(row.end(),word);
  }

  tableData.insert(tableData.end(),row);
}

char TabViewer::myGetChar() {
  char c;

  int add = 0;

  if(lastc == SPACE_CHAR)
    add = 1;

  while((c = inputF.get()) && c < SPACE_CHAR + add && c != -1) {}

  lastc = c;

  return c;
}

void TabViewer::writeDivider() {
  for(int i = 0; i < colCount; i++) {
    std::cout << '+';
    std::cout << std::string(colLengths[i], '-');
  }
  std::cout << "+\n";
}

void TabViewer::writeOutput() {
  if(colCount == 0)
    std::cout << "++\n++\n";
  else {
    writeDivider();
    int rows = tableData.size();
    int i = 0;
    int num = -1;
    bool first = true;
    while(i < rows) {
      std::cout << '|';
      bool change = false;
      std::vector<std::string> newrow;
      for(int k = 0; k < colCount; k++) {
        if(tableData[i][k].length() > colLengths[k]) {
          if(change == false) {
            newrow.resize(colCount);
            change = true;
          }
          newrow[k] = tableData[i][k].substr(colLengths[k]);
        } 
        std::string substr = tableData[i][k].substr(0, colLengths[k]);
        std::cout << substr;
        std::cout << std::string(colLengths[k]-substr.length(),' ') << '|';
      }
      if(!change && first) {
        first = false;
        num = i;
      }

      std::cout << '\n';
      if(change) {
        tableData.insert(tableData.begin()+i+1,newrow);
        rows++;
      }
      if(i == num || i == rows - 1)
        writeDivider();
      i++;
    }
  }
}