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

import com.alibaba.fastjson2.JSONObject;
import com.yutou.common.inter.ISqlDatabaseBean;
import lombok.Data;

import java.io.Serializable;
@Data
public class WSData implements Serializable, ISqlDatabaseBean {
    public String cmd;
    public String jsonSrc;
    public long ws_timer;

    @Deprecated
    public WSData() {
        throw new NullPointerException("需要传入json");
    }

    @Override
    public String toString() {
        return "WSData{" +
                "cmd='" + cmd + '\'' +
                ", jsonSrc='" + jsonSrc + '\'' +
                '}';
    }

    public WSData(JSONObject json) {
        this.cmd = json.getString("cmd");
        ws_timer = System.currentTimeMillis();
        this.jsonSrc = json.toString();
    }

    public static WSData parse(JSONObject json) {
        String cmd = json.getString("cmd");
        return switch (cmd) {
            case "DANMU_MSG" ->  new WSDanmuData(json);
            case "DM_INTERACTION" -> new WSDmInteraction(json);
            case "SEND_GIFT" -> new WSSendGift(json);
            case "INTERACT_WORD" -> new WSInteractWord(json);
            case "GUARD_BUY" -> new WSGuardBuy(json);
            case "SUPER_CHAT_MESSAGE" -> new WSSuperChatMessage(json);
            default -> new WSData(json);
        };
    }

    @Override
    public String getJson(){
        return jsonSrc;
    }
}
