#include <avr/io.h>
#include <avr/pgmspace.h>


#include "pcd8544.h"
#include "parseconfig.h"
#include "main.h"


unsigned char MakeHeader(sMeasuring *fMeasuring, sSdCard *fSdCard)
{


    char buffer[10];
    struct fat_dir_entry_struct file_entry;

    if(!fat_create_file((*fSdCard).dd, strcat( (*fMeasuring).OutputFileName , ".csv") , &file_entry))
    {
        uart_puts_p(PSTR("file create error\n"));
        return 1;
    }

    while(fat_read_dir((*fSdCard).dd, &file_entry))
    {
        if ( strstr( file_entry.long_name, (*fMeasuring).OutputFileName) )
        {
            (*fMeasuring).File = fat_open_file((*fSdCard).fs, &file_entry);
            if (!(*fMeasuring).File)
            {
                uart_puts_p(PSTR("file open error\n"));
                return 1;
            }
         }
    }

    unsigned char vypis = 0;

    int32_t file_pos = 0;
    if(!fat_seek_file((*fMeasuring).File, &file_pos, FAT_SEEK_END))
    {
        uart_puts_p(PSTR("error seeking on "));
        uart_putc('\n');

        fat_close_file((*fMeasuring).File);
        return 1;
    }
    fat_write_file( (*fMeasuring).File , (uint8_t*)("\n"), strlen("\n"));
    fat_write_file( (*fMeasuring).File , (uint8_t*)"Time [s];", strlen("Time [s];"));
    for (unsigned char i=0;i<8;i++)
    {
        if (strlen((*fMeasuring).PortSetting[i].PortName)!=0)
        {
            fat_write_file( (*fMeasuring).File , (uint8_t*)((*fMeasuring).PortSetting[i].PortName), strlen((*fMeasuring).PortSetting[i].PortName));
            fat_write_file( (*fMeasuring).File , (uint8_t*)(" [V];"), strlen(" [V];"));
        }
    }
    fat_write_file( (*fMeasuring).File , (uint8_t*)(" "), strlen(" "));
    //fat_close_file(Soubor);
    (*fMeasuring).Init=0;
    return 0;
}





unsigned char LoadConfig(sMeasuring *fMeasuring)
{
    uint8_t buffer[8];
    uint8_t fbufferint[10];
    uint8_t Line[32];
    uint8_t Linecount = 0;

    uint8_t offset = 0;
    uint8_t InText = 0;
    intptr_t count;

    unsigned char fStav = 0;
    unsigned char fOblast = 0;
    unsigned char fBufferPrikaz[40];
    unsigned char fBufferHodnota[40];
    unsigned char fBufferPoint = 0;
    unsigned char fPort = 0;

    LcdClear();
    LcdGotoXYFont(2,3);
    LcdFStr(FONT_1X,(unsigned char*)PSTR("Loading"));
    LcdUpdate();

    for (unsigned char i=0;i<8;i++)
    {
        memset(&((*fMeasuring).PortSetting[i].PortName)[0], 0, sizeof((*fMeasuring).PortSetting[i].PortName));
        (*fMeasuring).PortSetting[i].Sampling = 0;
    }

    uart_puts_p(PSTR("Parse start\n"));
    while((count = fat_read_file((*fMeasuring).File, buffer, sizeof(buffer))) > 0)
    {
        offset = 0;
        while(offset<count)
        {
            while(buffer[offset]!='\n' && offset<count)
            {
                if (buffer[offset] == '"')
                        InText ^= 1;

                Line[Linecount] = buffer[offset];
                Linecount++;

                if (buffer[offset] == ' ' && !InText)
                    Linecount--;

                offset++;

            }

            if (buffer[offset] == '\n')
            {
                //tady máme přístupk k jednomu příkazu z řádku
                if (Line[0] != ';' && Line[0] != '#' && Line[0] != '\n' && Linecount>3)
                {
                    fStav = 0;
                    fBufferPoint = 0;
                    memset(&fBufferPrikaz[0], 0, sizeof(fBufferPrikaz));
                    memset(&fBufferHodnota[0], 0, sizeof(fBufferHodnota));
                    for(intptr_t i = 0; i < Linecount; ++i)
                    {
                        switch (fStav) {
                            case 0:                                             //____narava do bufferu typ parametru
                                if (Line[i] == '[')
                                {
                                    fStav = 1;
                                }
                                else
                                {
                                    if (Line[i] != '=')
                                    {
                                        fBufferPrikaz[fBufferPoint] = Line[i];
                                        fBufferPoint++;
                                    }
                                    else
                                    {
                                        fStav=2;
                                        fBufferPoint = 0;
                                    }
                                }

                            break;
                            case 1:                                             //___zpracovani nazvu v [zavorkach]
                                    if (strstr(Line,"Setting"))
                                        fOblast = 1;
                                    if (strstr(Line,"Voltage"))
                                        fOblast = 2;
                                    if (strstr(Line,"Current"))
                                        fOblast = 3;
                                    if (strstr(Line,"Resistance"))
                                        fOblast = 4;
                            break;
                            case 2:                                             //nahrava hodnotu parametru
                                if (Line[i] != '"')
                                {
                                    fBufferHodnota[fBufferPoint] = Line[i];
                                    fBufferPoint++;
                                }

                            break;
                        }
                    }

                    //___zpracovani parametru a hodnot
                    switch (fOblast)
                    {
                        case 1:
                            if (strstr(fBufferPrikaz,"File"))
                            {
                                memset(&((*fMeasuring).OutputFileName)[0], 0, sizeof((*fMeasuring).OutputFileName));
                                strcat( (*fMeasuring).OutputFileName , fBufferHodnota);

                            }
                            else if (strstr(fBufferPrikaz,"StartDate"))
                            {
                                memset(&fbufferint[0], 0, sizeof(fbufferint));
                                fbufferint[0] = fBufferHodnota[0];
                                fbufferint[1] = fBufferHodnota[1];
                                (*fMeasuring).StartTime.day = atoi(fbufferint);
                                memset(&fbufferint[0], 0, sizeof(fbufferint));
                                fbufferint[0] = fBufferHodnota[3];
                                fbufferint[1] = fBufferHodnota[4];
                                (*fMeasuring).StartTime.month = atoi(fbufferint);
                                memset(&fbufferint[0], 0, sizeof(fbufferint));
                                fbufferint[0] = fBufferHodnota[6];
                                fbufferint[1] = fBufferHodnota[7];
                                (*fMeasuring).StartTime.year = atoi(fbufferint);
                                if ( !((*fMeasuring).StartTime.day) && !((*fMeasuring).StartTime.month) && !((*fMeasuring).StartTime.year))
                                    gmStart = 1;
                            }
                            else if (strstr(fBufferPrikaz,"StartTime"))
                            {
                                memset(&fbufferint[0], 0, sizeof(fbufferint));
                                fbufferint[0] = fBufferHodnota[0];
                                fbufferint[1] = fBufferHodnota[1];
                                (*fMeasuring).StartTime.hours = atoi(fbufferint);
                                memset(&fbufferint[0], 0, sizeof(fbufferint));
                                fbufferint[0] = fBufferHodnota[3];
                                fbufferint[1] = fBufferHodnota[4];
                                (*fMeasuring).StartTime.minutes = atoi(fbufferint);
                                memset(&fbufferint[0], 0, sizeof(fbufferint));
                                fbufferint[0] = fBufferHodnota[6];
                                fbufferint[1] = fBufferHodnota[7];
                                (*fMeasuring).StartTime.seconds = atoi(fbufferint);
                            }

                        break;
                        case 2:
                            if (strstr(fBufferPrikaz,"vPort"))
                            {
                                fPort = atoi(fBufferHodnota)-1;
                            }
                            else if(strstr(fBufferPrikaz,"vName"))
                            {
                                strcat( (*fMeasuring).PortSetting[fPort].PortName , fBufferHodnota);
                            }
                            else if(strstr(fBufferPrikaz,"vRange"))
                            {
                                (*fMeasuring).PortSetting[fPort].Range = atoi(fBufferHodnota);
                            }
                            else if(strstr(fBufferPrikaz,"vSampling"))
                            {
                                (*fMeasuring).PortSetting[fPort].Sampling = atoi(fBufferHodnota);
                            }
                        break;
                        case 3:
                        break;
                        case 4:
                        break;
                    }

                }
                memset(&Line[0], 0, sizeof(Line));
                Linecount = 0;
                offset++;
            }

        }

    }

    (*fMeasuring).Sample = nsd((*fMeasuring).PortSetting[0].Sampling, (*fMeasuring).PortSetting[1].Sampling);
    for (unsigned char i=2;i<8;i++)
    {
        (*fMeasuring).Sample = nsd((*fMeasuring).Sample, (*fMeasuring).PortSetting[i].Sampling);
    }
    (*fMeasuring).Init = 0;
    fat_close_file(((*fMeasuring).File));
    return 0;
}
