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

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.StringUtils;

/**
 * 进场信息
 * <a href="https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/live/message_stream.md#%E8%BF%9B%E5%9C%BA%E6%88%96%E5%85%B3%E6%B3%A8%E6%B6%88%E6%81%AF">进场</a>
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class WSInteractWord extends WSData {
    public final static int TYPE_ENTER = 1;
    public final static int TYPE_FOLLOW = 2;
    private long uid;
    private int type; //1为进场,2为关注
    private long roomId;
    private long timer;
    private String uname;
    private String uname_color;
    private String face;
    private WSUserMedal medal;


    public WSInteractWord(JSONObject json) {
        super(json);
        JSONObject data = json.getJSONObject("data");
        JSONObject medalJson = data.containsKey("fans_medal") ? data.getJSONObject("fans_medal"):null;
        type = data.getIntValue("msg_type");
        roomId = data.getLong("roomid");
        timer = data.getLong("score");
        uid = data.getLong("uid");
        uname = data.getString("uname");
        uname_color = data.getString("uname_color");
        face = data.getJSONObject("uinfo").getJSONObject("base").getString("face");
        if (medalJson != null) {
            medal = new WSUserMedal();
            medal.setUid(medalJson.getLong("anchor_roomid"));
            medal.setMedal_name(medalJson.getString("medal_name"));
            medal.setMedal_color(Integer.toHexString(medalJson.getIntValue("medal_color")));
            medal.setMedal_level(medalJson.getIntValue("medal_level"));
        }
    }

    public String getUname_color() {
        if (StringUtils.hasLength(uname_color)) {
            uname_color = "FFFFFF";
        }
        return uname_color;
    }

    @Override
    public String toString() {
        return "WSInteractWord{" +
                "type=" + type +
                ", roomId=" + roomId +
                ", timer=" + timer +
                ", medal=" + medal +
                ", uid=" + uid +
                ", uname='" + uname + '\'' +
                ", uname_color='" + uname_color + '\'' +
                ", face='" + face + '\'' +
                ", cmd='" + cmd + '\'' +
                ", jsonSrc='" + jsonSrc + '\'' +
                ", ws_timer=" + ws_timer +
                '}';
    }
}
