package com.tencent.liteav.live.model.impl.room.impl;

import static com.tencent.liteav.live.model.impl.room.impl.IMProtocol.Define.VALUE_PROTOCOL_VERSION;

import android.util.Pair;

import com.blankj.utilcode.util.TimeUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class IMProtocol {

    public static class Define {
        public static final String KEY_VERSION = "version";
        public static final String KEY_ACTION = "action";
        public static final String VALUE_PROTOCOL_VERSION = "1.0.0";

        public static final int CODE_UNKNOWN = 0;

        public static final int CODE_ROOM_TEXT_MSG = 300;
        public static final int CODE_ROOM_CUSTOM_MSG = 301;
        public static final int CODE_ROOM_LINK = 66601;
        public static final int CODE_ROOM_LINK_OK = 66602;
        public static final int CODE_ROOM_LINK_CANCEL = 66603;
        public static final int CODE_ROOM_LINK_CLOSE = 66609;
    }

    public static String getRoomTextMsgHeadJsonStr() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(Define.KEY_VERSION, VALUE_PROTOCOL_VERSION);
            jsonObject.put(Define.KEY_ACTION, Define.CODE_ROOM_TEXT_MSG);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }

    public static String getCusMsgJsonStr2(String key, String to) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(Define.KEY_VERSION, VALUE_PROTOCOL_VERSION);
            jsonObject.put(Define.KEY_ACTION, key);
            jsonObject.put("time", TimeUtils.getNowMills());
            jsonObject.put("to", to);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }

    public static String getCusMsgJsonStr2(String key, long time, String to) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(Define.KEY_VERSION, VALUE_PROTOCOL_VERSION);
            jsonObject.put(Define.KEY_ACTION, key);
            jsonObject.put("time", time);
            jsonObject.put("to", to);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }

    public static String getCusMsgJsonStr(String cmd, String msg) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(Define.KEY_VERSION, VALUE_PROTOCOL_VERSION);
            jsonObject.put(Define.KEY_ACTION, Define.CODE_ROOM_CUSTOM_MSG);
            jsonObject.put("command", cmd);
            jsonObject.put("message", msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }

    public static Pair<String, String> parseCusMsg(JSONObject jsonObject) {
        String cmd = jsonObject.optString("command");
        String message = jsonObject.optString("message");
        return new Pair<>(cmd, message);
    }
}
