package com.qmth.teachcloud.cas.util;

import com.qmth.teachcloud.cas.util.supwisdom.CasUtils;
import com.qmth.teachcloud.cas.util.supwisdom.Constants;
import com.qmth.teachcloud.cas.util.supwisdom.LoginUser;
import com.qmth.teachcloud.exchange.common.bean.params.RedirectParams;
import com.qmth.teachcloud.exchange.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.exchange.common.service.AuthInfoService;
import com.qmth.teachcloud.exchange.common.util.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 成都理工cas工具类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2023/6/12
 */
@Component
public class CdutCasUtil {
    private static final Logger log = LoggerFactory.getLogger(CdutCasUtil.class);

    @Resource
    AuthInfoService authInfoService;

    /**
     * 成都理工登录逻辑
     *
     * @param schoolCode
     * @return
     * @throws IOException
     */
    public void authentication(String schoolCode) throws IOException {
        HttpServletResponse response = ServletUtil.getResponse();
        HttpSession session = ServletUtil.getSession();
        String targetUrl = CasUtils.getTargetUrl();
        authInfoService.appHasExpired(schoolCode);
        if (CasUtils.isLogin(session)) {
            response.sendRedirect(targetUrl);
        } else {
            if (CasUtils.hasTicket()) {
                LoginUser loginUser = CasUtils.getLoginUser();
                if (loginUser.isLogin()) {
                    CasUtils.login(loginUser, session);
                    response.sendRedirect(targetUrl);
                } else {
                    String loginUrl = CasUtils.getLoginUrl();
                    response.sendRedirect(loginUrl);
                }
            } else {
                String loginUrl = CasUtils.getLoginUrl();
                response.sendRedirect(loginUrl);
            }
        }
    }

    /**
     * 成都理工注销逻辑
     *
     * @param schoolCode
     * @throws IOException
     */
    public void logout(String schoolCode) throws IOException {
        String logoutUrl = null;
        if (Objects.isNull(logoutUrl) || Objects.equals(logoutUrl, "")) {
            throw ExceptionResultEnum.PARAMS_ERROR.exception("鉴权退出地址不存在");
        }
        HttpServletRequest request = ServletUtil.getRequest();
        HttpServletResponse response = ServletUtil.getResponse();
        authInfoService.appHasExpired(schoolCode);
        if (doLogout(request)) {
            HttpSession session = request.getSession();
            CasUtils.logout(session);
            response.sendRedirect(CasUtils.getLogoutUrl());
        } else {
            response.sendRedirect(CasUtils.getLogoutUrl());
        }
    }

    /**
     * 知学登录
     *
     * @param schoolCode
     * @return
     * @throws IOException
     */
    public RedirectParams zxzkLogin(String schoolCode) throws IOException {
        String logoutUrl = null;
        HttpSession session = ServletUtil.getSession();
        RedirectParams redirectParams = null;
        if (CasUtils.isLogin(session)) {
            LoginUser loginUser = (LoginUser) session.getAttribute(Constants.LOGIN_USER_KEY);
            String account = loginUser.getAccount();
            redirectParams = new RedirectParams(account, schoolCode, logoutUrl, null);
        }
        return redirectParams;
    }

    /**
     * dologout
     *
     * @param request
     * @return
     */
    public boolean doLogout(HttpServletRequest request) {
        return true;
    }
}
