package com.yutou.bilibili.Controllers;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.api.LoginApi;
import com.yutou.biliapi.bean.login.CheckCookieBean;
import com.yutou.biliapi.bean.login.LoginUserDatabaseBean;
import com.yutou.biliapi.net.BiliCookieManager;
import com.yutou.biliapi.net.BiliLoginNetApiManager;
import com.yutou.bilibili.datas.ResultData;
import com.yutou.bilibili.datas.ReturnCode;
import com.yutou.bilibili.services.LiveLoginService;
import com.yutou.common.utils.Log;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
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.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class BiliUserController {
    @Resource
    LiveLoginService loginService;

    @ResponseBody
    @RequestMapping("/user/list")
    public JSONObject getAllUser() {
        JSONArray array = new JSONArray();
        List<LoginUserDatabaseBean> allUser = loginService.getAllUser();
        for (LoginUserDatabaseBean bean : allUser) {
            LoginApi api = BiliLoginNetApiManager.getInstance().getLoginApi(bean.getUserInfo().getMid());
            JSONObject json = new JSONObject();
            json.put("uid", bean.getUserInfo().getMid());
            json.put("uname", bean.getUserInfo().getUname());
            json.put("face", bean.getUserInfo().getFace());
            json.put("sql_time", bean.getSql_time());
            try {
                CheckCookieBean cookie = api.checkCookie().execute().body().getData();
                if (cookie == null) {
                    json.put("status", "无效");
                } else {
                    json.put("status", cookie.isRefresh() ? "待刷新" : "正常");
                }
            } catch (IOException e) {
                Log.e(e);
                json.put("status", "无效");
            }
            array.add(json);
        }
        return ResultData.success(array);
    }

    @ResponseBody
    @RequestMapping("/user/login")
    public JSONObject login(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object loginToken = session.getAttribute("loginToken");
        ReturnCode returnCode;
        if (loginToken == null) {
            loginToken = UUID.randomUUID().toString();
            returnCode = ReturnCode.RC301;
            session.setAttribute("loginToken", loginToken);
            return ResultData.success(loginToken.toString(), -1, returnCode);
        } else {
            returnCode = ReturnCode.RC300;
        }
        JSONObject login = loginService.login(loginToken.toString());
        if (login != null && login.containsKey("qrcode")) {
            return ResultData.success(login, -1, returnCode);
        } else if (login != null) {
            return ResultData.success(ReturnCode.RC302);
        }
        returnCode = ReturnCode.RC301;
        return ResultData.success(loginToken.toString(), -1, returnCode);

    }
    @ResponseBody
    @RequestMapping("/user/refreshCookie")
    public JSONObject refreshCookie(String uid) {
        if(!StringUtils.hasText(uid)){
            return ResultData.fail(ReturnCode.RC999);
        }
        if(loginService.refreshUserCookie(uid)){
            return ResultData.success(ReturnCode.RC100);
        }else{
            return ResultData.fail(ReturnCode.RC999);
        }
    }


    public static void main(String[] args) {
        LiveLoginService loginService = new LiveLoginService();
        List<LoginUserDatabaseBean> allUser = loginService.getAllUser();
        for (LoginUserDatabaseBean user : allUser) {
            System.out.println(user.getUserInfo().getMid());
            LoginApi api = BiliLoginNetApiManager.getInstance().getLoginApi(user.getUserInfo().getMid());
            try {
                CheckCookieBean cookie = api.checkCookie().execute().body().getData();
                System.out.println("cookie = " + cookie);
                if (cookie.isRefresh()) {
                    boolean flag = BiliCookieManager.refreshCookie(loginService.getCookie(user.getUserInfo().getMid()));
                    System.out.println("flag = " + flag);
                }
            } catch (IOException e) {
                Log.e(e);
            } finally {
                ;
            }
        }
//        HttpLoggingInterceptor.setLog(true);
//        BiliCookieManager.resetCookie(loginService.getCookie("3493115839121644"));

    }
}
