#ifndef COMMON_TOOL_H
#define COMMON_TOOL_H
#include <chrono>
#include <string>
#include <sys/time.h>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/algorithm/string.hpp>
#include "websocket/sio/sio_client.h"
#include "common.h"

class tool_other
{
    public:
        static double round(double dVal, int iPlaces)
        {
            double dRetval;
            double dMod = 0.0000001;
            if (dVal<0.0) dMod=-0.0000001;
            dRetval=dVal;
            dRetval+=(5.0/pow(10.0,iPlaces+1.0));
            dRetval*=pow(10.0,iPlaces);
            dRetval=floor(dRetval+dMod);
            dRetval/=pow(10.0,iPlaces);
            return(dRetval);
        }
        
        static std::string to13str(std::string& str)
        {
            if(str.size()<13)
              str.insert(0,13-str.size(),'0');
            return str;
        }

        static std::string to_cid(int ctype, int cid)
        {
            char buf[20] = {0};
            sprintf(buf, "%.3d%.10d", ctype, cid);

            return std::string(buf);
        }

        static std::string to13str(uint64_t data)
        {
            char ss[20]={0};
            sprintf(ss, "%013ld", data);

            return std::string(ss);
        }

        static uint32_t id64_to_id(std::string& str)
        {
            return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
        }

        static int id64_to_type(std::string& str)
        {
            return std::stoi(to13str(str).substr(0, 3));
        }
        
        static int card_id_to_type(const std::string &cardid)
        {
            return std::stoi(cardid.substr(0,3));
        }
        
        static int card_id_to_id(const std::string &cardid)
        {
            return atoi(cardid.substr(3).c_str());
        }
        
        static std::string type_id_to_str(int32_t type,uint32_t id)  
        {
            char sql[15] = {0};
            snprintf(sql, 15,"%03d%010d", type, id);
            return std::string(sql);
        }
        
        static uint64_t type_id_to_u64(uint64_t type,uint32_t id)
        {
            return type<<32|id;
        }
        
        static bool is_person(int32_t type)
        {
            return CT_PERSON == type;
        }

        static bool is_vehicle(int32_t type)
        {
            return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
        }
        
        static std::string get_string_cardid(uint64_t id)
        {
            uint64_t type = id>>32;
            uint32_t cid = id & (~(type<<32));
            return type_id_to_str(type,cid);
        }

        static uint64_t card_id_to_u64(const std::string & cardid)
        {
            return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
        }

        static bool is_coal(int32_t type,int32_t vtype)
        {
            return CT_COAL_CUTTER == type || vtype==VT_COAL_CUTTER;
        }
        
        static bool is_driving(int32_t type,int32_t vtype)
        {
            return CT_HEADING_MACHINE == type || vtype==VT_HEADING_MACHINE;
        }
        
        static bool is_coal_or_driving(int32_t type,int32_t vtype)
        {
            return is_coal(type,vtype) || is_driving(type,vtype);
        }

        static double get_pdoa(float poa[], const  double& offset)
        {
            if(poa == nullptr)
            {
                return -10.0;
            }

            float pdoa = poa[1] - poa[0] - offset;
            while(pdoa >= TPI){
                pdoa -= TPI;
            }

            while(pdoa < 0){
                pdoa += TPI;
            }

            pdoa -= PI;

            return pdoa;
        }

        template<class T>
        static void memcpy_int(std::vector<char>& msg, T val)
        {
            for(int i = sizeof(T)-1; i >=0; --i){
                msg.push_back((val>>(i*8))&0xFF);
            }
        }

        /*
         * @brief
         * @param
         * @return
         * @note
         * @warning
         * @bug
         * */
        static std::string replace(std::string val, std::string d, std::string n)
        {
            boost::replace_all(val, d, n);
            return val;
        }
};

class tool_map
{
    public:
        static bool try_get_value(sio::message::ptr& out_data,
                    const char* key, sio::message::ptr const& data)
        {
            auto map=data->get_map()[key];
            if(map && sio::message::flag_object == map->get_flag())
            {
                out_data = map;
                return true;
            }

            return false;
        }

        static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
        {
            auto map=data->get_map()[key];
            if(map && sio::message::flag_integer == map->get_flag())
            {
                out_data = map->get_int();
                return true;
            }

            return false;
        }

        static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
        {
            auto map=data->get_map()[key];
            if(map && sio::message::flag_integer == map->get_flag())
            {
                out_data = static_cast<uint32_t>(map->get_int());
                return true;
            }

            return false;
        }

        static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
        {
            auto map=data->get_map()[key];
            if(map && sio::message::flag_integer == map->get_flag())
            {
                out_data = static_cast<int>(map->get_int());
                return true;
            }

            return false;
        }

        static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
        {
            auto map=data->get_map()[key];
            if(map && sio::message::flag_string == map->get_flag())
            {
                out_data = map->get_string();
                return true;
            }

            return false;
        }

        static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
        {
            auto map=data->get_map()[key];
            if(map && sio::message::flag_double == map->get_flag())
            {
                out_data = map->get_double();
                return true;
            }

            return false;
        }

        static bool try_get_value(std::vector<sio::message::ptr>& out_data,
                    const char* key, sio::message::ptr const& data)
        {
            auto map=data->get_map()[key];
            if(map && sio::message::flag_array == map->get_flag())
            {
                out_data = map->get_vector();
                return true;
            }

            return false;
        }
};

class tool_json
{
    public:
        static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
                    rapidjson::Document::AllocatorType& allocator)
        {
            rapidjson::Value name;
            name.SetString(key, allocator);

            rapidjson::Value data;
            data.SetString(value.c_str(), allocator);

            out_data.AddMember(name, data, allocator);
        }

        static void push_back(rapidjson::Value& out_data, std::string value,
                    rapidjson::Document::AllocatorType& allocator)
        {
            rapidjson::Value data;
            data.SetString(value.c_str(), allocator);

            out_data.PushBack(data, allocator);
        }

        static bool try_get_iter(const char* key, const rapidjson::Value& node,
                    rapidjson::Value::ConstMemberIterator& out_iter)
        {
            if(node.IsObject())
            {
                out_iter = node.FindMember(key);
                if(node.MemberEnd() == out_iter)
                {
                    return false;
                }

                return true;
            }

            return false;
        }

        static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
        {
            rapidjson::Value::ConstMemberIterator iter;
            if(try_get_iter(key, node, iter))
            {
                if(iter->value.IsInt())
                {
                    d = iter->value.GetInt();
                    return true;
                }
            }

            return false;
        }

        static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
        {
            rapidjson::Value::ConstMemberIterator iter;
            if(try_get_iter(key, node, iter))
            {
                if(iter->value.IsUint64())
                {
                    d = iter->value.GetUint64();
                    return true;
                }
            }
            return false;
        }

        static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
        {
            rapidjson::Value::ConstMemberIterator iter;
            if(try_get_iter(key, node, iter))
            {
                if(iter->value.IsDouble())
                {
                    d = iter->value.GetDouble();
                    return true;
                }
            }
            return false;
        }

        static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
        {
            rapidjson::Value::ConstMemberIterator iter;
            if(try_get_iter(key, node, iter))
            {
                if(iter->value.IsString())
                {
                    d = iter->value.GetString();
                    return true;
                }
            }

            return false;
        }

        static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
        {
            rapidjson::Value::ConstMemberIterator iter;
            if(try_get_iter(key, node, iter))
            {
                if(iter->value.IsInt())
                {
                    return  iter->value.GetInt();
                }
            }

            return default_data;
        }

        static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
        {
            rapidjson::Value::ConstMemberIterator iter;
            if(try_get_iter(key, node, iter))
            {
                if(iter->value.IsString())
                {
                    return iter->value.GetString();
                }
            }

            return default_data;
        }

        static std::string doc_to_json(rapidjson::Document& doc)
        {
            rapidjson::StringBuffer sb;
            rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
            doc.Accept(writer);

            return sb.GetString();
        }
};

#endif // COMMON_TOOL_H