package com.qmth.teachcloud.cas.api;

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.cas.util.*;
import com.qmth.teachcloud.cas.util.supwisdom.CasUtils;
import com.qmth.teachcloud.cas.util.sxufe.Constants;
import com.qmth.teachcloud.exchange.common.bean.params.RedirectParams;
import com.qmth.teachcloud.exchange.common.contant.SystemConstant;
import com.qmth.teachcloud.exchange.common.enums.FunctionalTypeEnum;
import com.qmth.teachcloud.exchange.common.enums.SchoolCodeEnum;
import com.qmth.teachcloud.exchange.common.service.CommonService;
import com.qmth.teachcloud.exchange.common.util.JacksonUtil;
import com.qmth.teachcloud.exchange.common.util.Result;
import com.qmth.teachcloud.exchange.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * <p>
 * cas开放接口前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2022-04-26
 */
@Api(tags = "cas开放接口Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_OPEN)
@Validated
public class OpenApiController {
    private static final Logger log = LoggerFactory.getLogger(OpenApiController.class);

    @Resource
    CommonService commonService;

    @Resource
    XjtuCasUtil xjtuCasUtil;

    @Resource
    CdutCasUtil cdutCasUtil;

    @Resource
    SxufeCasUtil sxufeCasUtil;

    @Resource
    GdpuCasUtil gdpuCasUtil;

    @Resource
    SxmuCasUtil sxmuCasUtil;

    @Resource
    BjwlxyCasUtil bjwlxyCasUtil;

    @ApiOperation(value = "cas鉴权接口")
    @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
    @RequestMapping(value = "/{schoolCode}/authentication", method = RequestMethod.GET)
    @Aac(auth = false)
    public void authentication(@ApiParam(value = "学校编码") @PathVariable("schoolCode") String schoolCode,
                               @ApiParam(value = "工号") @RequestParam(required = false) String code,
                               @ApiParam(value = "系统参数") @RequestParam(required = false) String state,
                               @ApiParam(value = "用户类型") @RequestParam(required = false) String userType,
                               @ApiParam(value = "员工工号") @RequestParam(required = false) String employeeNo,
                               @ApiParam(value = "返回url") @RequestParam(required = false) String returnUrl,
                               @ApiParam(value = "功能类型") @RequestParam(defaultValue = "HOME") String functionalType) throws IOException {
        RedirectParams redirectParams = null;
        switch (SchoolCodeEnum.valueOf(schoolCode.toUpperCase())) {
            case XJTU:
                redirectParams = xjtuCasUtil.authentication(schoolCode, code, state, userType, employeeNo, returnUrl);
                break;
            case CDUT:
                cdutCasUtil.authentication(schoolCode);
                break;
            case SXUFE:
                sxufeCasUtil.authentication(schoolCode);
                break;
            case GDPU:
                redirectParams = gdpuCasUtil.authentication(schoolCode);
                break;
            case SXMU:
                redirectParams = sxmuCasUtil.authentication(schoolCode);
                break;
            case BJWLXY:
                redirectParams = bjwlxyCasUtil.authentication(schoolCode);
                break;
            default:
                break;
        }
        if (Objects.nonNull(redirectParams)) {
            commonService.redirectLogic(functionalType, redirectParams.getEmployeeNo(), redirectParams.getSchoolCode(), redirectParams.getReturnUrl(), Objects.nonNull(redirectParams.getOpenParams()) ? JacksonUtil.parseJson(redirectParams.getOpenParams()) : null);
        }
    }

    @ApiOperation(value = "cas鉴权退出接口")
    @RequestMapping(value = "/{schoolCode}/authentication/logout", method = RequestMethod.GET)
    @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
    @Aac(auth = false)
    public void logout(@ApiParam(value = "学校编码") @PathVariable("schoolCode") String schoolCode,
                       @ApiParam(value = "sessionId", required = false) @RequestParam(required = false) String gSessionId) throws IOException {
        switch (SchoolCodeEnum.valueOf(schoolCode.toUpperCase())) {
            case XJTU:
                xjtuCasUtil.logout(schoolCode, gSessionId);
                break;
            case CDUT:
                cdutCasUtil.logout(schoolCode);
                break;
            case SXUFE:
                sxufeCasUtil.logout(schoolCode);
                break;
            case GDPU:
                gdpuCasUtil.logout(schoolCode);
                break;
            case SXMU:
                sxmuCasUtil.logout(schoolCode);
                break;
            case BJWLXY:
                bjwlxyCasUtil.logout(schoolCode);
                break;
            default:
                break;
        }
    }

    @ApiOperation(value = "跳转知学知考")
    @RequestMapping(value = "/{schoolCode}/authentication/zxzk_login", method = RequestMethod.GET)
    @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
    @Aac(auth = false)
    public void zxzkLogin(@ApiParam(value = "学校编码") @PathVariable("schoolCode") String schoolCode) throws IOException {
        HttpServletResponse response = ServletUtil.getResponse();
        RedirectParams redirectParams = null;
        switch (SchoolCodeEnum.valueOf(schoolCode.toUpperCase())) {
            case CDUT:
                redirectParams = cdutCasUtil.zxzkLogin(schoolCode);
                if (Objects.nonNull(redirectParams)) {
                    commonService.redirectLogic(FunctionalTypeEnum.HOME.name(), redirectParams.getEmployeeNo(), redirectParams.getSchoolCode(), redirectParams.getReturnUrl(), Objects.nonNull(redirectParams.getOpenParams()) ? JacksonUtil.parseJson(redirectParams.getOpenParams()) : null);
                } else {
                    String loginUrl = CasUtils.getLoginUrl();
                    response.sendRedirect(loginUrl);
                }
                break;
            case SXUFE:
                redirectParams = sxufeCasUtil.zxzkLogin(schoolCode);
                if (Objects.nonNull(redirectParams)) {
                    commonService.redirectLogic(FunctionalTypeEnum.HOME.name(), redirectParams.getEmployeeNo(), redirectParams.getSchoolCode(), redirectParams.getReturnUrl(), Objects.nonNull(redirectParams.getOpenParams()) ? JacksonUtil.parseJson(redirectParams.getOpenParams()) : null);
                } else {
                    response.setHeader(SystemConstant.ACCESS_CONTROL_ALLOW_ORIGIN, SystemConstant.PATH_MATCH);
                    response.sendRedirect(Constants.CAS_LOGIN_URL);
                }
                break;
            case GDPU:
                redirectParams = gdpuCasUtil.zxzkLogin(schoolCode);
                if (Objects.nonNull(redirectParams)) {
                    commonService.redirectLogic(FunctionalTypeEnum.HOME.name(), redirectParams.getEmployeeNo(), redirectParams.getSchoolCode(), redirectParams.getReturnUrl(), Objects.nonNull(redirectParams.getOpenParams()) ? JacksonUtil.parseJson(redirectParams.getOpenParams()) : null);
                } else {
                    response.setHeader(SystemConstant.ACCESS_CONTROL_ALLOW_ORIGIN, SystemConstant.PATH_MATCH);
                    response.sendRedirect(com.qmth.teachcloud.cas.util.gdpu.Constants.CAS_LOGIN_URL);
                }
                break;
            case SXMU:
                redirectParams = sxmuCasUtil.zxzkLogin(schoolCode);
                if (Objects.nonNull(redirectParams)) {
                    commonService.redirectLogic(FunctionalTypeEnum.HOME.name(), redirectParams.getEmployeeNo(), redirectParams.getSchoolCode(), redirectParams.getReturnUrl(), Objects.nonNull(redirectParams.getOpenParams()) ? JacksonUtil.parseJson(redirectParams.getOpenParams()) : null);
                } else {
                    response.setHeader(SystemConstant.ACCESS_CONTROL_ALLOW_ORIGIN, SystemConstant.PATH_MATCH);
                    response.sendRedirect(com.qmth.teachcloud.cas.util.sxmu.Constants.CAS_LOGIN_URL);
                }
                break;
            case BJWLXY:
                redirectParams = bjwlxyCasUtil.zxzkLogin(schoolCode);
                if (Objects.nonNull(redirectParams)) {
                    commonService.redirectLogic(FunctionalTypeEnum.HOME.name(), redirectParams.getEmployeeNo(), redirectParams.getSchoolCode(), redirectParams.getReturnUrl(), Objects.nonNull(redirectParams.getOpenParams()) ? JacksonUtil.parseJson(redirectParams.getOpenParams()) : null);
                } else {
                    response.setHeader(SystemConstant.ACCESS_CONTROL_ALLOW_ORIGIN, SystemConstant.PATH_MATCH);
                    response.sendRedirect(com.qmth.teachcloud.cas.util.bjwlxy.Constants.CAS_LOGIN_URL);
                }
                break;
            default:
                break;
        }
    }

    @ApiOperation(value = "cas_mq鉴权接口")
    @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
    @RequestMapping(value = "/{schoolCode}/mq/account", method = RequestMethod.GET)
    @Aac(auth = false)
    public void maAccount(@ApiParam(value = "学校编码") @PathVariable("schoolCode") String schoolCode,
                          @ApiParam(value = "员工工号") @RequestParam(required = false) String employeeNo) throws IOException {
        RedirectParams redirectParams = null;
        switch (SchoolCodeEnum.valueOf(schoolCode.toUpperCase())) {
            case GDPU:
                redirectParams = gdpuCasUtil.mqAccount(schoolCode, employeeNo);
                break;
            default:
                break;
        }
        if (Objects.nonNull(redirectParams)) {
            commonService.redirectLogic(FunctionalTypeEnum.HOME.name(), redirectParams.getEmployeeNo(), redirectParams.getSchoolCode(), redirectParams.getReturnUrl(), Objects.nonNull(redirectParams.getOpenParams()) ? JacksonUtil.parseJson(redirectParams.getOpenParams()) : null);
        }
    }
}
