package com.qmth.distributed.print.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.service.SsoService;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.RoleTypeEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
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 java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 单点登录 前端控制器
 * </p>
 *
 * @author xf
 */
@Api(tags = "单点登录Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.exam}/sso")
public class SsoController {

    @Autowired
    SsoService ssoService;

    /**
     * 评卷员单点登录
     *
     * @return
     */
    @ApiOperation(value = "评卷员单点登录")
    @RequestMapping(value = "/marker_login", method = RequestMethod.POST)
    public Result markerLogin() {
        Map<String, Object> map = ssoService.markerLoginInfo();
        return ResultUtil.ok(map);
    }

    /**
     * 评卷员单点登录
     *
     * @return
     */
    @ApiOperation(value = "评卷员单点登录")
    @RequestMapping(value = "/marker_leader_login", method = RequestMethod.POST)
    public Result markerLeaderLogin() {
        Map<String, Object> map = ssoService.markerLeaderLoginInfo();
        return ResultUtil.ok(map);
    }

    @ApiOperation(value = "教研分析单点登录")
    @ApiResponses({@ApiResponse(code = 200, message = "教研分析单点登录", response = Objects.class)})
    @RequestMapping(value = "/analysis_login", method = RequestMethod.POST)
    public Result analysisLogin(@ApiParam(value = "登录名", required = true) @RequestParam String loginName,
                                @ApiParam(value = "角色", required = true) @RequestParam RoleTypeEnum role,
                                @ApiParam(value = "学院名称", required = true) @RequestParam String orgName,
                                @ApiParam(value = "真实名", required = false) @RequestParam(required = false) String realName,
                                @ApiParam(value = "手机号码", required = false) @RequestParam(required = false) String mobileNumber,
                                @ApiParam(value = "是否启用", required = false) @RequestParam(required = false) Boolean enable,
                                @ApiParam(value = "返回url", required = true) @RequestParam String returnUrl) throws IOException {
        if (role != RoleTypeEnum.OFFICE_TEACHER && role != RoleTypeEnum.PRESIDENT && role != RoleTypeEnum.TEACHER) {
            throw ExceptionResultEnum.ERROR.exception("角色类型超出限制");
        }
        return ResultUtil.ok(ssoService.analysisLogin(loginName, role, orgName, realName, mobileNumber, enable, returnUrl));
    }
}

