package com.crm.rely.backend.util;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.entity.login.InfoEntity;
import com.crm.rely.backend.exception.LoginException;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.service.RedisService;
import com.google.common.base.Strings;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: crm-backend
 * @description:
 * @author: houn
 * @create: 2019-08-13 15:05
 */
public class HttpServletRequestUtil {

    public static InfoEntity getLoginInfo(HttpServletRequest request, RedisService redisService) throws ServiceException {

        return getLoginInfo(InfoEntity.class, request, redisService, false);
    }

    public static <T extends InfoEntity> T getLoginInfo(Class<?> clazz,
                                                        HttpServletRequest request, RedisService redisService) throws ServiceException {

        return getLoginInfo(clazz, request, redisService, false);
    }

    public static <T extends InfoEntity> T getLoginInfo(Class<?> clazz, HttpServletRequest request,
                                                        RedisService redisService,
                                                        boolean isDefaultResultNull) throws ServiceException {
        T userUserInfoEntity = null;
        String accessToken = request.getHeader(Constants.ACCESS_TOKEN);
        if (Strings.isNullOrEmpty(accessToken) && isDefaultResultNull) {
            return userUserInfoEntity;
        }
        userUserInfoEntity = getLoginInfo(clazz, accessToken, redisService, isDefaultResultNull);

        return userUserInfoEntity;
    }

    public static InfoEntity getLoginInfo(HttpServletRequest request, RedisService redisService,
                                          boolean isDefaultResultNull) throws ServiceException {
        InfoEntity userUserInfoEntity = null;
        String accessToken = request.getHeader(Constants.ACCESS_TOKEN);
        if (Strings.isNullOrEmpty(accessToken) && isDefaultResultNull) {
            return userUserInfoEntity;
        }
        userUserInfoEntity = getLoginInfo(InfoEntity.class, accessToken, redisService, isDefaultResultNull);

        return userUserInfoEntity;
    }

    public static InfoEntity getLoginInfo(String accessToken, RedisService redisService) throws ServiceException {
        return getLoginInfo(InfoEntity.class, accessToken, redisService, false);
    }

    public static <T extends InfoEntity> T getLoginInfo(Class<?> clazz, String accessToken,
                                                        RedisService redisService) throws ServiceException {
        return getLoginInfo(clazz, accessToken, redisService, false);
    }

    public static InfoEntity getLoginInfo(String accessToken, RedisService redisService, boolean isDefaultResultNull) throws ServiceException {

        InfoEntity userUserInfoEntity = getLoginInfo(InfoEntity.class, accessToken, redisService, isDefaultResultNull);
        return userUserInfoEntity;
    }

    public static <T extends InfoEntity> T getLoginInfo(Class<?> clazz, String accessToken, RedisService redisService,
                                                        boolean isDefaultResultNull) throws ServiceException {

        T userUserInfoEntity = null;

        if (!Strings.isNullOrEmpty(accessToken)) {
            String decryptString = AESUtil.decrypt(accessToken);
            if (!Strings.isNullOrEmpty(decryptString)) {
                String[] sp = decryptString.split(Constants.LOGIN_LINK);
                if (sp != null && sp.length >= 2) {

                    String token = sp[0];
//                    String source = sp[1];
                    String value = redisService.getObject(token);
                    if (!Strings.isNullOrEmpty(value)) {
//                        userUserInfoEntity = redisService.getObject(value);
                        if (clazz == null) {
                            userUserInfoEntity = (T) redisService.getEntity(value, InfoEntity.class);
                        } else {
                            userUserInfoEntity = (T) redisService.getEntity(value, clazz);
                        }
                        if (userUserInfoEntity == null) {
                            redisService.remove(token);
                        }
                    }
                }
            }
        }
        if (userUserInfoEntity == null && !isDefaultResultNull) {
            throw new LoginException(Constants.INVALID);
        }
        return userUserInfoEntity;
    }
}
