package com.sonic.frog.service.impl;

import com.sonic.bear.lib.client.UserLoginClient;
import com.sonic.bear.lib.enums.ThirdTypeEnum;
import com.sonic.bear.lib.input.ThirdUserLoginInput;
import com.sonic.common.auth.domains.Session;
import com.sonic.common.exception.BizException;
import com.sonic.common.exception.BizExceptionUtils;
import com.sonic.common.rpc.GlobalResultCode;
import com.sonic.frog.client.AppleIdLoginValidateOauth2Client;
import com.sonic.frog.client.DiscordOauth2Client;
import com.sonic.frog.client.GoogleOauth2Client;
import com.sonic.frog.domain.bo.ThirdAuthBo;
import com.sonic.frog.domain.input.ThirdLoginOrRegisterInput;
import com.sonic.frog.domain.output.ThirdLoginOrRegisterOutput;
import com.sonic.frog.enums.ToastResultCode;
import com.sonic.frog.service.ThirdLoginOrRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author code
 * @Description 登录或注册验证
 * @Version 1.0
 */
@Slf4j
@Service
public class ThirdLoginOrRegisterServiceImpl implements ThirdLoginOrRegisterService {

    @Autowired
    private GoogleOauth2Client googleOauth2Client;
    @Autowired
    private AppleIdLoginValidateOauth2Client appleIdLoginValidateOauth2Client;
    @Autowired
    private DiscordOauth2Client discordOauth2Client;
    @Autowired
    private UserLoginClient userLoginClient;

    @Override
    public ThirdLoginOrRegisterOutput loginOrRegister(ThirdLoginOrRegisterInput input) {
        //三方账号不管登录环境发生变更的问题，所以这里不做设备相关的校验
        ThirdAuthBo thirdAuthBo;
        ThirdTypeEnum thirdType = input.getThirdType();
        switch (thirdType) {
            case GOOGLE:
                thirdAuthBo = googleOauth2Client.getGoogleIdByToken(input.getThirdToken());
                break;
            case APPLE:
                boolean valid = appleIdLoginValidateOauth2Client.isValid(input.getThirdToken(), input.getAppClient());
                BizExceptionUtils.check(!valid, "-1", "Apple account could not be verified");
                thirdAuthBo = appleIdLoginValidateOauth2Client.parseIdByIdToken(input.getThirdToken());
                break;
            case DISCORD:
                thirdAuthBo = discordOauth2Client.getDiscordIdByToken(input.getThirdToken());
                break;
            default:
                throw new BizException(GlobalResultCode.INVALID_PARAMS);
        }
        ToastResultCode.ACCOUNT_DOES_NOT_EXIST.check(StringUtils.isEmpty(thirdAuthBo.getThirdId()));
        //执行注册并登录的操作
        ThirdUserLoginInput thirdUserLoginInput = ThirdUserLoginInput.builder()
                .openId(thirdAuthBo.getThirdId())
                .email(thirdAuthBo.getEmail())
                .nickname(thirdAuthBo.getNickname())
                .thirdType(thirdType)
                .clientCode(input.getAppClient())
                .deviceId(input.getDeviceCode())
                .ip(input.getIp())
                .userAgent(input.getUserAgent()).build();
        Session session = userLoginClient.thirdLogin(thirdUserLoginInput);
        //构造出参对象
        ThirdLoginOrRegisterOutput output = new ThirdLoginOrRegisterOutput();
        output.setToken(session.getToken());
        return output;
    }

}
