package com.yutou.tools.BiliBili;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.mybatis.dao.BilibiliLiveDao;
import com.yutou.tools.mybatis.model.BilibiliLive;
import com.yutou.tools.mybatis.model.BilibiliLiveExample;
import com.yutou.tools.utils.BiliBiliLiveTools;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
public class Live {
    @Resource
    BilibiliLiveDao bilibiliLiveDao;

    @RequestMapping(value = "/bili/live/add/url.do")
    @ResponseBody
    public String addLiveUrl(String url){
        String cid;
        if(url.startsWith("https://")){
            cid=url.replace("https://live.bilibili.com/","").split("\\?")[0];
        }else{
            cid=url;
            url="https://live.bilibili.com/"+cid;
        }
        BilibiliLive live=new BilibiliLive();
        live.setUrl(url);
        live.setCreatetime(new Date());
        live.setStatus(1);
        try{
            live.setCid(Integer.parseInt(cid));
        }catch (Exception e){
            live.setCid(-1);
        }
        live.setTitle(BiliBiliLiveTools.getLiveUserName(live.getCid()+""));
        bilibiliLiveDao.insert(live);
        JSONObject json=new JSONObject();
        json.put("code",0);
        json.put("msg","ok");
        return json.toJSONString();
    }

    @ResponseBody
    @RequestMapping("/bili/live/get/url.do")
    public String getLiveUrl(){
        List<BilibiliLive> list=bilibiliLiveDao.selectByExample(new BilibiliLiveExample());
        JSONObject json=new JSONObject();
        json.put("code",0);
        json.put("msg","ok");
        json.put("data",JSONArray.toJSON(list));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping("/bili/live/set/update.do")
    public String configDown(String id,String url,String cid,String status){
        JSONObject json=new JSONObject();
        BilibiliLive live=bilibiliLiveDao.selectByPrimaryKey(Integer.parseInt(id));
        if(!StringUtils.isEmpty(url)){
            if(url.startsWith("https://")){
                cid=url.replace("https://live.bilibili.com/","").split("\\?")[0];
            }else{
                cid=url;
                url="https://live.bilibili.com/"+cid;
            }
            live.setUrl(url);
            live.setCid(Integer.parseInt(cid));
        }
        if(!StringUtils.isEmpty(cid)){
            live.setCid(Integer.parseInt(cid));
        }
        if(!StringUtils.isEmpty(status)){
            live.setStatus(Integer.parseInt(status));
        }
        live.setTitle(BiliBiliLiveTools.getLiveUserName(live.getCid()+""));
        int code=bilibiliLiveDao.updateByPrimaryKey(live);
        json.put("code",code);
        json.put("msg","ok");
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping("/bili/live/set/delete.do")
    public String delUrl(String id){
        JSONObject json=new JSONObject();
        try{
            int code=bilibiliLiveDao.deleteByPrimaryKey(Integer.parseInt(id));
            json.put("code",code);
            json.put("msg","ret "+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
}
