package cn.cslg.pas.controller;

import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.service.OAuth2Service;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Tag(name = "登录与用户信息")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/oauth2")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class OAuth2Controller {

    private final OAuth2Service oAuth2Service;

    @GetMapping("userinfo")
    @Operation(summary = "获取用户信息")
    public String userinfo() {
        return Response.success(oAuth2Service.getUserinfo());
    }

    @GetMapping("permissions")
    @Operation(summary = "获取用户专题库权限")
    public String getPermissions() throws IOException {
        return Response.success(oAuth2Service.getPermissions());
    }

    @PostMapping("token")
    @Operation(summary = "用户登录")
    public String getTokenByUsername(String username, String password, String code, String uuid) throws IOException {
       // return oAuth2Service.getTokenByUsername(username, password, code, uuid);
        return  oAuth2Service.login(username,password,code,uuid);
    }

    @GetMapping("verifyCode")
    @Operation(summary = "获取登录验证码")
    public String getVerifyCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        return oAuth2Service.getVerifyCode(request, response);
        return  oAuth2Service.verifyCode();
    }

    @PostMapping("changePwd")
    @Operation(summary = "用户修改密码")
    public String changePwd(String oldPassword, String newPassword) {
        return oAuth2Service.changePwd(oldPassword, newPassword);
    }

    @PostMapping("logout")
    @Operation(summary = "退出登录")
    public String logout() {
        StpUtil.logout();
        return Response.success(true);
    }
}
