/************************************************************************************************
*  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.
*
* TODO
*/

#include "functions.h"
#define _INITDMD
#include "dmd_reader_api_load.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>


int main(int argc, const char* argv[])
{
    int ret = 0;
    const char* dmd_filename;
    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 or M to write a list of channels or markers");
        return 1;
    }
    dmd_filename = argv[1];

    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
    {
        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);
    }

    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);
                }
                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;
}

