#include "drv_rtc.h"
#include "ocv_tables.h"
#include "soc_calculate.h"
#include "fault_manager.h"
#include "hv_adc_manager.h"
#include "eeprom_manager.h"
#include "statistic_manager.h"

#include "table_comm.h"

#include "kit_time.h"
#include "kit_debug.h"

static uint32_t ocv_dly = 0;
uint16_t bms_check_ocv(uint16_t soc)
{
    int32_t ocv_soc, min_temp, avg_volt;
    uint16_t lower, upper;
	kit_ret_e tmp = kKit_Ret_Error;
	uint8_t index = 0;
	
    min_temp = (int32_t)bms_get_statistic_data(kStatisticData_MinTemp) /10 - 50;
    avg_volt = bms_get_statistic_data(kStatisticData_AvgVolt);
    
    index = get_eeprom_data(kEep_Sop_OcvIndex, kEepromDataType_Low);
	if(index < kOcvTab_End)
	{
		tmp = kit_table_bilinear_search_by_yz((ThreeDTabItem*)ocv_table_list[index], min_temp, avg_volt, 100, &ocv_soc);
	}
	if((bms_get_fault_level(kFaultCode_AFE) == kFaultLevel_Normal) 
    && (tmp == kKit_Ret_Ok))
    {
        //是否在校准范围
        upper = get_eeprom_data(kEep_OcvDisRangeLower_Upper, kEepromDataType_Low) * 100;
        lower = get_eeprom_data(kEep_OcvDisRangeLower_Upper, kEepromDataType_High) * 100;
        
        if(lower < upper)
        {
            if((ocv_soc <= lower ) || (ocv_soc >= upper))
            {
                if(soc >= ocv_soc + 100)
                {
                    soc -= 100;
                }
                else if(soc + 100 <= ocv_soc)
                {
                    soc += 100;
                }
            }
        }
        //无禁校区间
        else
        {
            soc = ocv_soc;
        }
    }
    return soc;
}


uint16_t bms_check_pwr_on_ocv(uint16_t soc)
{
    uint32_t now_time = drv_rtc_get_tick();
    uint32_t last_time = get_eeprom_data(kEep_PowerOffTimeL, kEepromDataType_Double);
    uint32_t standby_time = (uint32_t)get_eeprom_data(kEep_OcvStandbyCur_Time, kEepromDataType_Low) * 360;
    KIT_DEBUG_PRINTF("OCV Last Time:%d \r\n", last_time);
    KIT_DEBUG_PRINTF("OCV Now Time:%d \r\n", now_time);
    if((standby_time != 0) && (now_time > last_time) 
    && ((now_time - last_time) >= standby_time))
	{
        ocv_dly = standby_time * 1000 - KIT_SECOND_CONVERT(30);
        KIT_DEBUG_PRINTF("Power On OCV\r\n");
        soc = bms_check_ocv(soc);
	}
    return soc;
}

uint16_t bms_check_run_ocv(uint32_t base_time, uint16_t soc)
{   
    uint32_t standby_time;
    
    if(KIT_ABS(bms_get_current()) <= get_eeprom_data(kEep_OcvStandbyCur_Time, kEepromDataType_High))
    {
        ocv_dly += base_time;
        standby_time = (uint32_t)get_eeprom_data(kEep_OcvStandbyCur_Time, kEepromDataType_Low) * 3600 * 1000;
		//standby_time = 1000 * 60;
        if((standby_time != 0) && (ocv_dly >= standby_time))
        {
            //OCV校准后dly-15min，不清零，便于下次快速校准
            if(ocv_dly >= KIT_SECOND_CONVERT(30))
            {
                ocv_dly -= KIT_SECOND_CONVERT(30);
            }
            else
            {
                ocv_dly = 0;
            }
            soc = bms_check_ocv(bms_get_soc());
        }
    }
    else
    {
        ocv_dly = 0;
    }
    
    return soc;
}
