package com.qmth.teachcloud.cas.util;

import com.qmth.teachcloud.cas.util.sxufe.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.service.AuthInfoService;
import com.qmth.teachcloud.exchange.common.util.ServletUtil;
import net.psctech.sso.filter.LoginFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Description: 山西财经cas工具类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2023/6/12
 */
@Component
public class SxufeCasUtil {
    private static final Logger log = LoggerFactory.getLogger(SxufeCasUtil.class);

    @Resource
    AuthInfoService authInfoService;

    /**
     * 山西财经登录逻辑
     *
     * @param schoolCode
     * @return
     * @throws IOException
     */
    public void authentication(String schoolCode) throws IOException {
        HttpSession session = ServletUtil.getSession();
        HttpServletResponse response = ServletUtil.getResponse();
        authInfoService.appHasExpired(schoolCode);

        Object object = session.getAttribute(LoginFilter.CONST_CAS_USERNAME);
        log.info("cas:authentication:object:{}", object);
        if (object != null) {
            String targetUrl = CasUtils.getTargetUrl();
            log.info("cas:authentication:targetUrl:{}", targetUrl);
            // 跳转到知学知考
            response.setHeader(SystemConstant.ACCESS_CONTROL_ALLOW_ORIGIN, SystemConstant.PATH_MATCH);
            response.sendRedirect(targetUrl);
        } else {
            // 返回登录页
            String loginUrl = URLEncoder.encode(Constants.CAS_BASE_PATH, SystemConstant.CHARSET_NAME);
            log.info("cas:authentication:loginUrl:{}", loginUrl);
            response.setHeader(SystemConstant.ACCESS_CONTROL_ALLOW_ORIGIN, SystemConstant.PATH_MATCH);
            response.sendRedirect(loginUrl);
        }
    }

    /**
     * 山西财经注销逻辑
     *
     * @param schoolCode
     * @throws IOException
     */
    public void logout(String schoolCode) throws IOException {
        HttpServletResponse response = ServletUtil.getResponse();
        HttpSession session = ServletUtil.getSession();
        authInfoService.appHasExpired(schoolCode);
        //注销本系统session
        session.invalidate();
        log.info("cas:logout:logoutUrl:{}", CasUtils.getLogoutUrl());
        //跳转至注销后地址
        response.setHeader(SystemConstant.ACCESS_CONTROL_ALLOW_ORIGIN, SystemConstant.PATH_MATCH);
        response.sendRedirect(CasUtils.getLogoutUrl());
    }

    /**
     * 知学登录
     *
     * @param schoolCode
     * @return
     * @throws IOException
     */
    public RedirectParams zxzkLogin(String schoolCode) throws IOException {
        String logoutUrl = null;
        HttpSession session = ServletUtil.getSession();
        String userName = "";
        Object object = session.getAttribute(LoginFilter.CONST_CAS_USERNAME);
        RedirectParams redirectParams = null;
        if (object != null) {
            userName = object.toString();
            redirectParams = new RedirectParams(userName, schoolCode, logoutUrl, null);
        }
        return redirectParams;
    }
}
