/************************************************************************************************
*  Copyright Dewetron Ges.m.b.H 2015                                                           *
*                                                                                              *
*   This file is a DMD reader library example                                                  *
************************************************************************************************/

/**
* @file main.c
* @author Dewetron Ges.m.b.H.
* @date 2 Feb 2015
* @brief DMD Reader lib example usage.
*
* Writes selected parts of a DMD file into a CSV
*/

#include "functions.h"
#define _INITDMD
#include "dmd_reader_api_load.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#ifdef WIN32
#else
#include <linux/limits.h>
#endif

#ifndef MAX_PATH
#define MAX_PATH 256
#endif

int main(int argc, const char* argv[])
{
    int ret = 0;
    char dmd_filename[MAX_PATH];
    int dmd_channel_index = 0;
    const char* csv_filename;
    int mode = -1;

    if (argc < 4)
    {
        printf("Usage: csvwriter.exe <dmd_filename.dmd> <channelIndex> <csv_filename.csv> <OPTIONAL: user_reduced 0 or 1>\n");
        printf("       <channelIndex> can be C, M or X to write a list of channels, markers or the config xml");
        return 1;
    }


#if WIN32
    if (!_fullpath(dmd_filename, argv[1], MAX_PATH))
#else
    if (!realpath(argv[1], dmd_filename))
#endif
    {
        printf("Error could not open input file %s\n", argv[1]);
        return 3;
    }

    if ( (strcmp(argv[2], "M")==0)
       ||(strcmp(argv[2], "m")==0))
    {
        mode = 1;
    }
    else if ( (strcmp(argv[2], "C") == 0)
            ||(strcmp(argv[2], "c") == 0))
    {
        mode = 3;
    }
    else if ( (strcmp(argv[2], "X") == 0)
            ||(strcmp(argv[2], "x") == 0))
    {
        mode = 4;
    }
    else
    {
        mode = 0;
        dmd_channel_index = atoi(argv[2]);
    }
    if (argc == 5)
    {
        if (strcmp(argv[4], "1") == 0)
        {
            mode = 2;
            dmd_channel_index = atoi(argv[2]);
        }
    }
    csv_filename = argv[3];

    if (mode == 0)
    {
        printf("Input:   %s\nData:   Channel %d\nOutput:  %s\n", dmd_filename, dmd_channel_index, csv_filename);
    }
    else if (mode == 1)
    {
        printf("Input:   %s\nData:    Markers\nOutput:  %s\n", dmd_filename, csv_filename);
    }
    else if (mode == 2)
    {
        printf("Input:   %s\nReduced Data:   Channel %d\nOutput:  %s\n", dmd_filename, dmd_channel_index, csv_filename);
    }
    else if (mode == 3)
    {
        printf("Input:   %s\nData:    Channel List\nOutput:  %s\n", dmd_filename, csv_filename);
    }
    else if (mode == 4)
    {
        printf("Input:   %s\nData:    XML Configuration\nOutput:  %s\n", dmd_filename, csv_filename);
    }

    if (DMDReader_LoadLibrary(DMD_READER_DLL_NAME) != DMDReader_NoError)
    {
        printf("Error: Could not load dmd reader api\n");
        return 2;
    }

    {
        DMDReader_FileHandle dmd_handle;
        if (DMDReader_OpenFile(dmd_filename, &dmd_handle) == DMDReader_NoError)
        {
            FILE* csv_handle = fopen(csv_filename, "w");
            if (csv_handle)
            {
                const char* error_info = NULL;
                printf("Starting CSV output\n");
                if (mode == 0)
                {
                    error_info = WriteDMDChannelToCSV(dmd_handle, dmd_channel_index, csv_handle);
                }
                else if (mode == 1)
                {
                    error_info = WriteDMDMarkersToCSV(dmd_handle, csv_handle);
                }
                else if (mode == 2)
                {
                    error_info = WriteDMDChannelToCSV_Reduced(dmd_handle, dmd_channel_index, csv_handle);
                }
                else if (mode == 3)
                {
                    error_info = WriteDMDChannelsToCSV(dmd_handle, csv_handle);
                }
                else if (mode == 4)
                {
                    error_info = WriteXMLConfig(dmd_handle, csv_handle);
                }
                fclose(csv_handle);
                if (error_info)
                {
                    printf("Error: %s\n", error_info);
                    ret = 5;
                }
            }
            else
            {
                printf("Error could not create output file %s\n", csv_filename);
                ret = 4;
            }
            DMDReader_CloseFile(&dmd_handle);
        }
        else
        {
            printf("Error could not open input file %s\n", dmd_filename);
            ret = 3;
        }
    }

    DMDReader_CloseLibrary();
    return ret;
}

