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.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;

/**
 * 创建日期：2022/6/14 10:56
 *
 * @author Ryan
 * 类说明：
 */
@MessageTag(value = "RC:CHATSHARE:MUSIC", flag = MessageTag.ISCOUNTED)
public class RCChatShareMusicMessage extends MessageContent {
    private String songName;
    private String chargeType;
    private String songAuth;
    private String teacherAvatar;
    private String teacherName;
    private String musicTagNames;
    private String songId;

    public String getSongName() {
        return songName;
    }

    public void setSongName(String songName) {
        this.songName = songName;
    }

    public String getChargeType() {
        return chargeType;
    }

    public void setChargeType(String chargeType) {
        this.chargeType = chargeType;
    }

    public String getSongAuth() {
        return songAuth;
    }

    public void setSongAuth(String songAuth) {
        this.songAuth = songAuth;
    }

    public String getTeacherAvatar() {
        return teacherAvatar;
    }

    public void setTeacherAvatar(String teacherAvatar) {
        this.teacherAvatar = teacherAvatar;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public String getMusicTagNames() {
        return musicTagNames;
    }

    public void setMusicTagNames(String musicTagNames) {
        this.musicTagNames = musicTagNames;
    }

    public String getSongId() {
        return songId;
    }

    public void setSongId(String songId) {
        this.songId = songId;
    }

    public RCChatShareMusicMessage() {
    }

    public RCChatShareMusicMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            if (jsonObject.has("songName")) {
                songName = jsonObject.optString("songName");
            }
            if (jsonObject.has("chargeType")) {
                chargeType = jsonObject.optString("chargeType");
            }
            if (jsonObject.has("songAuth")) {
                songAuth = jsonObject.optString("songAuth");
            }
            if (jsonObject.has("teacherAvatar")) {
                teacherAvatar = jsonObject.optString("teacherAvatar");
            }
            if (jsonObject.has("teacherName")) {
                teacherName = jsonObject.optString("teacherName");
            }
            if (jsonObject.has("musicTagNames")) {
                musicTagNames = jsonObject.optString("musicTagNames");
            }
            if (jsonObject.has("songId")) {
                songId = jsonObject.optString("songId");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();
        try {
            // 消息携带用户信息时, 自定义消息需添加下面代码
            if (!TextUtils.isEmpty(songName)) {
                jsonObj.putOpt("songName", songName);
            }
            if (!TextUtils.isEmpty(chargeType)) {
                jsonObj.putOpt("chargeType", chargeType);
            }
            if (!TextUtils.isEmpty(songAuth)) {
                jsonObj.putOpt("songAuth", songAuth);
            }
            if (!TextUtils.isEmpty(teacherAvatar)) {
                jsonObj.putOpt("teacherAvatar", teacherAvatar);
            }
            if (!TextUtils.isEmpty(teacherName)) {
                jsonObj.putOpt("teacherName", teacherName);
            }
            if (!TextUtils.isEmpty(musicTagNames)) {
                jsonObj.putOpt("musicTagNames", musicTagNames);
            }
            if (!TextUtils.isEmpty(songId)) {
                jsonObj.putOpt("songId", songId);
            }
        } 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.songName);
        dest.writeString(this.chargeType);
        dest.writeString(this.songAuth);
        dest.writeString(this.teacherAvatar);
        dest.writeString(this.teacherName);
        dest.writeString(this.musicTagNames);
        dest.writeString(this.songId);
        ParcelUtils.writeToParcel(dest, getExtra());
        ParcelUtils.writeToParcel(dest, getUserInfo());
    }

    protected RCChatShareMusicMessage(Parcel in) {
        this.songName = in.readString();
        this.chargeType = in.readString();
        this.songAuth = in.readString();
        this.teacherAvatar = in.readString();
        this.teacherName = in.readString();
        this.musicTagNames = in.readString();
        this.songId = in.readString();
        this.setExtra(ParcelUtils.readFromParcel(in));
        this.setUserInfo((UserInfo)ParcelUtils.readFromParcel(in, UserInfo.class));
    }

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

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