package com.yutou.nas.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.nas.Services.IBangumiService;
import com.yutou.nas.bangumi.AnimationData;
import com.yutou.nas.mybatis.model.BangumiItem;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class DmhyRssDownloadManager implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Resource
    IBangumiService bangumiService;
    private static DmhyRssDownloadManager manager;


    public static DmhyRssDownloadManager getInstance() {
        if (manager == null) {
            manager = new DmhyRssDownloadManager();
            if (manager.bangumiService == null) {
                manager.bangumiService = getBean(IBangumiService.class);
            }
        }
        return manager;
    }

    private DmhyRssDownloadManager() {
    }

    public void start() {
        List<BangumiItem> list = bangumiService.getAllBangumi();
        AnimationData data = new AnimationData();
        Log.i("BT", "启动BT检测，总数:" + list.size());
        File path = new File(BTDownloadManager.DownloadHomePath);
        if (!path.exists()) {
            Log.i("BT", "下载目录已离线，取消扫描");
            return;
        }
        for (BangumiItem item : list) {
            if (item.getEnable() == 0) {
                continue;
            }
            JSONObject json = RssXMLtoJson.toJSON(getDmhyUrl(item));
            item.setAuthor(data.nameToValue(item.getAuthor(), true) + "");
            item.setCategories(data.nameToValue(item.getCategories(), false) + "");
            if (json != null) {
                String s = RedisTools.get(item.getId() + "");
                if ("next".equals(s)) {
                    if(BTDownloadManager.skipDownload(item.getTitle(), json)) {
                        RedisTools.remove(item.getId() + "");
                    }
                    continue;
                }
                BTDownloadManager.download(item.getTitle(), json);
            } else {
                //QQBotManager.getInstance().sendMessage(item.getTitle() + "\n下载失败\n" + getDmhyUrl(item));
                Log.i(item.getTitle() + "\n下载失败");
            }
        }
    }

    private String getDmhyUrl(BangumiItem item) {
        String dmhyUrl = "https://share.dmhy.org/topics/rss/page/1/rss.xml?keyword=%s%s&order=date-desc";
        String sort_id=new AnimationData().nameToValue(item.getCategories(),false)+"";
        dmhyUrl = String.format(dmhyUrl,
                item.getTitlekey().replace(" ", "+"),
                "-1".equals(sort_id) ? "" : String.format("&sort_id=%s", sort_id)
        );
        return dmhyUrl;
    }


    public static void main(String[] args) {

    }

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        if (DmhyRssDownloadManager.applicationContext == null) {
            DmhyRssDownloadManager.applicationContext = applicationContext;
        }
    }

    private static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
