#include "mfcc_thruster.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mfcc_tc_tm_sample.h"

thruster_tank_presure_limit thr_tnk_prs_lmt;
thruster_radiator_temperature_limit thr_rad_temp_limt;
thruster_power_consumption thr_pwr_cmp;
s_thr_mode thrs_mode;
s_thr_firing_duration thrs_fire_dur;
s_thr_propellant_remining thrs_prop_rem;
s_thr_onbrd_temp thrs_onbrd_temp;

uint8_t thruster_id;

int8_t mfcc_thruster_tc_hdlr(uint32_t tc_id, uint8_t** tc_data, uint16_t* tc_len)
{
    int8_t status;
    uint16_t len;
    uint8_t counter=0;


    switch(tc_id)
    {
        case TC_THRUSTER_SET_MODE:
            {
                len = sizeof(s_thr_mode); 
                *tc_data = (uint8_t *)malloc(len);
                printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n4-Thruster_4\n");
                scanf("%hhd",&thrs_mode.thruster_id);
                printf("Set the thruster MODE \n");
                printf("Standby mode - 1\n");
                printf("Thermal conditioning mode - 2\n");
                printf("Firing mode - 3\n");
                printf("Cooling mode - 4\n");
                printf("Commisioning mode - 5\n");
                scanf("%hhd",&thrs_mode.mode);
                if(thrs_mode.mode < 1 || thrs_mode.mode > 5)
                {
                    printf("Invalid Mode\n");
                    status = -1;
                }
                else
                {
                    memcpy(*tc_data,&thrs_mode,sizeof(s_thr_mode));
                    status = 0;
                }
            }
            break;

        case TC_THRUSTER_GET_MODE:
            {
                do{
                    if(counter!=0)
                    {
                        printf("INVALID Thruster ID,Enter inputs again \n");
                    }
                    counter ++;
                    printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3 \n4-Thruster_4\n");
                    scanf("%hhd",&thruster_id);
                }while(thruster_id <1 || thruster_id >4);

                len = sizeof(thruster_id);;
                *tc_data = (uint8_t *)malloc(len);
                memcpy(*tc_data,&thruster_id,sizeof(thruster_id));
                status = 0;
            }
            break;

        case TC_THRUSTER_SET_FIRING_DURATION:
            {
                len = sizeof(s_thr_firing_duration);
                *tc_data = (uint8_t *)malloc(len);
                printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n4-Thruster_4\n");
                scanf("%hhd",&thrs_fire_dur.thruster_id);
                printf("Enter the firing duration in seconds\n");
                scanf("%d",&thrs_fire_dur.firing_duration);
                //printf("############Firing Duration =%d\n",thrs_fire_dur.firing_duration);
                memcpy(*tc_data,&thrs_fire_dur,sizeof(s_thr_firing_duration));
                status = 0;
            }
            break;

        case TC_THRUSTER_GET_FIRING_DURATION:
            {
                counter = 0;
                do{
                    if(counter!=0)
                    {
                        printf("INVALID Thruster ID,Enter inputs again \n");
                    }
                    counter ++;
                    printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3 \n4-Thruster_4\n");
                    scanf("%hhd",&thruster_id);
                }while(thruster_id <1 || thruster_id >4);

                len = sizeof(thruster_id);
                *tc_data = (uint8_t*)malloc(len);
                memcpy(*tc_data,&thruster_id,sizeof(thruster_id));
                status = 0;
            }
            break;

        case TC_THRUSTER_GET_INPUT_POWER_CONSMP: 
            {
                uint8_t counter;
                do{
                    if(counter!=0)
                    {
                        printf("INVALID Thruster ID,Enter inputs again \n");
                    }
                    counter ++;
                    printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3 \n4-Thruster_4\n");
                    scanf("%hhd",&thruster_id);
                }while(thruster_id <1 || thruster_id >4);

                len = sizeof(thruster_id);;
                *tc_data = (uint8_t *)malloc(len);
                memcpy(*tc_data,&thruster_id,sizeof(thruster_id));
                status = 0;
            }
            break;

        case TC_THRUSTER_SET_RADIATOR_TEMP_LIMIT:
            {
                len = sizeof(thruster_radiator_temperature_limit);
                *tc_data = (uint8_t *)malloc(len);
                printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n4-Thruster_4\n");
                scanf("%hhd",&thr_rad_temp_limt.thruster_id);
                printf("Enter the minimum temperature:\n");
                scanf("%f",&thr_rad_temp_limt.min_rad_temp);
                printf("Enter the maximum temperature:\n");
                scanf("%f",&thr_rad_temp_limt.max_rad_temp);
                os_memcpy(*tc_data,&thr_rad_temp_limt,sizeof(thruster_radiator_temperature_limit));
                status = 0;
            }
            break;

        case TC_THRUSTER_GET_RADIATOR_TEMP_LIMIT:
            {
                uint8_t counter;
                do{
                    if(counter!=0)
                    {
                        printf("INVALID Thruster ID,Enter inputs again \n");
                    }
                    counter ++;
                    printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3 \n4-Thruster_4\n");
                    scanf("%hhd",&thruster_id);
                }while(thruster_id <1 || thruster_id >4);

                len = sizeof(thruster_id);;
                *tc_data = (uint8_t *)malloc(len);
                memcpy(*tc_data,&thruster_id,sizeof(thruster_id));
                status = 0;
            }
            break;

        case TC_THRUSTER_SET_TANK_PRESSURE_LIMIT:
            {
                len = sizeof(thruster_tank_presure_limit);
                *tc_data = (uint8_t *)malloc(len);
                printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n4-Thruster_4\n");
                scanf("%hhd",&thr_tnk_prs_lmt.thruster_id);
                printf("Enter the minimum tank pressure:\n");
                scanf("%f",&thr_tnk_prs_lmt.min_tank_prs);
                printf("Enter the maximum tank pressure:\n");
                scanf("%f",&thr_tnk_prs_lmt.max_tank_prs);
                os_memcpy(*tc_data,&thr_tnk_prs_lmt,len);
                status = 0;
            }
            break;

        case TC_THRUSTER_GET_TANK_PRESSURE_LIMIT:
            {
                uint8_t counter;
                do{
                    if(counter!=0)
                    {
                        printf("INVALID Thruster ID,Enter inputs again \n");
                    }
                    counter ++;
                    printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3 \n4-Thruster_4\n");
                    scanf("%hhd",&thruster_id);
                }while(thruster_id <1 || thruster_id >4);

                len = sizeof(thruster_id);;
                *tc_data = (uint8_t *)malloc(len);
                memcpy(*tc_data,&thruster_id,sizeof(thruster_id));
                status = 0;
            }
            break;

        case TC_THRUSTER_GET_REMAINING_PROPELLANT: 
            {
                uint8_t counter;
                do{
                    if(counter!=0)
                    {
                        printf("INVALID Thruster ID,Enter inputs again \n");
                    }
                    counter ++;
                    printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3 \n4-Thruster_4\n");
                    scanf("%hhd",&thruster_id);
                }while(thruster_id <1 || thruster_id >4);
                
                len = sizeof(thruster_id);;
                *tc_data = (uint8_t *)malloc(len);
                memcpy(*tc_data,&thruster_id,sizeof(thruster_id));
                status = 0;
            }
            break;
/*
        case TC_THRUSTER_GET_ACTIVE_STATUS:
            {
                uint8_t counter;
                do{
                    if(counter!=0)
                    {
                        printf("INVALID Thruster ID,Enter inputs again \n");
                    }
                    counter ++;
                    printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3 \n4-Thruster_4\n");
                    scanf("%hhd",&thruster_id);
                }while(thruster_id <1 || thruster_id >4);

                len = sizeof(thruster_id);;
                *tc_data = (uint8_t *)malloc(len);
                memcpy(*tc_data,&thruster_id,sizeof(thruster_id));
                status = 0;
            }
            break;
*/
        case TC_THRUSTER_GET_ONBOARD_TEMPERATURE: 
            {
                uint8_t counter;
                do{
                    if(counter!=0)
                    {
                        printf("INVALID Thruster ID,Enter inputs again \n");
                    }
                    counter ++;
                    printf("Enter the Thruster ID: \n1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3 \n4-Thruster_4\n");
                    scanf("%hhd",&thruster_id);
                }while(thruster_id <1 || thruster_id >4);
                
                len = sizeof(thruster_id);;
                *tc_data = (uint8_t *)malloc(len);
                memcpy(*tc_data,&thruster_id,sizeof(thruster_id));
                status = 0;
            }
            break;
        default: 
            {
                status = -1;
            }
            break;
    }
    *tc_len = len;
    return status;
}

int8_t mfcc_thruster_tm_hdlr(uint32_t tc_tm_id, uint8_t* tm_data, uint16_t tm_len)
{
    uint8_t sts = 1;
    switch(tc_tm_id)
    {
        case TM_THRUSTER_SET_MODE:
            {
                os_memcpy(&sts,tm_data,tm_len);
                if(sts == 1)
                {
                    printf("Thruster mode set fail\n");
                }
                else
                {
                    printf("Thruster mode set successfully\n");
                }
            }
            break;

        case TM_THRUSTER_GET_MODE:
            {
                memset(&thrs_mode,0,sizeof(s_thr_mode));
                printf("TM_LEN==%d\n",tm_len);
                os_memcpy(&thrs_mode,tm_data,tm_len);
                printf("Thruster ID = %d\n",thrs_mode.thruster_id);
               if(thrs_mode.mode == 1)
                {
                    printf("%d - Standby mode\n",thrs_mode.mode);
                }
                else if(thrs_mode.mode == 2)
                {
                    printf("%d - Thermal conditioning mode\n",thrs_mode.mode);
                }
                else if(thrs_mode.mode == 3)
                {
                    printf("%d - Firing mode\n",thrs_mode.mode);
                }
                else if(thrs_mode.mode == 4)
                {
                    printf("%d - Cooling mode\n",thrs_mode.mode);
                }
                else if(thrs_mode.mode == 5)
                {
                    printf("%d - Commisioning mode\n",thrs_mode.mode);
                }
               else
               {
                    printf("%d - Invalid mode\n",thrs_mode.mode);
               }
            }
            break;

        case TM_THRUSTER_SET_FIRING_DURATION:
            {
                os_memcpy(&sts,tm_data,tm_len);
                if(sts == 1)
                {
                    printf("Firing duration set fail\n");
                }
                else
                {
                    printf("Firing duration set successfully\n");
                }
            }
            break;

        case TM_THRUSTER_GET_FIRING_DURATION:
            {
                memset(&thrs_fire_dur,0,5);
                os_memcpy(&thrs_fire_dur,tm_data,tm_len);
                printf("1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n 4-Thruster_4\nThruster ID:%hhd\n",thrs_fire_dur.thruster_id);
                printf("Thruster firing duration : %d\n",thrs_fire_dur.firing_duration);
            }
            break;

        case TM_THRUSTER_GET_INPUT_POWER_CONSMP:
            {
                memcpy(&thr_pwr_cmp,tm_data,tm_len);
                printf("1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n 4-Thruster_4\nThruster ID:%hhd\n",thr_pwr_cmp.thruster_id);
                printf("Thruster input electric power voltage : %f\n",thr_pwr_cmp.voltage);
                printf("Thruster input electric power current : %f\n",thr_pwr_cmp.current);
            }
            break;

        case TM_THRUSTER_SET_RADIATOR_TEMP_LIMIT:
            {
                os_memcpy(&sts,tm_data,tm_len);
                if(sts == 1)
                {
                    printf("Thruster radiator limit not set\n");
                }
                else
                {
                    printf("Thruster radiator limit set successfully\n");
                }
            }
            break;

        case TM_THRUSTER_GET_RADIATOR_TEMP_LIMIT:
            {
                memcpy(&thr_rad_temp_limt,tm_data,tm_len);
                printf("1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n4-Thruster_4\nThruster ID:%hhd\n",thr_rad_temp_limt.thruster_id);
                printf("Thruster mimimun radiation temperature : %f\n",thr_rad_temp_limt.min_rad_temp);
                printf("Thruster maximum radiation temperature : %f\n",thr_rad_temp_limt.max_rad_temp);
            }
            break;

        case TM_THRUSTER_SET_TANK_PRESSURE_LIMIT:
            {
                os_memcpy(&sts,tm_data,tm_len);
                if(sts == 1)
                {
                    printf("Thruster tank pressure limit not set\n");
                }
                else
                {
                    printf("Thruster tank pressure limit set successfully\n");
                }
            }
            break;

        case TM_THRUSTER_GET_TANK_PRESSURE_LIMIT:
            {
                os_memcpy(&thr_tnk_prs_lmt,tm_data,tm_len);
                printf("1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n 4-Thruster_4\nThruster ID:%hhd\n",thr_tnk_prs_lmt.thruster_id);  
                printf("Minimum thruster tank pressure : %f\n",thr_tnk_prs_lmt.min_tank_prs);  
                printf("Maximum thruster tank pressure : %f\n",thr_tnk_prs_lmt.max_tank_prs);  
            }
            break;

        case TM_THRUSTER_GET_REMAINING_PROPELLANT: 
            {
                os_memcpy(&thrs_prop_rem,tm_data,tm_len);
                printf("1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n 4-Thruster_4\nThruster ID:%hhd\n",thrs_prop_rem.thruster_id);
                printf("Thruster remaining propellant : %d\n",thrs_prop_rem.propellant_remain_lvl);
            }
            break;

/*        case TM_THRUSTER_GET_ACTIVE_STATUS:
            {     
                os_memcpy(&thr_act_sts,tm_data,tm_len);
                printf("1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n 4-Thruster_4\nThruster ID:%hhd\n",thr_act_sts.thr_sts);
                printf("Thruster active status : %d\n",thr_act_sts.thr_sts);
            }
            break;
  */
            case TM_THRUSTER_GET_ONBOARD_TEMPERATURE:
            {
                os_memcpy(&thrs_onbrd_temp,tm_data,tm_len);
                printf("1-Thruster_1 \n2-Thruster_2 \n3-Thruster_3\n 4-Thruster_4\nThruster ID:%hhd\n",thrs_onbrd_temp.thruster_id);
                printf("Thruster onboard temperature : %f\n",thrs_onbrd_temp.temperature);
            }
            break;
    }
    return sts;
}
