#include"area_business_motionless_persion.h"


#include"common_tool.h"
#include"log.h"
#include"area.h"
#include"event.h"
#include"tool_time.h"
#include"area_business.h"
#include"card.h"
#include"config_file.h"


struct motionless_data:business_data
{
    motionless_data()
        :m_acc_0count(0)
        ,m_is_warning(false)
    {
        //        m_acc_0count=0;
        //        m_acc_start_time=0;
    }

    ///;检测到多少次之后定位完全静止
    //   std::atomic<int> m_acc_0count;
    //    ///检测到多久之后告警
    //    time_t m_acc_start_time;

    int m_acc_0count; //连续静止数量
    bool m_is_warning;  //
};

void area_business_motionless_persion::init(config_file& config)
{
    //       sleep_ms = std::stoi(config.get("service.motionless_thread_sleep_ms","5000"));
    _acc_0count_limit = std::stoi(config.get("service.motionless_acc_0count_limit","40"));
    //        _acc_seconds_limit = std::stoi(config.get("service.motionless_acc_seconds_limit","120"));
}

void area_business_motionless_persion::on_load_his(const std::shared_ptr<area_hover>&area_hover_ptr,
                                            const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
{
    on_enter(area_hover_ptr, card_ptr, ptr);
}

void area_business_motionless_persion::on_enter(const std::shared_ptr<area_hover>&area_hover_ptr,
                                                const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
{
    if(!card_ptr->is_person())
    {
        return;
    }

    auto ptr_temp = std::make_shared<motionless_data>();
    ptr = ptr_temp;
    area_hover_ptr->m_data[area_business_type()] = ptr_temp;

    auto ev_ptr_temp = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_CARD_MOTIONLESS);
    ptr_temp->m_is_warning = (nullptr != ev_ptr_temp && !ev_ptr_temp->is_end());
}

void area_business_motionless_persion::on_hover(const std::shared_ptr<area_hover>&area_hover_ptr,
                                                const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
{
    if(!card_ptr->is_person())
    {
        return;
    }

    if(nullptr == ptr)
    {
        log_error("area_business_motionless_persion::on_hover：nullptr == ptr");
        return;
    }

    auto ptr_temp = static_cast<motionless_data*>(ptr.get());

    if(0 == static_cast<int>(card_ptr->m_acc))
    {
        ++ptr_temp->m_acc_0count;
    }
    else
    {
        ptr_temp->m_acc_0count=0;
    }

    if(ptr_temp->m_acc_0count >= _acc_0count_limit)
    {
        ptr_temp->m_acc_0count = _acc_0count_limit;
        if(!ptr_temp->m_is_warning)
        {
            ptr_temp->m_is_warning = true;
            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_MOTIONLESS, id, 0, 0, true);
        }
    }
    else
    {
        if(ptr_temp->m_is_warning)
        {
            ptr_temp->m_is_warning = false;
            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_MOTIONLESS, id, 0, 0, false);
        }
    }
}

void area_business_motionless_persion::on_leave(const std::shared_ptr<area_hover>&area_hover_ptr,
                                                const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
{
    if(!card_ptr->is_person())
    {
        return;
    }

    if(nullptr == ptr)
    {
        log_error("area_business_motionless_persion::on_leave：nullptr == ptr");
        return;
    }

    auto ptr_temp = static_cast<motionless_data*>(ptr.get());
    if(ptr_temp->m_is_warning)//如果正在告警，则发送取消告警的信息
    {
        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_MOTIONLESS, id, 0, 0, false);
    }
}

int area_business_motionless_persion::_acc_0count_limit=20;

//void module_motionless_persion::deal_alarm(std::shared_ptr<card_location_base>& card_ptr)
//{
//    auto area_hover_ptr = card_ptr->get_area_hover();
//    if(!area_hover_ptr)
//    {
//        log_error("if(!area_hover_ptr)==true");
//        return;
//    }

//    auto arr = area_hover_ptr->m_data;
//    int index = 1;

//    if(arr.size()<2 || !arr[index])
//    {
//        return;
//    }

//    auto ptr = static_cast<motionless_data*>(area_hover_ptr->m_data[index].get());

//    if(ptr->m_acc_0count >= _acc_0count_limit)
//    {
//        ptr->m_acc_0count = _acc_0count_limit;
//        if(0 == ptr->m_acc_start_time)
//        {
//            ptr->m_acc_start_time = tool_time::now_to_seconds();
//        }
//    }
//    else
//    {
//        ptr->m_acc_start_time=0;
//    }

//    bool is_alarm = (0 != ptr->m_acc_start_time
//            &&tool_time::elapse_seconds(ptr->m_acc_start_time)> _acc_seconds_limit);

//    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_MOTIONLESS, id, 0, 0, is_alarm);
//}




