package com.rong.io.live.message;

import android.os.Parcel;
import android.util.Log;

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

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MentionedInfo;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;

/**
 * Author by pq, Date on 2022/4/1.
 * 直播购物车商品变化
 */
@MessageTag(value = "DY:LIVE_GOODS_CHANGE", flag = MessageTag.STATUS)
public class RCLiveGoodsChangeMessage extends MessageContent {
    private String goodsContent;


    public RCLiveGoodsChangeMessage() {
    }

    public String getContent() {
        return goodsContent;
    }

    public void setContent(String str) {
        this.goodsContent = str;
    }

    public RCLiveGoodsChangeMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
            Log.i("pq", "json:" + jsonStr);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);

            if (jsonObject.has("user")) {
                setUserInfo(this.parseJsonToUserInfo(jsonObject.getJSONObject("user")));
            }

            if (jsonObject.has("content")) {
                JSONArray jsonArray = jsonObject.optJSONArray("content");
                goodsContent = jsonArray.toString();
            }
            Log.i("pq", "goodsContent:" + goodsContent);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();
        try {
            // 消息携带用户信息时, 自定义消息需添加下面代码
            JSONObject jsonUserInfo = this.getJSONUserInfo();
            if (jsonUserInfo != null) {
                jsonObj.putOpt("user", jsonUserInfo);
            }

            jsonObj.putOpt("content", goodsContent);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.goodsContent);
        ParcelUtils.writeToParcel(dest, getExtra());
        ParcelUtils.writeToParcel(dest, getUserInfo());
    }

    protected RCLiveGoodsChangeMessage(Parcel in) {
        this.goodsContent = in.readString();
        this.setExtra(ParcelUtils.readFromParcel(in));
        this.setUserInfo((UserInfo) ParcelUtils.readFromParcel(in, UserInfo.class));
    }

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

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