package com.hotent.auth.server.controller;

import com.hotent.auth.server.service.AuthenticationService;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.model.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.client.ClientProtocolException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@Api(tags = "认证接口")
@ApiGroup(group = {ApiGroupConsts.GROUP_AUTH})
public class AuthenticationMobileController {
    @Resource
    AuthenticationService authenticationService;

    @RequestMapping(value = "/sso/sendMsgYzmCode", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "发送登录手机验证码", httpMethod = "GET", notes = "发送登录手机验证码")
    public CommonResult<?> sendMsgYzmCode(@RequestParam(value = "mobile", required = true) String mobile, @RequestParam(value = "use", required = true) String use,
                                          @RequestParam(value = "randImageCode", required = true) String randImageCode, @RequestParam(value = "randImageId", required = true) String randImageId) throws AuthenticationException, ClientProtocolException, IOException {
        return authenticationService.sendMsgYzmCode(mobile, use, randImageId, randImageCode);
    }

    /***
     * 短信验证码登录，直接获取token
     * @param mobile
     * @param code
     * @param use
     * @return
     * @throws AuthenticationException
     */
    @RequestMapping(value = "/sso/msgYzmLogin", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "手机号验证码登录", httpMethod = "GET", notes = "手机号验证码登录")
    public CommonResult<?> msgYzmLogin(@RequestParam(value = "mobile", required = true) String mobile,
                                       @RequestParam(value = "code", required = true) String code,
                                       @RequestParam(value = "use", required = true) String use) throws AuthenticationException {
        return authenticationService.msgYzmLogin(mobile, code, use);
    }

    /***
     * 通过统一登录页面，采取短信验证码方式登录， 返回code  跳转回第三方应用，通过code换取token
     * @param mobile
     * @param code
     * @param use
     * @param clientId
     * @return
     * @throws AuthenticationException
     */
    @RequestMapping(value = "/sso/msgYzmLoginCode", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "手机号验证码登录", httpMethod = "POST", notes = "手机号验证码登录")
    public CommonResult<?> msgYzmLogin(@RequestParam(value = "mobile", required = true) String mobile,
                                       @RequestParam(value = "code", required = true) String code,
                                       @RequestParam(value = "use", required = true) String use,
                                       @RequestParam(value = "clientId", required = true) String clientId) throws AuthenticationException {
        return authenticationService.msgYzmLoginCode(clientId, mobile, code, use);
    }


    /***
     * 通过统一登录页面，采取扫码方式登录， 返回code  跳转回第三方应用，通过code换取token
     * @param busId
     * @return
     * @throws AuthenticationException
     * @throws ClientProtocolException
     * @throws IOException
     */
    @RequestMapping(value = "/sso/scanLoginCode", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取扫码结果方法", httpMethod = "POST", notes = "获取扫码结果方法")
    public  CommonResult<?> scanLoginCode(@RequestParam(value = "busId", required = true) String busId,
                                          @RequestParam(value = "clientId", required = true) String clientId) throws AuthenticationException, ClientProtocolException, IOException {
        return authenticationService.scanLoginCode(clientId,busId);
    }
}
