package com.yutou.bilibili.BiliBili.Controllers;

import com.alibaba.fastjson.JSONObject;
import com.yutou.bilibili.BiliBili.LiveUtils;
import com.yutou.bilibili.Tools.AppTools;
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 java.io.File;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

@Controller
@RequestMapping("/bili/")
public class BiliUserController {
    @ResponseBody
    @RequestMapping("/login/get/test.do")
    public JSONObject testLogin(){
        JSONObject json=new JSONObject();
        if(StringUtils.isEmpty(AppTools.readFile(new File("cookies.json")))){
            json.put("code",-1);
            json.put("msg","未登录");
        }else{
            JSONObject login= LiveUtils.getUserLoginInfo();
            JSONObject data=new JSONObject();
            json.put("code",login.getInteger("code"));
            data.put("uname",login.getJSONObject("data").getString("uname"));
            data.put("icon",login.getJSONObject("data").getString("face"));
            json.put("data",data);
        }
        return json;
    }
    @ResponseBody
    @RequestMapping("/login/set/login.do")
    public JSONObject login(){
        JSONObject login=LiveUtils.http_get("https://passport.bilibili.com/qrcode/getLoginUrl");
        JSONObject json=new JSONObject();
        json.put("code",login.getInteger("code"));
        json.put("url",login.getJSONObject("data").getString("url"));
        new Thread(new Runnable() {
            @Override
            public void run() {
                waitLogin(login.getJSONObject("data").getString("oauthKey"));
            }
        }).start();
        return json;
    }
    public void waitLogin(String oauthKey){
        long time=System.currentTimeMillis();
                String bd="gourl=https%3A%2F%2Fpassport.bilibili.com%2Fajax%2FminiLogin%2Fredirect&oauthKey="+oauthKey;
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if((System.currentTimeMillis()-time)>5*60*1000){
                    cancel();
                    return;
                }
                JSONObject json=LiveUtils.http_post("https://passport.bilibili.com/qrcode/getLoginInfo",bd);
                if(json.containsKey("code")&&json.getInteger("code")==0){
                    String _url=json.getJSONObject("data").getString("url");
                    Map<String,String> map=AppTools.getUrlParams(_url);
                    JSONObject tmp=LiveUtils.http_post(_url,"");
                    JSONObject cookie=new JSONObject();
                    for (String key : map.keySet()) {
                        cookie.put(key,map.get(key));
                    }
                    String sid=tmp.getString("cookie");
                    sid=sid.split("sid=")[1];
                    sid=sid.split(";")[0];
                    cookie.put("sid",sid);
                    cookie.put("Domain",".bilibili.com");
                    AppTools.saveFile(new File("cookies.json"),cookie.toJSONString());
                    cancel();
                }
            }
        },0,3000);

    }
}
