/** \file
 *  \brief This file defines the serial console commands for the high-level state machine.
 */

/* Include Files */
#include "KTag.h"

#if (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED)

/* Local Definitions and Constants */

/* Private Function Prototypes */
static void Simulate_Hit(uint8_t team_ID, uint16_t damage);

/* Public Variables */

/* Private Variables */

/* Public Functions */

//! Console command handler for the 'event' command.
COMM_Console_Command_Result_T COMM_HandleEventCommand(char8 * data, uint32_t size)
{
    // data[0] through data[5] is 'event '.
    
    if (data[6] == '?')
    {
        COMM_Console_Print_String("event ?             Display this help.\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("event raw <id>      Inject the event with ID <id>.\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("event tag <n>       Send <n> tag(s).\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("event hit <t> <d>   Simulate a hit from team <t> for <d> damage.\n");
        vTaskDelay(pdMS_TO_TICKS(10));
    }
    else if ( (data[6] == 'r') &&
              (data[7] == 'a') &&
              (data[8] == 'w') )
              
    {
        if (COMM_Console_IsEndOfMessage(data[9]))
        {
            COMM_Console_Print_String("ERROR: missing event ID!\n");
        }
        else if (data[9] == ' ')
        {
            uint16_t id = 0;
        
            if (COMM_Console_DecodeParameterUInt16(&(data[10]), &id) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                if ((id > KEVENT_NO_EVENT) && (id < KEVENT_IS_OUT_OF_RANGE))
                {
                    KEvent_T raw_event = { .ID = id, .Data = (void *)0x00 };
                    Post_KEvent(&raw_event);
                }
                else
                {
                    COMM_Console_Print_String("ERROR: specified event ID (");
                    COMM_Console_Print_UInt16(id);
                    COMM_Console_Print_String(") is invalid!\n");
                }
            }
            else
            {
                COMM_Console_Print_String("ERROR: could not comprehend event ID!\n");
            }
        }
        else
        {
            COMM_Console_Print_String("ERROR: unrecognized or mangled command!\n");
        }
    }
    else if ( (data[6] == 't') &&
              (data[7] == 'a') &&
              (data[8] == 'g') )
              
    {
        if (COMM_Console_IsEndOfMessage(data[9]))
        {
            if (Send_Tag() == SYSTEMK_RESULT_SUCCESS)
            {
                COMM_Console_Print_String("Tag sent.\n");
            }
            else
            {
                COMM_Console_Print_String("Error: Couldn't send tag!\n");
            }
        }
        else if (data[9] == ' ')
        {
            uint16_t times = 0;
        
            if (COMM_Console_DecodeParameterUInt16(&(data[10]), &times) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                while (times > 0)
                {
                    if (Send_Tag() == SYSTEMK_RESULT_SUCCESS)
                    {
                        COMM_Console_Print_String("Tag sent.\n");
                    }
                    else
                    {
                        COMM_Console_Print_String("Error: Couldn't send tag!\n");
                    }
                    //! \todo Why can't the console command 'event tag <n>' send tags faster than once per second?
                    vTaskDelay(1000 / portTICK_PERIOD_MS);
                    times--;
                }
            }
            else
            {
                COMM_Console_Print_String("ERROR: could not comprehend tag repetitions!\n");
            }
        }
        else
        {
            COMM_Console_Print_String("ERROR: unrecognized or mangled command!\n");
        }
    }
    else if ( (data[6] == 'h') &&
              (data[7] == 'i') &&
              (data[8] == 't') )
              
    {
        if (COMM_Console_IsEndOfMessage(data[9]))
        {
            Simulate_Hit(1, 10);
            COMM_Console_Print_String("Hit!\n");
        }
        else if (data[9] == ' ')
        {
            uint8_t team_ID = 0;
            uint16_t damage = 10;
        
            if (COMM_Console_DecodeParameterUInt8(&(data[10]), &team_ID) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                const char * damage_location;
                
                // Damage is the first parameter after team ID.
                if (COMM_Console_FindNthParameter(&(data[10]), 1, &damage_location) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
                {
                    if (COMM_Console_DecodeParameterUInt16(damage_location, &damage) != COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
                    {
                        COMM_Console_Print_String("ERROR: could not comprehend damage--using default.\n");
                        damage = 10;
                    }
                }
                Simulate_Hit(team_ID, damage);
                COMM_Console_Print_String("Hit!\n");
            }
            else
            {
                COMM_Console_Print_String("ERROR: could not comprehend team ID!\n");
            }
        }
        else
        {
            COMM_Console_Print_String("ERROR: unrecognized or mangled command!\n");
        }
    }
    else
    {
        COMM_Console_Print_String("ERROR: Unknown event command!\n");
    }
    

	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

/* Private Functions */

static void Simulate_Hit(uint8_t team_ID, uint16_t damage)
{    
    static DecodedPacket_T Simulated_Tag_Rx_Buffer;
    static KEvent_T tag_received_event;
    
    Simulated_Tag_Rx_Buffer.Tag.type = DECODED_PACKET_TYPE_TAG_RECEIVED;
    Simulated_Tag_Rx_Buffer.Tag.protocol = LASER_X_PROTOCOL;
    Simulated_Tag_Rx_Buffer.Tag.player_ID = 0x00;
    Simulated_Tag_Rx_Buffer.Tag.team_ID = team_ID;
    Simulated_Tag_Rx_Buffer.Tag.damage = damage;
    Simulated_Tag_Rx_Buffer.Tag.color = GetColorFromTeamID(team_ID);
    tag_received_event.ID = KEVENT_TAG_RECEIVED;
    tag_received_event.Data = &Simulated_Tag_Rx_Buffer;
    Post_KEvent(&tag_received_event);
}

#endif // (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED)
