

#ifndef _MEDIAF3PROTOCOL_H_
#define _MEDIAF3PROTOCOL_H_

#include "MFSPublic.h"
#include "3rd/uvw.hpp"
#include "3rd/cJSON.h"

#include "Rtp808Service.h"
#include "BaseService.h"

namespace mfs
{
    /*
{
    "data":{
        "msgHead":{
            "deviceType":"1",
            "subPackage":0,
            "encrypt":0,
            "bodySize":0,
            "mobile":"14875300001",
            "msgID":37122,
            "msgSN":7481,
            "packageSum":0,
            "type":0,
            "packageNO":0
        },
        "msgBody":{
            "closeVideoType":0,
            "channelNum":1,
            "control":0,
            "changeStreamType":0
        }
    },
    "desc":{
        "messageType":0,
        "msgID":37122,
        "sysTime":"201110093338",
        "type":"0",
        "deviceId":"86165115-f3b8-4a64-b928-5e59ace67dfc",
        "uuid":"e1b926c8-3644-455b-88fe-61f5e8c3f48f"
    }
}
    */
    enum EF3CommandID
    {
        EF3CommandID_HEARTBEAT = 0x0C07,     //  心跳
        EF3CommandID_REALTIME = 0x9101,     //  实时视频播放
        EF3CommandID_REALTIME_C = 0x9102,   //  实时视频控制
        EF3CommandID_PLAYBACK = 0x9201,     //  视频回放播放
        EF3CommandID_PLAYBACK_C = 0x9202,   //  视频回放控制
    };

    class IMediaF3ProtocolAnswer
    {
        public:
        virtual void onAnswer1078(int num, int result) = 0;
    };
    struct MessageInfo
    {
        int number;
        std::string messgae;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        std::weak_ptr<IMediaF3ProtocolAnswer> answer;
    };
    struct MessageBaseSetting
    {
        std::string simcard;
        std::string deviceID;
        std::string vehicleId;
        std::string channelNum;
        std::weak_ptr<IMediaF3ProtocolAnswer> answer;
    };

    class MediaF3Protocol:public BaseProtocol
    {
        private:
        std::vector<MessageInfo> _messagesSent;
        std::vector<MessageInfo> _messagesSending;

        bool _connected = false;
    
        std::pair<std::vector<uint8_t>, int> _readParam;
        int parseCLBSCommand();
        int executeCLBSCommand(const char* strJson);
        int getMessageID(cJSON* dataNode, int& msgID);

        MediaF3Protocol(std::shared_ptr<uvw::Loop> loop, std::shared_ptr<uvw::TCPHandle> client, const std::string& name):BaseProtocol(loop, client, name)
        {
            client->userAlloc(true);
        }

        static int GetMessageNum();
        
        void SendMessageInner(const MessageInfo& info);
        void OnAnswer(cJSON* dataNode);

        public:
        virtual void OnData(char* data, int dataLen) override;
        virtual void OnAlloc(uv_buf_t *buf, int suggested) override; 
        virtual void OnClose() override; 
        virtual void OnConnection() override; 


        /**
         * f3指令
         */
        // 9101
        static int PlayRealTime(const MessageBaseSetting& baseSetting, const std::string&  streamType, const std::string&  dataType, int port);
        // 9102
        static int ControlRealTime(const MessageBaseSetting& baseSetting, const std::string& streamType, const std::string& closeVideoType,const std::string& control);
        // 9201
        static int PlayPlayback(const MessageBaseSetting& baseSetting, const std::string& endTime, const std::string& startTime, const std::string& forwardOrRewind, const std::string& remoteMode, const std::string& streamType, const std::string& storageType, int port);
        // 9202
        static int ControlPlayback(const MessageBaseSetting& baseSetting, const std::string& dragPlaybackTime, const std::string& forwardOrRewind,const std::string& remote);

        static void Heartbeat();


        static void SendMessage(const MessageInfo& info);
        static std::shared_ptr<MediaF3Protocol> Instance();
        static void SetupOnce();
        static void SetClientID(const std::string& id);
    };
}
#endif