#include "MineCardManager.h"
#include "ya_setting.h"
#include "common_tool.h"
#include "mine.h"

CMineCardManager::CMineCardManager()
{
    m_minecards = std::make_shared<mine_card_info>();
}

std::shared_ptr<mine_card_info> CMineCardManager::GetMineCards() 
{
    return m_minecards;
}

 /**
     * @brief 获取事件ID
     **/
uint64_t CMineCardManager::GetMineEventId(int evType)
{
    return event_list::to_list_id(static_cast<EVENT_TYPE>(evType),OT_MINE,0,0);
}

bool CMineCardManager::Exist(SET_CARDID &list,uint64_t id)
{
    if (list.find(id) == list.end())
    {
        return false;
    }
    return true;
}

bool CMineCardManager::Exist(SET_CARDID &list,int card_id,int card_type)
{
    return Exist(list,tool_other::type_id_to_u64(card_type,card_id));
}

/**
 * @brief 创建警告开始事件
 * @param evType 警告类型
 * @param limitVal 限制数据(阀门值)
 * @param curVal 当前数据
 **/
void CMineCardManager::StartEvent(int evType,int limitVal,int curVal)
{
    int oldState = EVENT_STATUS::ES_END;
    uint64_t evid = this->GetMineEventId(evType);
    auto ev_ptr = event_list::instance()->get(evid);
    if (nullptr != ev_ptr)
    {
        oldState = ev_ptr->m_status;
    }
    else
    {
        ev_ptr = event_list::instance()->create_event_mine(0,(EVENT_TYPE)evType);
        if (nullptr == ev_ptr)
        {
            //打印日志
            log_error("CMineCardManager::StartEvent: Create Event Fail.evid=%%llu evType=%d,evState=ES_START ",evid,evType);
            return ;
        }
        ev_ptr->m_obj_id = "0";
        event_list::instance()->add(ev_ptr->get_list_id(),ev_ptr);
    }
    ev_ptr->m_status = EVENT_STATUS::ES_START;	
    ev_ptr->m_cur_value= curVal;
    ev_ptr->m_limit_value = limitVal;
    ev_ptr->m_is_display = false;
    ev_ptr->m_cur_time = std::chrono::system_clock::now();;
    if (oldState == EVENT_STATUS::ES_END)
    {
        event_list::instance()->save_event(ev_ptr);
    }
    if (evType < MAX_MINE_EVENT_NUM)
    {
        m_evlist[evType] = evid;
    }
    log_info("CMineCardManager::StartEvent: Create Success .id=%%llu -%%llu  evType=%d,evState=ES_START ",evid,ev_ptr->get_list_id(),evType);
}

/**
 * @brief 取消警告事件
 * @param evType 警告类型
 * @param curVal 当前数据
 **/
void CMineCardManager::CancelEvent(int evType,int curVal)
{
    uint64_t evid = this->GetMineEventId(evType);
    auto ev_ptr = event_list::instance()->get(evid);
    if (nullptr == ev_ptr)
    {
        //打印日志
        log_errno("CMineCardManager::CancelEvent: Not Find Event.id=%%llu  evType=%d,evState=ES_END ",evid,evType);
        return ;
    }
    if (ev_ptr->m_status == EVENT_STATUS::ES_END)
    {
        return ;
    }
    ev_ptr->m_status = EVENT_STATUS::ES_END;	
    ev_ptr->m_cur_value= curVal;
    ev_ptr->m_is_display = false;
    ev_ptr->m_cur_time = std::chrono::system_clock::now();;
    event_list::instance()->save_event(ev_ptr);
    if (evType < MAX_MINE_EVENT_NUM)
    {
        m_evlist[evType] = 0;
    }
    log_info("CMineCardManager::CancelEvent: Success .id=%%llu  evType=%d,evState=ES_END ",evid,evType);
}

/**
 * @brief 人员下井
 * @param card 卡
 **/
void CMineCardManager::OnPersonDown(std::shared_ptr<card> card,bool bInit/* = false*/)
{
    if (nullptr == card)
    {
        return ;
    }
     //设置卡的状态
    card->m_is_attendance = EAttendanceState::MineDown;
   
    uint64_t key = tool_other::type_id_to_u64(card->m_id,card->m_type);
    if (this->Exist(m_minecards->m_down_person,key))
    {
        return ;
    }   
     //移除升井中的状态
    m_minecards->m_up_person.erase(key);
    //加入到井下列表中
    m_minecards->m_down_person.insert(key);
    if(!bInit)
    {
        //井下超员判断
        this->OnPersonOvercrowdingWarning();
    }
}
    /**
 * @brief 人员升井
 * @param card 卡
 **/
void CMineCardManager::OnPersonUp(std::shared_ptr<card> card)
{
    if (nullptr == card)
    {
        return ;
    }
    card->m_is_attendance = EAttendanceState::MineUp;
    uint64_t key = tool_other::type_id_to_u64(card->m_id,card->m_type);
    if (this->Exist(m_minecards->m_up_person,key))
    {
        return ;
    }   
     //移除井下列表
    m_minecards->m_down_person.erase(key);
    //加入到升井列表中
    m_minecards->m_up_person.insert(key);
    
    int max_person = CYaSetting::m_sys_setting.over_count_person;
    //井下超员判断--取消
    if (max_person != 0 && (int)m_minecards->m_down_person.size() < max_person )
    {
        this->CancelEvent(EVENT_TYPE::ET_OVER_COUNT_PERSON,(int)m_minecards->m_down_person.size());
    }
    
}

/**
 * @brief 人员超员警告
**/
void CMineCardManager::OnPersonOvercrowdingWarning()
{
    int max_person = CYaSetting::m_sys_setting.over_count_person;
    //井下超员判断
    if (max_person != 0 && (int)m_minecards->m_down_person.size() > max_person )
    {
        std::string log = "";
        for (uint64_t id : m_minecards->m_down_person)
        {
            std::shared_ptr<card_location_base> pitcard = card_list::instance()->get(id);
            if (nullptr != pitcard)
            {
                log += tool_other::type_id_to_str(pitcard->m_type,pitcard->m_id);
                log += "&";
            }
        }
        //打印日志
        log_info("[lemon mp_card_list_down_person:]:%s",log.c_str());

        this->StartEvent(EVENT_TYPE::ET_OVER_COUNT_PERSON,max_person,(int)m_minecards->m_down_person.size());
    }
}

/**
* @brief 人员井下超时
**/
void CMineCardManager::OnPersonInMineOverTime(int card_id,int card_type)
{
    std::shared_ptr<card_location_base> card = card_list::instance()->get(tool_other::type_id_to_u64(card_type, card_id));
    if (nullptr == card)
    {
        return ;
    }

    std::shared_ptr<mine_tool> mine = card->get_mine_tool();
    if(nullptr == mine )
    {
        return ;
    }

    int sec= tool_time::to_ms(std::chrono::system_clock::now()) - mine->get_work_time();
    if (!mine->m_is_overtime && sec > (int)CYaSetting::m_sys_setting.over_time_person)
    {
        //人员超时
        mine->m_is_overtime = true;
        mine->m_overtime = std::chrono::system_clock::now();
    }
    else if (mine->m_is_overtime && sec < (int)CYaSetting::m_sys_setting.over_time_person)
    {
        mine->m_is_overtime = false;
        mine->m_overtime = std::chrono::system_clock::now();
    }
    else
    {
        return ;
    }

    // 发送警告
    event_ptr evPtr = event_list::instance()->get_event_card(card->m_id,card->m_type,ET_CARD_OVER_TIME_PERSON);
    if (nullptr == evPtr)
    {
        event_ptr evPtr = event_list::create_event_card(card->m_id, card->m_type, ET_CARD_OVER_TIME_PERSON);
        event_list::instance()->copy_event(card, evPtr);
        event_list::instance()->add(evPtr->get_list_id(), evPtr);
    }
    evPtr->m_limit_value = 0;
    evPtr->m_cur_value = 0;
    evPtr->m_is_display = false;
    evPtr->m_status = mine->m_is_overtime ? ES_START : ES_END;
    evPtr->m_cur_time = std::chrono::system_clock::now();

    event_list::save_event(evPtr);

    log_info("OnPersonInMineOverTime: send Event=ET_CARD_OVER_TIME_PERSON card=id,eventState=%d",card->m_id,evPtr->m_status);
}
    /**
 * @brief 车下井
 * @param card 卡
 **/
void CMineCardManager::OnVehicleDown(std::shared_ptr<card> card,bool bInit/* = false*/)
{
    if (nullptr == card)
    {
        return ;
    }
}
    /**
 * @brief 车升井
 * @param card 卡
 **/
void CMineCardManager::OnVehicleUp(std::shared_ptr<card> card)
{
    if (nullptr == card)
    {
        return ;
    }
}

 /**
 * @brief 车辆超员警告
 **/
void CMineCardManager::OnVehicleOvercrowdingWarning()
{

}
