
#include "accident.h"

extern struct Accident_dispose_t *Accident_dispose;
extern pthread_rwlock_t accident_dispose_rwlock;
Accident_Upload_Control_t accident_upload_control;
struct Accident_dispose_t *clould_info = NULL;
#ifdef EN_TDENGINE

/**
 * @description: 更新事故反演历史事故消息上报时间
 * @param {*} 
 * @return {*}
 */
void update_accident_send_md5_time(void)
{
    time_t time_now = time(NULL);
    accident_upload_control.accident_upload_time.send_md5_time = time_now;
    accident_upload_control.accident_upload_time.rcv_ftp_time = time_now + ACCIDENT_RCV_FTP_TIMEOUT;
}

/**
 * @description: 更新事故反演文件上传时间
 * @param {*} 
 * @return {*}
 */
void update_accident_send_file_time(void)
{
    time_t time_now = time(NULL);
    accident_upload_control.accident_upload_time.send_file_time = time_now;
    accident_upload_control.accident_upload_time.rcv_clear_file_time = time_now + ACCIDENT_RCV_CLEAR_FILE_TIMEOUT;
}


/**
 * @description: 事故上传控制结构体初始化
 * @param {*} 
 * @return {*}
 */
void accident_upload_control_init(void)
{
    accident_upload_control.accident_upload_state = ACCIDENT_STATE_INIT;
    accident_upload_control.send_accident_create_upload_flag = false;
    accident_upload_control.rcv_accident_clear_flag = false;
    accident_upload_control.accident_upload_time.send_md5_time = time(NULL);
    accident_upload_control.accident_upload_time.rcv_ftp_time = time(NULL);
    accident_upload_control.accident_upload_time.send_file_time = time(NULL);
    accident_upload_control.accident_upload_time.rcv_clear_file_time = time(NULL);
    memset(accident_upload_control.send_md5, 0, sizeof(accident_upload_control.send_md5));
    memset(accident_upload_control.rcv_md5, 0, sizeof(accident_upload_control.rcv_md5));
}

/**
 * @description: 上传文件线程
 * @param {void} *arg
 * @return {*}
 */
void *accident_upload_thread(void *arg)
{
    // struct Accident_dispose_t *dispose_tmp = NULL;
    // struct list_head *pos;
    time_t current_time;
    time_t remaining_sleep;
    while (1) 
    {
        sleep(5);
        
        current_time = time(NULL);

        

        switch (accident_upload_control.accident_upload_state)
        {
            case ACCIDENT_STATE_INIT:
                accident_upload_control.send_accident_create_upload_flag = false;

                clould_info = accident_get_clould_api();
                if(NULL == clould_info)
                {
                    continue;
                }
                char *test_md5 = clould_info->md5;
                if(accident_upload_control.send_accident_create_upload_flag == false)
                {
                    ems_syslog(LOG_ERR, "[zxf]accident send_accident_create_upload_flag set true!");
                    // strncpy(accident_upload_control.send_md5, dispose_tmp->md5, sizeof(accident_upload_control.send_md5)); 
                    strncpy(accident_upload_control.send_md5, test_md5, sizeof(accident_upload_control.send_md5)); 
                    accident_upload_control.send_accident_create_upload_flag = true;
                }
                accident_upload_control.accident_upload_state = ACCIDENT_STATE_WAITING_FTP;
                break;
            case ACCIDENT_STATE_WAITING_FTP:
                if(current_time >= accident_upload_control.accident_upload_time.rcv_ftp_time)
                {
                    ems_syslog(LOG_ERR, "[zxf]accident wait ftp timeout!");
                    accident_upload_control.accident_upload_state = ACCIDENT_STATE_INIT;
                }
                break;
            case ACCIDENT_STATE_WAITING_CLEAR:

                remaining_sleep = ACCIDENT_WAIT_CLEAR_FILE_TIMEOUT;  // 设置睡眠时间
                while(remaining_sleep > 0)
                {
                    sleep(1);

                    // 检查是否收到清除标志
                    if (accident_upload_control.rcv_accident_clear_flag == true)
                    {
                        ems_syslog(LOG_INFO, "[zxf]Received clear flag, break waiting early");
                        break;
                    }

                    // 计算剩余睡眠时间
                    remaining_sleep = 600 - (time(NULL) - current_time);
                }

                accident_upload_control.accident_upload_state = ACCIDENT_STATE_INIT;
                break;
                
            default:
                break;
        }

    }
        
    return NULL;
}

/**
 * @description: 开始事故文件上传任务
 * @param {*}
 * @return {*}
 */
int start_accident_upload_task(void)
{
    accident_upload_control_init();

    pthread_t tid;
    if (pthread_create(&tid, NULL, accident_upload_thread, NULL) != 0)
    {
        ems_syslog(LOG_ERR, "[zxf]accident upload thread failed");
        return -1;
    }
    else
    {
        pthread_setname_np(tid, "accident_upload_task");
    }
    return 0;
}
#else
void update_accident_send_md5_time(void)
{
}

/**
 * @description: 更新事故反演文件上传时间
 * @param {*} 
 * @return {*}
 */
void update_accident_send_file_time(void)
{
}


/**
 * @description: 事故上传控制结构体初始化
 * @param {*} 
 * @return {*}
 */
void accident_upload_control_init(void)
{

}

/**
 * @description: 上传文件线程
 * @param {void} *arg
 * @return {*}
 */
void *accident_upload_thread(void *arg)
{
    return NULL;
}

/**
 * @description: 开始事故文件上传任务
 * @param {*}
 * @return {*}
 */
int start_accident_upload_task(void)
{
    return 0;
}

#endif