package io.rong.imkit.widget;

import android.os.Parcel;
import android.text.TextUtils;

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

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.MessageContent;

@MessageTag(value = "DY:NoticeMsg", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)
public class CustomMessage extends MessageContent {
    private static final String TAG = "CustomMessageTAG";
    // 这两个参数用来拼装默认消息的标题
    // 区分合并消息是在群聊里还是单聊里
    private Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
    //群公告
    private String GroupOfAnnouncement;

    public Conversation.ConversationType getConversationType() {
        return conversationType;
    }

    public void setConversationType(Conversation.ConversationType conversationType) {
        this.conversationType = conversationType;
    }

    public String getGroupOfAnnouncement() {
        return GroupOfAnnouncement;
    }

    public void setGroupOfAnnouncement(String groupOfAnnouncement) {
        GroupOfAnnouncement = groupOfAnnouncement;
    }

    protected CustomMessage() {
    }

    public static CustomMessage obtain(String context) {
        CustomMessage model = new CustomMessage();
        model.setExtra(context);
        return model;
    }

    public CustomMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
            RLog.e(TAG, "接续消息json" + jsonStr);
        } catch (UnsupportedEncodingException e) {
            RLog.e(TAG, "UnsupportedEncodingException", e);
        }
        if (jsonStr == null) {
            RLog.e(TAG, "jsonStr is null ");
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            if (jsonObject.has("content")) {
                //设置群公告
                setExtra(jsonObject.getString("content"));

            }
        }catch (Exception e) {

        }




    }

    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();
        RLog.e(TAG, "序列化消息内容");
        try {
            if (!TextUtils.isEmpty(getExtra())) {
                jsonObj.put("content", getExtra());
            }
        } catch (JSONException e) {
            RLog.e(TAG, "JSONException " + e.getMessage());
        }
        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            RLog.e(TAG, "UnsupportedEncodingException", e);
        }
        return null;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, getExtra());

    }

    /**
     * 构造函数。
     *
     * @param in 初始化传入的 Parcel。
     */
    private CustomMessage(Parcel in) {
        setExtra(ParcelUtils.readFromParcel(in));

    }

    public static final Creator<CustomMessage> CREATOR = new Creator<CustomMessage>() {
        @Override
        public CustomMessage createFromParcel(Parcel source) {
            return new CustomMessage(source);
        }

        @Override
        public CustomMessage[] newArray(int size) {
            return new CustomMessage[size];
        }
    };

}
