package com.dsideal.sso.Util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.kit.PropKit;

public class SsoLoginHelper {

    public static Map<String, String> loginCheck(HttpServletRequest request) {
        String cookieSessionId = getSessionIdByCookie(request);
        if (cookieSessionId != null && !cookieSessionId.trim().isEmpty()) {
            return loginCheck(cookieSessionId);
        }
        return null;
    }

    public static Map<String, String> loginCheck(String sessionId) {
        if (sessionId != null && !sessionId.trim().isEmpty()) {
            return SsoLoginStore.get(sessionId);
        }
        return null;
    }

    public static String getSessionIdByCookie(HttpServletRequest request) {
        String cookieSessionId = CookieUtil.getValue(request, PropKit.get("sso.sessionid"));
        return cookieSessionId;
    }

    public static void login(HttpServletResponse response, String sessionId, Map<String, String> loginMap) {
        SsoLoginStore.put(sessionId, loginMap);
        CookieUtil.set(response, PropKit.get("sso.sessionid"), sessionId, false);
    }

    public static void login(String sessionId, Map<String, String> loginMap) {
        SsoLoginStore.put(sessionId, loginMap);
    }

    /**
     * WEB客户端退出
     *
     * @param request
     * @param response
     */
    public static void logout(HttpServletRequest request, HttpServletResponse response) {
        String cookieSessionId = getSessionIdByCookie(request);
        if (cookieSessionId != null) {
            SsoLoginStore.remove(cookieSessionId);
        }
        CookieUtil.remove(request, response, PropKit.get("sso.sessionid"));
    }

    /**
     * APP客户端退出
     *
     * @param sessionId
     */
    public static void logout(String sessionId) {
        SsoLoginStore.remove(sessionId);
    }

}
