#include"module_area_over_speed.h"

#include<memory>
#include<string>

#include"area_business.h"
#include"event.h"
#include"log.h"
#include"common_tool.h"
#include"area.h"


struct over_speed_data:business_data
{
    over_speed_data()
    {
        m_over_speed_count=0;
        m_normal_speed_count=0;
    }

    int m_over_speed_count;
    int m_normal_speed_count;
};

void module_area_over_speed::on_enter(std::shared_ptr<card_location_base> card_ptr, int index)
{
    auto area_hover_ptr = card_ptr->get_area_hover();
    if(!area_hover_ptr)
    {
        log_error("if(!area_hover_ptr)==true");
        return;
    }

    area_hover_ptr->m_data[index] = std::make_shared<over_speed_data>();

    double limit = area_hover_ptr->m_area->m_over_speed_vehicle;
    if(limit < card_ptr->m_speed)
    {
        auto ptr = static_cast<over_speed_data*>(area_hover_ptr->m_data[index].get());
        ptr->m_over_speed_count++;
    }
}

void module_area_over_speed::on_hover(std::shared_ptr<card_location_base> card_ptr, int index)
{
    auto area_hover_ptr = card_ptr->get_area_hover();
    if(!area_hover_ptr)
    {
        log_error("if(!area_hover_ptr)==true");
        return;
    }
    auto ptr = static_cast<over_speed_data*>(area_hover_ptr->m_data[index].get());

    double limit = area_hover_ptr->m_area->m_over_speed_vehicle;
    if(limit < card_ptr->m_speed)//超速
    {
        ptr->m_over_speed_count++;
    }
    else//速度正常
    {
        ptr->m_normal_speed_count++;
    }

    //确定告警
    if(SPEED_COUNT_LIMIT <= ptr->m_over_speed_count)
    {
        ptr->m_over_speed_count=SPEED_COUNT_LIMIT;

        auto ev_ptr = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_CARD_AREA_OVER_SPEED);
        if(!ev_ptr)//从没有告警状态转化为告警状态
        {
            ptr->m_normal_speed_count=0;
        }

        uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
        event_tool::instance()->handle_event(OT_CARD, ET_CARD_AREA_OVER_SPEED, id, limit, card_ptr->m_speed, true);
    }

    //确定正常
    if(SPEED_COUNT_LIMIT <= ptr->m_normal_speed_count)
    {
        ptr->m_normal_speed_count=SPEED_COUNT_LIMIT;

        auto ev_ptr = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_CARD_AREA_OVER_SPEED);
        if(ev_ptr && !ev_ptr->is_end())
        {
            ptr->m_over_speed_count=0;

            uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
            event_tool::instance()->handle_event(OT_CARD, ET_CARD_AREA_OVER_SPEED, id, limit, card_ptr->m_speed, false);
        }
    }
}

void module_area_over_speed::on_leave(std::shared_ptr<card_location_base> card_ptr, int index)
{
    auto area_hover_ptr = card_ptr->get_area_hover();
    if(!area_hover_ptr)
    {
        log_error("if(!area_hover_ptr)==true");
        return;
    }

    double limit = area_hover_ptr->m_area->m_over_speed_vehicle;

    uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
    event_tool::instance()->handle_event(OT_CARD, ET_CARD_AREA_OVER_SPEED, id, limit, card_ptr->m_speed, false);

    area_hover_ptr->m_data[index] = std::make_shared<over_speed_data>();
}


