package cn.reghao.tnb.auth.app.controller;

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.util.StringRegexp;
import cn.reghao.tnb.auth.api.dto.AccountInfo;
import cn.reghao.tnb.auth.api.dto.OAuthAccountInfo;
import cn.reghao.tnb.auth.api.iface.AccountQuery;
import cn.reghao.tnb.auth.app.db.mapper.UserAccountMapper;
import cn.reghao.tnb.auth.app.service.AccountTokenService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2023-07-31 11:31:04
 */
@Slf4j
@Tag(name = "OAuth2 资源服务器接口")
@RestController
@RequestMapping("/api/account/resource")
public class AccountResourceController {
    private final UserAccountMapper userAccountMapper;
    private final AccountQuery accountQuery;
    private AccountTokenService accountTokenService;

    public AccountResourceController(UserAccountMapper userAccountMapper, AccountQuery accountQuery,
                                     AccountTokenService accountTokenService) {
        this.userAccountMapper = userAccountMapper;
        this.accountQuery = accountQuery;
        this.accountTokenService = accountTokenService;
    }

    @Operation(summary = "OAuth2 认证访问", description = "N")
    @GetMapping("/my/1")
    public String getUserInfo() {
        Authentication authentication = accountTokenService.getAuthentication();
        if (authentication instanceof OAuth2Authentication) {
            OAuth2Authentication oauth = (OAuth2Authentication) authentication;
            String principal = (String) oauth.getUserAuthentication().getPrincipal();
            //String mobile = Long.parseLong(principal);
            log.info("principal -> {}", principal);
            AccountInfo accountInfo = getAccountInfo(principal);
            if (accountInfo != null) {
                long userId = accountInfo.getUserId();
                OAuthAccountInfo oAuthAccountInfo = new OAuthAccountInfo(userId, accountInfo.getScreenName());
                return WebResult.success(oAuthAccountInfo);
            }
        }

        log.info("get my info by oauth");
        return WebResult.fail();
    }

    private AccountInfo getAccountInfo(String principal) {
        AccountInfo accountInfo;
        if (StringRegexp.matchEmail(principal)) {
            accountInfo = userAccountMapper.findAccountInfoByEmail(principal);
        } else if (StringRegexp.matchMobile(principal)) {
            accountInfo = userAccountMapper.findAccountInfoByMobile(principal);
        } else {
            accountInfo = userAccountMapper.findAccountInfoByUsername(principal);
        }

        return accountInfo;
    }

    @Operation(summary = "OAuth2 认证访问", description = "N")
    @PostMapping("/user/2")
    public String getUserInfo1() {
        log.info("get user info by oauth");
        return WebResult.success();
    }
}
