package com.yutou.nas.Controllers;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.nas.utils.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;

@Controller
public class JellyfinController {
    @RequestMapping("/jellyfin/addItem")
    @ResponseBody
    public String addItem(String Name, String Date) {
        if (!StringUtils.isEmpty(Name) && !StringUtils.isEmpty(Date)) {
            QQBotManager.getInstance().sendMessage(734332887L, " 新增:" + Name);
            AppTools.exec("/home/yutou/public/servier/tinyMediaManager/tinyMediaManager tvshow -u --scrapeUnscraped", null, false, true);
            new JellyfinAPIManager().refresh();
        }
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/jellyfin/updateMeta")
    public String updateMeta() {
        AppTools.exec("/home/yutou/public/servier/tinyMediaManager/tinyMediaManager tvshow -u --scrapeUnscraped", null, false, true);
        new JellyfinAPIManager().refresh();
        return "ok";
    }
    @ResponseBody
    @RequestMapping("/jellyfin/setAppVersion")
    public String setAppVersion(String version){
        RedisTools.set("jellyfin_app_version",version);
        return "";
    }
    @ResponseBody
    @RequestMapping("/jellyfin/setTvAppVersion")
    public String setTvAppVersion(String version){
        RedisTools.set("jellyfin_tv_version",version);
        return "";
    }
    @ResponseBody
    @RequestMapping("/jellyfin/updateApp.do")
    public JSONObject updateApp(String version){
        JSONObject json=new JSONObject();
        String appVersion=RedisTools.get("jellyfin_app_version");
        if(appVersion==null){
            json.put("code",0);
        }else{
            File dir=new File("web"+File.separator+"apk"+File.separator+"jellyfin"+File.separator);
            buildDownloadJson(json, appVersion, dir);

        }
        return json;
    }

    private void buildDownloadJson(JSONObject json, String appVersion, File dir) {
        json.put("code",0);
        if(dir.exists()){
            for (File file : dir.listFiles()) {
                if(file.getName().endsWith(".apk")){
                    json.put("code",1);
                    JSONObject data=new JSONObject();
                    data.put("version",appVersion);
                    data.put("url","http://nas.cnmglz.com:9999/apk"+file.getAbsolutePath().replace(new File("web"+File.separator+"apk"+File.separator).getAbsolutePath(),""));
                    json.put("data",data);
                    break;
                }
            }
        }
    }

    @ResponseBody
    @RequestMapping("/jellyfin/updateTvApp.do")
    public JSONObject updateTvApp(String version){
        JSONObject json=new JSONObject();
        String appVersion=RedisTools.get("jellyfin_tv_version");
        if(appVersion==null){
            json.put("code",0);
        }else{
            File dir=new File("web"+File.separator+"apk"+File.separator+"jellyfinTv"+File.separator);
            buildDownloadJson(json, appVersion, dir);

        }
        return json;
    }
}
