package com.hfkj.common.security;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.model.SecUserSessionObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

@Component
public class UserCenter {

	private static Logger log = LoggerFactory.getLogger(UserCenter.class);

	@Autowired
    private RedisUtil redisUtil;
	private final int EXPIRE = 3600 * 24 * 30; // 登录过期时间为30天

    /**
     * 保存登录信息
     * @throws Exception
     */
    public void save(SessionObject seObj) {
        redisUtil.set(seObj.getToken(), seObj, EXPIRE);
    }

    /**
     * 是否登录
     * @param request
     * @return
     */
    public boolean isLogin(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if(StringUtils.isBlank(token)) {
            return false;
        }
        return redisUtil.hasKey(token);
    }

    /**
     * 是否登录
     * @param token 账户token
     * @return
     */
    public boolean isLogin(String token){
        if(StringUtils.isBlank(token)) {
            return false;
        }
        return redisUtil.hasKey(token);
    }

    /**
     * 退出登录
     * @param request
     */
    public void remove(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (StringUtils.isNotBlank(token)) {
            //通过token方式登录
            redisUtil.del(token);
        }
    }

    /**
     * 退出登录
     * @param token
     */
    public void remove(String token) {
        if (StringUtils.isNotBlank(token)) {
            //通过token方式登录
            redisUtil.del(token);
        }
    }

    /**
     * 获取session信息
     * @param clazz
     * @return
     * @param <E>
     */
    public <E> E getSessionModel(Class<E> clazz){
        if (RequestContextHolder.getRequestAttributes()!=null) {
            HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            SessionObject sessionObject = getSessionObject(req);
            if (sessionObject == null){
                return null;
            }
            if (clazz.equals(sessionObject.getObject().getClass())) {
                return (E)sessionObject.getObject();
            }
        }
        return null;
    }

    /**
     * 获取session信息
     * @param request
     * @return
     */
    public SessionObject getSessionObject(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (StringUtils.isBlank(token)) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
        }
        Object obj = redisUtil.get(token);
        if (obj == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
        }
        return (SessionObject) obj;
    }

    /**
     * 获取session信息
     * @param token
     * @return
     */
    public SessionObject getSessionObject(String token) {
        if (StringUtils.isBlank(token)) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
        }
        Object obj = redisUtil.get(token);
        if (obj == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
        }
        return (SessionObject) obj;
    }

}
