package com.yutou.biliapi.databases;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.bean.live.LiveRoomConfig;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.common.databases.AbsDatabasesBean;
import com.yutou.common.databases.SQLiteManager;
import org.springframework.util.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class BiliLiveConfigDatabase extends SQLiteManager {
    String fileName;

    public BiliLiveConfigDatabase() {
        this.fileName = "live_config.db";
        init();
    }

    @Override
    public String getFileName() {
        return fileName;
    }

    @Override
    protected List<AbsDatabasesBean> getDataBean() {
        return List.of(new LiveConfigDatabaseBean());
    }

    public void setConfig(LiveConfigDatabaseBean bean) {
        LiveConfigDatabaseBean config = getConfig(bean.getRoomId());
        if (config == null) {
            add(bean);
            return;
        }
        bean.setSql_time(config.getSql_time());
        update(bean);
    }

    public LiveConfigDatabaseBean getConfig(String roomId) {
        List<LiveConfigDatabaseBean> list = get(getDataBean().getFirst().getTableName(), LiveConfigDatabaseBean.class);
        if (list.isEmpty()) {
            return null;
        }
        for (LiveConfigDatabaseBean bean : list) {
            if (Objects.equals(bean.getRoomId(), roomId)) {
                return bean;
            }
        }
        return null;
    }

    public boolean deleteConfig(String roomId) {
        LiveConfigDatabaseBean config = getConfig(roomId);
        if (config == null) {
            return false;
        }
        return delete(config);
    }
    public List<LiveConfigDatabaseBean> getConfigs(int page,int limit) {
        int offset = (page - 1) * limit;
        List<LiveConfigDatabaseBean> list = get(getDataBean().getFirst().getTableName(),"`sql_time` >0 limit "+limit+" offset "+offset, LiveConfigDatabaseBean.class);
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        return list;
    }
    public List<LiveConfigDatabaseBean> getAllConfig() {
        List<LiveConfigDatabaseBean> list = get(getDataBean().getFirst().getTableName(), LiveConfigDatabaseBean.class);
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        return list;
    }
    public int getConfigCount() {
        return getCount(getDataBean().getFirst().getTableName());
    }

}
