package com.wteam.controller.passport;

import com.wteam.framework.common.enums.ResultCode;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.framework.common.security.token.Token;
import com.wteam.framework.common.utils.UuidUtils;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.connect.entity.dto.AuthCallback;
import com.wteam.framework.modules.connect.entity.dto.ConnectAuthUser;
import com.wteam.framework.modules.connect.request.AuthRequest;
import com.wteam.framework.modules.connect.service.ConnectService;
import com.wteam.framework.modules.connect.util.ConnectUtil;
import com.wteam.framework.modules.member.service.MemberService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 买家端,web联合登录
 *
 * @author elms
 */
@Slf4j
@RestController
@Tag(name = "买家端,web联合登录")
@RequestMapping("/member/passport/connect")
public class ConnectMemberWebController {

    @Autowired
    private ConnectService connectService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ConnectUtil connectUtil;


    @GetMapping("/login/web/{type}")
    @Operation(summary = "WEB信任登录授权")
    @Parameters({
            @Parameter(name = "type", description = "登录方式:QQ,微信,微信_PC",
                    example = "QQ,WECHAT,WECHAT_PC",  in = ParameterIn.PATH)
    })
    public ResultMessage<String> webAuthorize(@PathVariable String type, HttpServletResponse response) throws IOException {
        AuthRequest authRequest = connectUtil.getAuthRequest(type);
        String authorizeUrl = authRequest.authorize(UuidUtils.getUUID());
        response.sendRedirect(authorizeUrl);
        return ResultUtil.data(authorizeUrl);
    }


    @Operation(summary = "信任登录统一回调地址", hidden = true)
    @GetMapping("/callback/{type}")
    public void callBack(@PathVariable String type, AuthCallback callback, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        connectUtil.callback(type, callback, httpServletRequest, httpServletResponse);
    }

    @Operation(summary = "信任登录响应结果获取")
    @GetMapping("/result")
    public ResultMessage<Object> callBackResult(String state) {
        if (state == null) {
            throw new ServiceException(ResultCode.USER_CONNECT_LOGIN_ERROR);
        }
        return connectUtil.getResult(state);
    }

    @GetMapping("/register/auto")
    @Operation(summary = "WEB信任登录授权")
    public ResultMessage<Token> webAuthorize() {
        Token token = memberService.autoRegister();
        return ResultUtil.data(token);
    }

    @Operation(summary = "unionID登录")
    @Parameters({
            @Parameter(name = "openId", description = "openid", required = true, in =ParameterIn.QUERY),
            @Parameter(name = "type", description = "联合类型", required = true,
                    example = "WECHAT,QQ,ALIPAY,WEIBO,APPLE", in =ParameterIn.QUERY),
            @Parameter(name = "uniAccessToken", description = "联合登陆返回的accessToken", required = true, in =ParameterIn.QUERY)
    })
    @GetMapping("/app/login")
    public ResultMessage<Token> unionLogin(ConnectAuthUser authUser, @RequestHeader("uuid") String uuid) {
        try {
            return ResultUtil.data(connectService.appLoginCallback(authUser, uuid));
        } catch (Exception e) {
            log.error("unionID登录错误", e);
        }
        return null;
    }


}
