#include "fault_manager.h"
#include "dido_manager.h"
#include "eeprom_manager.h"
#include "hv_adc_manager.h"
#include "logic_ctrl.h"

DoLogicStatus logic_cmd_ctrl(uint32_t base_time)
{
    static DoLogicStatus status = kDoLogicStatus_Off;
    switch(status)
    {
        case kDoLogicStatus_Off:
			if(bms_get_circuit_cmd_status()== kCircuitStatus_On && bms_is_dis_allow() == true && bms_is_chg_allow() == true)
			{
				bms_crtl_do_status(kDoType_Positive, kDoCtrlStatus_On, 100);
                bms_crtl_do_status(kDoType_Negative, kDoCtrlStatus_On, 100);
				status = kDoLogicStatus_On;
			}	
			break;
		case kDoLogicStatus_On:
			if(((bms_is_dis_allow() == false || bms_is_chg_allow() == false)) && (KIT_ABS(bms_get_current()) < 30))
			{
				bms_crtl_do_status(kDoType_PreChg, kDoCtrlStatus_Off, RELAY_CTRL_NO_DLY);
				bms_crtl_do_status(kDoType_Positive, kDoCtrlStatus_Off, 100);
				bms_crtl_do_status(kDoType_Negative, kDoCtrlStatus_Off, 100);
				status = kDoLogicStatus_Off;
			}
			break;
		default:
			break; 
	}
	return status;
}

void logic_cmd_ctrl_poll(uint32_t base_time, uint8_t idx)
{
    CircuitStatus circuit_status = kCircuitStatus_Off;
    DoStatus relay_status = bms_get_do_status_by_type(kDoType_Positive);
    
    logic_cmd_ctrl(base_time);
    circuit_status = (CircuitStatus)((relay_status == kDoStatus_On) || (relay_status == kDoStatus_ForceOn));
    
    bms_set_circuit_status(kCircuitType_Dis, circuit_status);
    bms_set_circuit_status(kCircuitType_Chg, circuit_status);
    bms_set_circuit_status(kCircuitType_Main, circuit_status);
}

