
#include <fstream>
// #include <uvw.hpp>
// #include "Rtp808.h"
#include "MediaInfoService.h"

namespace mfs
{
    static MediaInfoService* _instance = nullptr;

    MediaInfoService* MediaInfoService::Instance()
    {
        return _instance;
    }

    MediaInfoService* MediaInfoService::SetupOnce(int port, std::shared_ptr<uvw::Loop> loop)
    {
        if (_instance)
        {
            delete _instance;
            _instance = nullptr;
        }
        
        _instance = new MediaInfoService("MediaInfoService", port, loop);
        return _instance;
    }

    int MediaInfoService::sendFlowInfo(Rtp808Protocol* rtpClient, int endFlag)
    {
        if(rtpClient->getUuid().size() > 0)
        {
            auto typeName = rtpClient->getType();
            int type = IsContainsStr(typeName,"RealStream") ? 0 : (IsContainsStr(typeName,"Talkback") ? 1 : 2);
            std::string flowJson = "{\"data\": {\"msgHead\": {\"msgID\": 65501}, \"msgBody\": {\"mobile\": \"" + rtpClient -> getSimNumber() + "\", \"channelNumber\": " + 
                                    std::to_string(rtpClient -> getChannel()) + ", \"type\": " + std::to_string(type) + ", \"use\": " + 
                                    std::to_string(rtpClient -> getCurrentFlow()) + ", \"vehicleId\": \"" + rtpClient -> getUuid() + 
                                    "\", \"endTime\": \"" + getNowStr() + 
                                    "\", \"startTime\": \"" + rtpClient -> getStartTime() + "\", \"endFlag\": " + std::to_string(endFlag) + "}}}";
            uint32_t size = flowJson.size();
            std::vector<uint8_t> sendData(size + 4);
            uint32_t sizeBigEndian = swapBigEndian(size);
            memcpy(sendData.data(), &sizeBigEndian, 4);
            memcpy(sendData.data() + 4, &flowJson[0], size);
            for (auto iter = _clients.begin(); iter != _clients.end(); iter++)
            {
                (*iter)->Write((char*)sendData.data(), sendData.size());
            }
            return 0;
        }
        return -1;
    }
    void MediaInfoService::sendFlowInfo(const StreamInfo& info)
    {
        std::string strjson = std::string()+"{\"data\":{\"msgHead\":{\"msgID\":65601},\"msgBody\":{	" +
            "\"targetID\":"+info.targetID+","   +
            "\"targetType\":"+info.targetType+","      +
"\"ip\":\""+info.ip+"\","                       +
"\"port\":"+info.port+","                       +
"\"state\":"+info.state+","                     +
"\"timestampStart\":"+info.timestampStart+","   +
"\"timestampEnd\":"+info.timestampEnd+","       +
"\"message\":\""+info.message+"\","             +
"\"sendBytes\":"+info.sendBytes+","             +
"\"recvBytes\":"+info.recvBytes+","             +
"\"simNumber\":\""+info.simNumber+"\","         +
"\"channel\":"+info.channel+","                 +
"\"uuid\":\""+info.uuid+"\""                    +
"}}}";
        uint32_t size = strjson.size();
        std::vector<uint8_t> sendData(size + 4);
        uint32_t sizeBigEndian = swapBigEndian(size);
        memcpy(sendData.data(), &sizeBigEndian, 4);
        memcpy(sendData.data() + 4, strjson.data(), size);
        for (auto iter = _clients.begin(); iter != _clients.end(); iter++)
        {
            (*iter)->Write((char*)sendData.data(), sendData.size());
        }


        // 添加文件输出备份
        
    }
    /**
     * 
struct StreamInfo2
{
    std::string playType;			// 播放类型(实时、回放、对讲、侦听、广播)
    std::string targetType;			// 客户端类型（手机、浏览器）
    std::string vehicleId;			// 车辆id
    std::string simcardNumber;		// 终端手机卡号
    std::string channelNumber;		// 终端通道号
    std::string sendBytes;			// 终端发送到服务器数据的字节数
    std::string recvBytes;			// 服务器发送到终端数据的字节数
    std::string startTime;			// 开始连接时间
    std::string endTime;			// 断开连接时间
    std::string uuid;				// 唯一标识
    std::string userID;				// 用户ID
};
     */
    void MediaInfoService::sendUserStart(const StreamInfo& info)
    {
        std::string strjson = std::string()+"{\"data\":{\"msgHead\":{\"msgID\":51003},\"msgBody\":{	" +
            "\"playType\":\""+info.playType+"\","   +
            "\"targetType\":\""+info.targetType+"\","      +
            "\"vehicleId\":\""+info.vehicleId+"\","                     +
            "\"simcardNumber\":\""+info.simNumber+"\","   +
            "\"channelNumber\":\""+info.channel+"\","       +
            "\"startTime\":\""+info.timestampStartData+"\","         +
            "\"uuid\":\""+info.uuid+"\","                    +
            "\"userID\":\""+info.userID+"\""                    +
            "}}}";
        LOG_DEBUG<<strjson<<LOG_ENDL;
        uint32_t size = strjson.size();
        std::vector<uint8_t> sendData(size + 4);
        uint32_t sizeBigEndian = swapBigEndian(size);
        memcpy(sendData.data(), &sizeBigEndian, 4);
        memcpy(sendData.data() + 4, strjson.data(), size);
        for (auto iter = _clients.begin(); iter != _clients.end(); iter++)
        {
            (*iter)->Write((char*)sendData.data(), sendData.size());
        }

    }
    void MediaInfoService::sendUserEnd(const StreamInfo& info)
    {
        std::string strjson = std::string()+"{\"data\":{\"msgHead\":{\"msgID\":51002},\"msgBody\":{	" +
            "\"playType\":\""+info.playType+"\","   +
            "\"targetType\":\""+info.targetType+"\","      +
            "\"vehicleId\":\""+info.vehicleId+"\","                     +
            "\"simcardNumber\":\""+info.simNumber+"\","   +
            "\"channelNumber\":\""+info.channel+"\","       +
            "\"sendBytes\":"+info.sendBytes+","             +
            "\"recvBytes\":"+info.recvBytes+","             +
            "\"startTime\":\""+info.timestampStartData+"\","         +
            "\"endTime\":\""+info.timestampEnd+"\","                 +
            "\"uuid\":\""+info.uuid+"\","                    +
            "\"userID\":\""+info.userID+"\""                    +
            "}}}";
        LOG_DEBUG<<strjson<<LOG_ENDL;
        uint32_t size = strjson.size();
        std::vector<uint8_t> sendData(size + 4);
        uint32_t sizeBigEndian = swapBigEndian(size);
        memcpy(sendData.data(), &sizeBigEndian, 4);
        memcpy(sendData.data() + 4, strjson.data(), size);
        for (auto iter = _clients.begin(); iter != _clients.end(); iter++)
        {
            (*iter)->Write((char*)sendData.data(), sendData.size());
        }

    }
    void MediaInfoService::sendDeviceEnd(const StreamInfo& info)
    {
        std::string strjson = std::string()+"{\"data\":{\"msgHead\":{\"msgID\":51001},\"msgBody\":{	" +
            "\"playType\":\""+info.playType+"\","   +
            "\"vehicleId\":\""+info.vehicleId+"\","                     +
            "\"simcardNumber\":\""+info.simNumber+"\","   +
            "\"channelNumber\":\""+info.channel+"\","       +
            "\"sendBytes\":"+info.sendBytes+","             +
            "\"recvBytes\":"+info.recvBytes+","             +
            "\"startTime\":\""+info.timestampStart+"\","         +
            "\"endTime\":\""+info.timestampEnd+"\","                 +
            "\"uuid\":\""+info.uuid+"\","                    +
            "\"userID\":\""+info.userID+"\""                    +
            "}}}";
        LOG_DEBUG<<strjson<<LOG_ENDL;
        uint32_t size = strjson.size();
        std::vector<uint8_t> sendData(size + 4);
        uint32_t sizeBigEndian = swapBigEndian(size);
        memcpy(sendData.data(), &sizeBigEndian, 4);
        memcpy(sendData.data() + 4, strjson.data(), size);
        for (auto iter = _clients.begin(); iter != _clients.end(); iter++)
        {
            (*iter)->Write((char*)sendData.data(), sendData.size());
        }

    }
}
