package com.yutou.bilibili.services;

import com.yutou.biliapi.bean.live.LiveRoomInfo;
import com.yutou.biliapi.bean.live.MasterInfoBean;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
import com.yutou.biliapi.net.BiliLiveNetApiManager;
import com.yutou.common.okhttp.HttpDownloadUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

@Service
public class LiveConfigService {
    BiliLiveConfigDatabase database = new BiliLiveConfigDatabase();

    public LiveConfigDatabaseBean addConfig(String url, LiveConfigDatabaseBean bean) {
        if (!StringUtils.hasText(url)) {
            return null;
        }
        String roomId = url.replace("https://live.bilibili.com/", "").split("\\?")[0];
        if (!StringUtils.hasText(roomId)) {
            return null;
        }
        bean.setRoomId(roomId);
        try {
            LiveRoomInfo body = BiliLiveNetApiManager.getInstance().getApi(null).getRoomInfo(String.valueOf(bean.getRoomId())).execute().body().getData();
            MasterInfoBean infoBean = BiliLiveNetApiManager.getInstance().getApi(null).getMasterInfo(String.valueOf(body.getUid())).execute().body().getData();
            bean.setAnchorUid(body.getUid());
            bean.setRoomId(body.getRoomId());
            bean.setAnchorFace(infoBean.getInfo().getFace());
            bean.setAnchorName(infoBean.getInfo().getUname());
            database.setConfig(bean);
            return bean;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public LiveConfigDatabaseBean updateConfig(String roomId, LiveConfigDatabaseBean bean) {
        LiveConfigDatabaseBean config = database.getConfig(roomId);
        if (config == null) {
            return null;
        }
        bean.setRoomId(roomId);
        bean.setSql_time(config.getSql_time());
        database.setConfig(bean);
        return bean;
    }

    public boolean deleteConfig(String roomId) {
        LiveConfigDatabaseBean config = database.getConfig(roomId);
        if (config == null) {
            return false;

        }
        return database.deleteConfig(roomId);
    }

    public List<LiveConfigDatabaseBean> getAllConfig() {
        return database.getAllConfig();
    }
    public List<LiveConfigDatabaseBean> getConfigs(int page,int limit) {
        return database.getConfigs(page, limit);
    }

    public LiveConfigDatabaseBean getConfig(String roomId) {
        return database.getConfig(roomId);
    }

    public File getFace(String roomId) {
        LiveConfigDatabaseBean config = database.getConfig(new String(roomId));
        if (config == null) {
            return null;
        }
        return new File(config.getRecordPath() + File.separator + config.getAnchorName() + File.separator + config.getAnchorFace());
    }


    public boolean checkUrl(String url) {
        if (!url.startsWith("https://live.bilibili.com/")) {
            try {
                new String(url);
            } catch (Exception e) {
                return false;
            }
        } else {
            url = url.replace("https://live.bilibili.com/", "").split("\\?")[0];
        }
        try {
            LiveRoomInfo body = BiliLiveNetApiManager.getInstance().getApi(null).getRoomInfo(url).execute().body().getData();
            return body != null;
        } catch (IOException e) {
            return false;

        }
    }

    public int getConfigCount() {
        return database.getConfigCount();
    }
}
