#include <stdlib.h>
#include <stdint.h>
#include "thruster_tc_tm.h"
#include "exo_tctm_ipc.h"
#include "obc_tc_tm.h"
#include "exo_ahw_al_thruster_common.h"

/**
 * @brief This API used to thruster tc handler
 *
 */
int8_t thruster_tc_hdlr(uint32_t tc_id, uint8_t* tc_data, uint16_t tc_len)
{
    int8_t status = 1;
    switch(tc_id)
    {
        case TC_THRUSTER_SET_MODE:
            {
                status = obc_tc_thruster_set_mode(tc_data);
            }
            break;

        case TC_THRUSTER_GET_MODE:
            {
                status = obc_tc_thruster_get_mode(tc_data);
            }
            break;

        case TC_THRUSTER_SET_FIRING_DURATION:
            {
                status = obc_tc_thruster_set_firing_duration(tc_data);
            }
            break;

        case TC_THRUSTER_GET_FIRING_DURATION:
            {
                status = obc_tc_thruster_get_firing_duration(tc_data);
            }
            break;

        case TC_THRUSTER_GET_INPUT_POWER_CONSMP:
            {
                status = obc_tc_thruster_get_input_power_consmp(tc_data);
            }
            break;

        case TC_THRUSTER_SET_RADIATOR_TEMP_LIMIT:
            {
                status = obc_tc_thruster_set_radiator_temp_limit(tc_data);
            }
            break;

        case TC_THRUSTER_GET_RADIATOR_TEMP_LIMIT:
            {
                status = obc_tc_thruster_get_radiator_temp_limit(tc_data);
            }
            break;

        case TC_THRUSTER_SET_TANK_PRESSURE_LIMIT:
            {
                status = obc_tc_thruster_set_tank_pressure_limit(tc_data);
            }
            break;

        case TC_THRUSTER_GET_TANK_PRESSURE_LIMIT:
            {
                status = obc_tc_thruster_get_tank_pressure_limit(tc_data);
            }
            break;

        case TC_THRUSTER_GET_REMAINING_PROPELLANT:
            {
                status = obc_tc_thruster_get_remaining_propellant(tc_data);
            }
            break;

/*        case TC_THRUSTER_GET_ACTIVE_STATUS:
            {
                status = obc_tc_thruster_get_active_status(tc_data);
            }
            break;
*/
        case TC_THRUSTER_GET_ONBOARD_TEMPERATURE:
            {
                status = obc_tc_thruster_get_onboard_temperature(tc_data);
            }
            break;
    }
    return status;
}

/**
 * @brief This API used to thruster tc handler
 *
 */
int8_t obc_tc_thruster_set_mode(uint8_t* tc_data)
{
    thruster_mode_info thrst_mde;
    uint8_t status = 0;
    obc_pck_data tm_info = {0};
    os_memcpy(&thrst_mde,tc_data,sizeof(thruster_mode_info));
    status = ahw_al_thruster_set_operating_mode(thrst_mde.thruster_id,thrst_mde.mode);
    if(status != 0 )
    {
        status = 1;
    }
    tm_info.rx_data_len = 1;
    tm_info.rx_data = &status;
    tm_info.tm_rsp_id = TM_THRUSTER_SET_MODE;
    obc_pack_tm_data(&tm_info);
    return status;
}

int8_t obc_tc_thruster_get_mode(uint8_t* tc_data)
{
    thruster_mode_info thrst_mde = {0};
    uint8_t status = 1;
    os_memcpy(&thrst_mde.thruster_id,tc_data,1);
    obc_pck_data tm_info = {0};
    status = ahw_al_thruster_get_operating_mode(thrst_mde.thruster_id,&thrst_mde.mode);
    if(status != 0 )
    {
        status = 1;
    }
#ifdef THR_DEB_ENB
    DEBUG_CPRINT(("TC TM thruster Id = %d\n",thrst_mde.thruster_id));
    DEBUG_CPRINT(("TC TM GET mode = %d\n",thrst_mde.mode));
#endif
    tm_info.rx_data_len = sizeof(thruster_mode_info);
    tm_info.rx_data =(uint8_t*)&thrst_mde;
    tm_info.tm_rsp_id = TM_THRUSTER_GET_MODE;
    obc_pack_tm_data(&tm_info);
    return status;
}

int8_t obc_tc_thruster_set_firing_duration(uint8_t* tc_data)
{
    thruster_firing_dur_info firing_dur;
    uint8_t status = 1;
    obc_pck_data tm_info = {0};
    os_memcpy(&firing_dur,tc_data,sizeof(thruster_firing_dur_info));
#ifdef THR_DEB_ENB
    DEBUG_CPRINT(("TC TM firing duration = %ld\n",firing_dur.firing_duration));
#endif
    status = ahw_al_thruster_set_firing_duration(firing_dur.thruster_id,firing_dur.firing_duration);
    if(status != 0 )
    {
        status = 1;
    }
    tm_info.rx_data_len = 1;
    tm_info.rx_data = &status;
    tm_info.tm_rsp_id = TM_THRUSTER_SET_FIRING_DURATION;
    obc_pack_tm_data(&tm_info);
    status = TCTM_OK;
    return status;
}

int8_t obc_tc_thruster_get_firing_duration(uint8_t* tc_data)
{
    uint32_t firing_duration;
    thruster_firing_dur_info firing_dur = {0};
    uint8_t status = 1;
    obc_pck_data tm_info = {0};
    os_memcpy(&firing_dur.thruster_id,tc_data,1);
    status = ahw_al_thruster_get_firing_duration(firing_dur.thruster_id,&firing_duration);
    firing_dur.firing_duration = firing_duration;
    if(status!=0)
    {
        status = 1;
    }
#ifdef THR_DEB_ENB
    DEBUG_CPRINT(("TC TM GET firing duration = %d\n",firing_dur.firing_duration));
    DEBUG_CPRINT(("TC TM Thruster ID = %d\n",firing_dur.thruster_id));
#endif
    tm_info.rx_data_len = sizeof(thruster_firing_dur_info);
    tm_info.rx_data = (uint8_t*)&firing_dur;
    tm_info.tm_rsp_id = TM_THRUSTER_GET_FIRING_DURATION;
    obc_pack_tm_data(&tm_info);
    return status;
}

int8_t obc_tc_thruster_get_input_power_consmp(uint8_t* tc_data)
{
    float voltage;
    float current;
    thruster_power_consumption thr_pwr_cmp = {0};
    uint8_t status = 1;
    obc_pck_data tm_info = {0};
    os_memcpy(&thr_pwr_cmp.thruster_id,tc_data,1);
    status = ahw_al_thruster_get_input_power_voltage_consmp(thr_pwr_cmp.thruster_id,&voltage);
    thr_pwr_cmp.voltage = voltage;
    if(status != 0)
    {
        status = 1;
    }
    status = ahw_al_thruster_get_input_power_current_consmp(thr_pwr_cmp.thruster_id,&current);
    thr_pwr_cmp.current = current;
    if(status != 0)
    {
        status = 1;
    }
    tm_info.rx_data_len = sizeof(thr_pwr_cmp);
    tm_info.rx_data = (uint8_t*)&thr_pwr_cmp;
    tm_info.tm_rsp_id = TM_THRUSTER_GET_INPUT_POWER_CONSMP;
    obc_pack_tm_data(&tm_info);
    return status;
}

int8_t obc_tc_thruster_set_radiator_temp_limit(uint8_t* tc_data)
{
thruster_radiator_temperature_limit thr_rad_temp_limt;
    uint8_t status = 1;
    obc_pck_data tm_info = {0};
    os_memcpy(&thr_rad_temp_limt,tc_data,sizeof(thruster_radiator_temperature_limit));
#ifdef THR_DEB_ENB
    DEBUG_CPRINT(("TC TM set min rad temp = %f\n",thr_rad_temp_limt.min_rad_temp));
    DEBUG_CPRINT(("TC TM set max rad temp = %f\n",thr_rad_temp_limt.max_rad_temp));
#endif
    status = ahw_al_thruster_set_radiator_low_temp_threshold(thr_rad_temp_limt.thruster_id,thr_rad_temp_limt.min_rad_temp);
    if(status != 0)
    {
        status = 1;
    }
    status = ahw_al_thruster_set_radiator_high_temp_threshold(thr_rad_temp_limt.thruster_id,thr_rad_temp_limt.max_rad_temp);
    if(status != 0)
    {
        status = 1;
    }
    tm_info.rx_data_len = 1;
    tm_info.rx_data = &status;
    tm_info.tm_rsp_id = TM_THRUSTER_SET_RADIATOR_TEMP_LIMIT;
    obc_pack_tm_data(&tm_info);
    status = TCTM_OK;
    return status;
}

int8_t obc_tc_thruster_get_radiator_temp_limit(uint8_t* tc_data)
{
    float min_rad_temp;
    float max_rad_temp;
    thruster_radiator_temperature_limit thr_rad_temp_limt = {0};
    uint8_t status = 1;
    obc_pck_data tm_info = {0};
    os_memcpy(&thr_rad_temp_limt.thruster_id,tc_data,1);
    status = ahw_al_thruster_get_radiator_low_temp_threshold(thr_rad_temp_limt.thruster_id,&min_rad_temp);
    thr_rad_temp_limt.min_rad_temp = min_rad_temp;
    if(status != 0)
    {
        status = 1;
    }

#ifdef THR_DEB_ENB
    DEBUG_CPRINT(("TC TM GET min radiator temp = %f\n",thr_rad_temp_limt.min_rad_temp));
#endif
    status = ahw_al_thruster_get_radiator_high_temp_threshold(thr_rad_temp_limt.thruster_id,&max_rad_temp);
    thr_rad_temp_limt.max_rad_temp = max_rad_temp;
    if(status != 0)
    {
        status = 1;
    }
#ifdef THR_DEB_ENB
    DEBUG_CPRINT(("TC TM GET max radiator temp = %f\n",thr_rad_temp_limt.max_rad_temp));
#endif
    tm_info.rx_data_len = sizeof(thr_rad_temp_limt);
    tm_info.rx_data = (uint8_t*)&thr_rad_temp_limt;
    tm_info.tm_rsp_id = TM_THRUSTER_GET_RADIATOR_TEMP_LIMIT;
    obc_pack_tm_data(&tm_info);
    return status;
}

int8_t obc_tc_thruster_set_tank_pressure_limit(uint8_t* tc_data)
{
thruster_tank_presure_limit thr_tnk_prs_lmt;
    uint8_t status = 1;
    obc_pck_data tm_info = {0};
    os_memcpy(&thr_tnk_prs_lmt,tc_data,sizeof(thruster_tank_presure_limit));
    status = ahw_al_thruster_set_min_tank_pressure_limit(thr_tnk_prs_lmt.thruster_id,thr_tnk_prs_lmt.min_tank_prs);
    if(status != 0)
    {
        status = 1;
    }
    status = ahw_al_thruster_set_max_tank_pressure_limit(thr_tnk_prs_lmt.thruster_id,thr_tnk_prs_lmt.max_tank_prs);
    if(status != 0)
    {
        status = 1;
    }
    os_memcpy(&thr_tnk_prs_lmt,tc_data,sizeof(thruster_tank_presure_limit));
    tm_info.rx_data_len = 1;
    tm_info.rx_data = &status;
    tm_info.tm_rsp_id = TM_THRUSTER_SET_TANK_PRESSURE_LIMIT;
    obc_pack_tm_data(&tm_info);
    status = TCTM_OK;
    return status;
}


int8_t obc_tc_thruster_get_tank_pressure_limit(uint8_t* tc_data)
{
    float min_tank_prs;
    float max_tank_prs;
    thruster_tank_presure_limit thr_tnk_prs_lmt = {0};
    uint8_t status = 1;
    obc_pck_data tm_info = {0};
    os_memcpy(&thr_tnk_prs_lmt.thruster_id,tc_data,1);
#ifdef THR_DEB_ENB
    DEBUG_CPRINT(("TC TM GET min tnk prs = %f\n",thr_tnk_prs_lmt.min_tank_prs));
    DEBUG_CPRINT(("TC TM GET max tnk prs = %f\n",thr_tnk_prs_lmt.max_tank_prs));
#endif 
    status = ahw_al_thruster_get_min_tank_pressure_limit(thr_tnk_prs_lmt.thruster_id,&min_tank_prs);
    thr_tnk_prs_lmt.min_tank_prs = min_tank_prs;
    if(status != 0)
    {
        status = 1;
    }
    status = ahw_al_thruster_get_max_tank_pressure_limit(thr_tnk_prs_lmt.thruster_id,&max_tank_prs);
    thr_tnk_prs_lmt.max_tank_prs = max_tank_prs;
    if(status != 0)
    {
        status = 1;
    }
    tm_info.rx_data_len = sizeof(thr_tnk_prs_lmt);
    tm_info.rx_data = (uint8_t*)&thr_tnk_prs_lmt;
    tm_info.tm_rsp_id = TM_THRUSTER_GET_TANK_PRESSURE_LIMIT;
    obc_pack_tm_data(&tm_info);
    return status;
}

int8_t obc_tc_thruster_get_remaining_propellant(uint8_t* tc_data)
{
    float thr_rmng_prplnt_lvl;
    thruster_remaining_proplent_level get_rem_prplnt = {0};
    uint8_t status = 1;
    obc_pck_data tm_info = {0};
    os_memcpy(&get_rem_prplnt.thruster_id,tc_data,1);
#ifdef THR_DEB_ENB
    DEBUG_CPRINT(("TC TM GET remaining proplnt = %f\n",get_rem_prplnt.thr_rmng_prplnt_lvl));
#endif
    status = ahw_al_thruster_get_remaining_propellant_level(get_rem_prplnt.thruster_id,&thr_rmng_prplnt_lvl);
    get_rem_prplnt.thr_rmng_prplnt_lvl = thr_rmng_prplnt_lvl;
    if(status != 0)
    {
       status = 1;
    } 
    tm_info.rx_data_len = sizeof(get_rem_prplnt);
    tm_info.rx_data =(uint8_t*)&get_rem_prplnt;
    tm_info.tm_rsp_id = TM_THRUSTER_GET_REMAINING_PROPELLANT;
    obc_pack_tm_data(&tm_info);
    return status;
}

/*
int8_t obc_tc_thruster_get_active_status(uint8_t* tc_data)
{
thruster_get_status thr_get_sts = {0};
    uint8_t status = 1;
    os_memcpy(&thr_get_sts.thruster_id,tc_data,1);
    status = ahw_al_thruster_get_active_status(thr_get_sts.thruster_id,&thr_get_sts.get_act_sts);
    if(status != 0)
    {
        status = 1;
    }
    obc_pck_data tm_info = {0};
    tm_info.rx_data_len = sizeof(thr_get_sts);
    tm_info.rx_data =(uint8_t*)&thr_get_sts;
    tm_info.tm_rsp_id = TM_THRUSTER_GET_ACTIVE_STATUS;
    obc_pack_tm_data(&tm_info);
    return status;
}
*/

int8_t obc_tc_thruster_get_onboard_temperature(uint8_t* tc_data)
{
    float get_onbrd_temp;
    thruster_onboard_temperature thr_onbrd_temp = {0};
    uint8_t status = 1;
    os_memcpy(&thr_onbrd_temp.thruster_id,tc_data,1);
    status = ahw_al_thruster_get_on_board_temperature(thr_onbrd_temp.thruster_id,&get_onbrd_temp);
    thr_onbrd_temp.get_onbrd_temp = get_onbrd_temp;
    if(status != 0)
    {
        status = 1;
    }
    obc_pck_data tm_info = {0};
    tm_info.rx_data_len = sizeof(thr_onbrd_temp);
    tm_info.rx_data =(uint8_t*)&thr_onbrd_temp;
    tm_info.tm_rsp_id = TM_THRUSTER_GET_ONBOARD_TEMPERATURE;
    obc_pack_tm_data(&tm_info);
    return status;
}
