#ifndef COMMON_TOOL_H
#define COMMON_TOOL_H

/**
 * @brief 基楚的工具类
 * @author 戴月腾
 * @date 2018-09-15
 */


#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 "websocket/sio/sio_client.h"
#include "common.h"


class tool_other
{
public:

    static std::string to13str(std::string& str)
    {
        uint64_t tmp = std::stoull(str);
        return to13str(tmp);
    }

    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));
	}
};

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
