#include "loadfile.h"
#include <iostream>
#include <QFile>
#include <QTextStream>

QString LoadFile::parseFile(QString path, QString file_format)
{
    QString error; /**/
    // OPEN FILE FOR INPUT
    QFile file(path);
    if (!file.open (QIODevice::ReadOnly)) {
        std::cerr << "File cannot be opened." << std::endl;
        error = "!!! File cannot be opened. !!!"; /**/
        return error; /**/
    }

    QTextStream stream ( &file );

    // OPEN STREAM FOR OUTPUT
    QString output_path = "/tmp/proj_output";
    QFile output( output_path );

    int i = 0;
    QString output_path_orig = output_path;
    while ( output.exists() )
    {
        i++;
        output_path = output_path_orig + QString::number(i);
        output.setFileName( output_path );
    }

    if( !output.open( QIODevice::WriteOnly | QIODevice::Append ) )
    {
      std::cerr << "Failed to create output file." << std::endl;
      error = "!!! Failed to create output file. !!!"; /**/
      return error; /**/
    }

    QTextStream ts( &output );

    int number_columns = 2;
    QChar a = 'A';
    if(file_format.data()[13] == a) { // test type of selected future tool proj/cs2cs or geod/invgeod
        number_columns = 4;
    }

    // INITALIZE VALUES
    QChar separator1 = ',';
    QChar separator2 = ';';
    bool num = false;
    int test, cnum = 0;

    // PROCCESS FILE    
    while( ! stream.atEnd() )
    {
         QChar input;
         stream >> input;

         test = 0;
         if (input == ' ' || input == separator1 || input == separator2 || input == '\t' || input == '\n' || input == '\r' || input == '\r\n') {
            if (num == true) {
               num = false;
               if (cnum != number_columns) {
                  ts << " ";
               }
            }
         }
         else
         {
              if (num == false) {
                 num = true;
                 cnum++;
              }
              ts << input;
              test = 1;
         }
         if (cnum == number_columns && test == 0) {
              ts << '\n';
              cnum = 0;
         }

    }
    if (cnum != 0) {
        output.close();
        file.close(); // when your done.
        std::cerr << "Wrong number of data (probably odd number)." << std::endl;
        error = "!!! Wrong number of data (probably odd number). !!!"; /**/
        return error; /**/
    }
    output.close();
    file.close(); // when your done.

    return output.fileName();
}

void LoadFile::saveAsCSV(QString output_path, QString file_format, QString result)
{
    /*
    // TEST RESULT IS EMPTY
    if (!file.open (QIODevice::ReadOnly))
        std::cerr << "File cannot be opened" << std::endl;
    */

    QChar separator = ',';
    QChar s = 's';
    if(file_format.data()[6] == s) { // test type of selected separator - comma or semicolon
        separator = ';';
    }

    // OPEN STREAM FOR OUTPUT
    QFile output( output_path );

    if(!output.open( QIODevice::WriteOnly | QIODevice::Append ) )
    {
      std::cerr << "Failed to create output file." << std::endl;
    }
    QTextStream ts( &output );

    // INITALIZE VALUES
    QChar input;
    bool num = false;
    int test, cnum = 0, i = 0, result_size = result.size();
    std::cout << result_size << std::endl;

    // PROCCESS FILE
    for (int i=0; i<result_size; i++)
    {
         input = result.at(i);
         /*
         test = 0;
         if (input == ' ' || input == '\t' || input == '\n') {
            if (num == true) {
               num = false;
               if (cnum != 2) {
                  ts << separator;
               }
            }
         }
         else
         {
              if (num == false) {
                 num = true;
                 cnum++;
              }
              ts << input;
              test = 1;
         }
         if (cnum == 2 && test == 0) {
              ts << '\n';
              cnum = 0;
         }
         */

         if (input == ' ' || input == '\t') {
            if (num == true) {
               num = false;
               ts << separator;
            }
         }
         else if (input == '\n' || input == '\r' || input == '\r\n') {
             ts << input;
         }
         else
         {
              if (num == false) {
                 num = true;
              }
              ts << input;
         }

    }
    output.close();

}

