package com.koobietech.eas.controller;


import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.koobietech.eas.common.exception.EasException;
import com.koobietech.eas.common.result.JsonResult;
import com.koobietech.eas.dao.pojo.AdminPojo;
import com.koobietech.eas.dao.dto.LoginTokenDto;
import com.koobietech.eas.service.EasSysAdminLoginService;
import com.koobietech.eas.service.EasSysStudentLoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lc
 */
@Tag(name = "登陆模块")
@RestController
@RequestMapping("/login")
public class EasSysUsersLoginController {
    @Resource
    private EasSysAdminLoginService adminLoginService;

    @Resource
    private CaptchaService captchaService;

    @Resource
    EasSysStudentLoginService studentLoginService;

    @PostMapping("/studentLogin")
    @Operation(summary = "学生登陆", description = "用户名和密码为请求载荷，若登录成功，返回两个token")
    public JsonResult studentLogin(@RequestBody AdminPojo adminPojo) {

        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaVerification(adminPojo.getCaptchaVerification());

        ResponseModel response = captchaService.verification(captchaVO);
        if (!response.isSuccess()) {
            throw new EasException(response.getRepMsg(), Integer.parseInt(response.getRepCode()));
        }

        LoginTokenDto ret = studentLoginService.studentLogin(adminPojo);
        return JsonResult.data(ret);
    }

    @PostMapping("/adminLogin")
    @Operation(summary = "管理员登录",  description = "用户名和密码为请求载荷，若登录成功，返回两token")
    public JsonResult adminLogin(@RequestBody AdminPojo adminPojo){

        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaVerification( adminPojo.getCaptchaVerification() );
        ResponseModel response = captchaService.verification(captchaVO);
        if (!response.isSuccess()) {
            throw new EasException( response.getRepMsg() , Integer.parseInt(response.getRepCode()));
        }

        LoginTokenDto result = adminLoginService.adminLogin(adminPojo);

        return JsonResult.data(result);
    }

    @GetMapping("/getUserInfo")
    @Operation(summary = "获取用户信息", description = "根据token获取用户信息")
    public JsonResult getUserInfo(@RequestHeader("Authorization") String token) {
        return JsonResult.data(adminLoginService.getUserInfo(token));
    }

    @PostMapping("/refreshToken")
    @Operation(summary = "刷新token", description = "当token过期，在请求头中携带refresh token，若刷新成功，返回新的token和refresh token")
    public JsonResult refreshToken(@RequestHeader("Authorization") String refreshToken) {
        // 返回新的token和refresh token
        return JsonResult.data(adminLoginService.refreshToken(refreshToken));
    }

    @PostMapping("/verify/get")
    @Operation(summary = "获取验证码", description = "前端发起获取验证码请求，后端访问静态资源返回滑动图片")
    public JsonResult get(@RequestBody CaptchaVO captchaVO) {
        return  adminLoginService.verifyToJsonResult(captchaService.get(captchaVO)) ;
    }

    @PostMapping("/verify/check")
    @Operation(summary = "核对验证码轨迹", description = "前端把用户的滑动轨迹转化成字符串传入后端，通过算法判断轨迹是否为真人")
    public JsonResult check(@RequestBody CaptchaVO captchaVO) {
        return adminLoginService.verifyToJsonResult(captchaService.check(captchaVO));
    }

}
