/*
 * api_help.c
 *
 *  Created on: Apr 23, 2010
 *  Modified  : Jul 02, 2010
 *      Author: zumbruch
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/iocan128.h>
#include <avr/iocanxx.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>

#include "api.h"
#include "api_define.h"
#include "api_global.h"
#include "api_constants.h"
#include "api_show.h"
#include "api_help.h"


void help(struct uartStruct *ptr_uartStruct)
{
    /* list all available commands with short descriptions
     * or
     * command specific
     *
     * command: HELP [CMND]
     * response: RECV HELP --- CMD1 "bla1"
     *               ...
     *           RECV HELP --- CMDx "blaX"
     * response: RECV HELP *** CMND "bla"
     *               ...
     * response: RECV HELP *** CMND "bla"
     */
#define LENGTH 50
    int32_t index = -1;
    int32_t indexCopy = -1;
    char header[LENGTH];
    char currentReceiveHeader[LENGTH];

    clearString(header, LENGTH);
    createReceiveHeader(NULL, header, LENGTH);
    switch (ptr_uartStruct->number_of_arguments)
    {
    case 0:
        snprintf_P(message, BUFFER_SIZE, PSTR("%s---"), header);
        snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s available commands are:"), message);
        UART0_Send_Message_String(NULL,0);
        for (index = 0; index < commandKeyNumber_MAXIMUM_NUMBER; index++)
        {
            /*exclude list*/
            if ( commandKeyNumber_OWON == index) { continue; }

            /* compose message */
            snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s "), message);
            strncat_P(uart_message_string, (const char*) (pgm_read_word( &(commandKeywords[index]))), BUFFER_SIZE -1 );
            strncat_P(uart_message_string, PSTR(" : "), BUFFER_SIZE - 1 );
            strncat_P(uart_message_string, (const char*) (pgm_read_word( &(commandKeywords[index]))), BUFFER_SIZE -1 );
            strncat_P(uart_message_string, PSTR(" "), BUFFER_SIZE - 1 );
            strncat_P(uart_message_string, (const char*) (pgm_read_word( &(commandShortDescriptions[index]))), LENGTH);
            while (strlen(uart_message_string) < (MAX_LENGTH_KEYWORD + 1 + LENGTH))
            {
                strncat_P(uart_message_string, PSTR(" "), 1);
            }
            strncat_P(uart_message_string, (const char*) (pgm_read_word( &(commandImplementations[index]))), MAX_LENGTH_COMMAND);
            UART0_Send_Message_String(NULL,0);
        }
        break;
    case 1:
        index = Parse_Keyword(&setParameter[1][0]);
        if ( -1 < index && index < commandKeyNumber_MAXIMUM_NUMBER)
        {
            /* get current Command keyword */
            clearString(currentCommandKeyword,MAX_LENGTH_KEYWORD);
            strncat_P(currentCommandKeyword, (const char*) (pgm_read_word( &(commandKeywords[index]))), MAX_LENGTH_KEYWORD);

            /* extend already prepared header by current command keyword */
            /* compose message string */
            clearString(message, BUFFER_SIZE);
            snprintf_P(message, BUFFER_SIZE, PSTR("%s%s ***"), header, currentCommandKeyword);

            snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s %s "), message, currentCommandKeyword);
            strncat_P(uart_message_string, (const char*) (pgm_read_word( &(commandShortDescriptions[index]))), BUFFER_SIZE - 1 );
            UART0_Send_Message_String(NULL,0);

            snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s "), message);
            strncat_P(uart_message_string, (const char*) (pgm_read_word( &(commandImplementations[index]))), BUFFER_SIZE - 1);
            UART0_Send_Message_String(NULL,0);

            /*create keyword specific receiveHeader, e.g. "RECV CMDP"*/
            indexCopy = ptr_uartStruct->commandKeywordIndex;
            ptr_uartStruct->commandKeywordIndex = index;
            clearString(currentReceiveHeader, LENGTH);
            createReceiveHeader(NULL, currentReceiveHeader, LENGTH);
            ptr_uartStruct->commandKeywordIndex = indexCopy;
        }
        switch (index)
        {
        case commandKeyNumber_TWIS:
             snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s send CAN messages "), message );
             UART0_Send_Message_String(NULL,0);
            break;
        case commandKeyNumber_RSET:
            snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s reset micro controller using watchdog mechanism"), message );
            UART0_Send_Message_String(NULL,0);
            //         Initialization(ptr_uartStruct); /* call function with name  readRegister */
            break;

        default:
           snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s %s --- unknown command"), message, header );
           UART0_Send_Message_String(NULL,0);
           break;
        }
        break;
           default:
              ptr_uartStruct->number_of_arguments--;
              help(ptr_uartStruct);
              break;
    }
}

#if 0
strncat_P(     desc[commandKeyNumber_SEND],PSTR("send TWIS messages "),DESC_LENGTH -1);

strncat_P(     desc[commandKeyNumber_WDOG],PSTR("set/get watch/dog status"),DESC_LENGTH -1);

strncat_P(arguments[commandKeyNumber_OWSA],PSTR("<command_key_word> [arguments]"),ARG_LENGTH -1);
#endif
