package com.yutou.biliapi.bean.websocket.live;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.common.utils.Log;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigInteger;

/**
 * 弹幕信息
 * <a href="https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/live/message_stream.md#%E5%BC%B9%E5%B9%95">弹幕</a>
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class WSDanmuData extends WSData {
    private String dm_v2;
    private int id;
    private int model;// 1~3 滚动弹幕 4 底端弹幕 5 顶端弹幕 6 逆向弹幕 7 精准定位 8 高级弹幕
    private int fontSize;
    private String fontColor;
    private long time;
    private String uCode;
    private String danmu;
    private String uid;
    private String uname;
    private WSUserMedal medal;

    public WSDanmuData(JSONObject json) {
        super(json);
        JSONArray infoData = json.getJSONArray("info");
        setModel(infoData.getJSONArray(0).getInteger(1));
        setFontSize(infoData.getJSONArray(0).getInteger(2));
        setFontColor(Integer.toHexString(infoData.getJSONArray(0).getInteger(3)));
        setTime(infoData.getJSONArray(0).getLong(4));
        setUCode(infoData.getJSONArray(0).getString(7));
        setDanmu(infoData.getString(1));
        setUid(infoData.getJSONArray(2).getFirst().toString());
        setUname(infoData.getJSONArray(2).getString(1));
        try {
            medal = WSUserMedal.create(infoData.getJSONArray(3));
        } catch (Exception e) {
            Log.i("弹幕信息解析失败：" + json);
        }
    }

    @Override
    public String toString() {
        return "弹幕 = " + "用户：" + getUname() + " 发送了: " + getDanmu() +" | json = "+jsonSrc;
    }
}
