package com.qmth.sop.server.auth;

import com.qmth.boot.core.enums.Platform;
import com.qmth.boot.core.security.model.AccessEntity;
import com.qmth.boot.core.security.service.AuthorizationService;
import com.qmth.boot.tools.signature.SignatureType;
import com.qmth.sop.business.bean.auth.AuthBean;
import com.qmth.sop.business.cache.CommonCacheService;
import com.qmth.sop.business.entity.SysConfig;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.entity.TBSession;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import com.qmth.sop.common.enums.PrivilegePropertyEnum;
import com.qmth.sop.common.enums.RoleTypeEnum;
import com.qmth.sop.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 java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

@Component
public class SopServerAuthenticationService implements AuthorizationService {
    private final static Logger log = LoggerFactory.getLogger(SopServerAuthenticationService.class);

    @Resource
    CommonCacheService commonCacheService;

    @Override
    public AccessEntity findByIdentity(String identity, SignatureType signatureType, String path) {
        return new SopServerSession(identity, SignatureType.TOKEN);
    }

    @Override
    public boolean hasPermission(AccessEntity accessEntity, String path) {
        if (Objects.nonNull(accessEntity) && Objects.nonNull(accessEntity.getIdentity())) {
            TBSession tbSession = (TBSession) commonCacheService.getUserSession(accessEntity.getIdentity());
            if (Objects.isNull(tbSession)) {
                log.warn("Authorization faile: session id not exists: {}", accessEntity.getIdentity());
                throw ExceptionResultEnum.NOT_LOGIN.exception();
            }
            if (tbSession.getExpireTime() <= System.currentTimeMillis()) {
                log.warn("Authorization faile: session has expired, expire time={}", tbSession.getExpireTime());
                throw ExceptionResultEnum.NOT_LOGIN.exception();
            }
            Platform platform = ServletUtil.getRequestPlatform();
            String deviceId = ServletUtil.getRequestDeviceId();
            if (!tbSession.getPlatform().equalsIgnoreCase(platform.name())) {
                log.warn("Authorization faile: platform invalid, session platform is {}", tbSession.getPlatform());
                throw ExceptionResultEnum.AUTHORIZATION_ERROR.exception();
            }
            if (!tbSession.getDeviceId().equalsIgnoreCase(deviceId)) {
                log.warn("Authorization faile: deviceId invalid, session deviceId is {} ", tbSession.getDeviceId());
                throw ExceptionResultEnum.AUTHORIZATION_ERROR.exception();
            }
            Long userId = Long.parseLong(tbSession.getIdentity());
            SysUser sysUser = commonCacheService.userCache(userId);
            HttpServletRequest request = ServletUtil.getRequest();
            HttpServletResponse response = ServletUtil.getResponse();
            request.setAttribute(SystemConstant.SESSION, tbSession);
            request.setAttribute(SystemConstant.USER, sysUser);
            boolean auth = authFootCommon(userId, SystemConstant.USER_OAUTH_CACHE, path, request, response);
            if (auth) {
                Long expireTime = tbSession.getExpireTime();
                if (Objects.nonNull(expireTime) && expireTime.longValue() > -1L) {
                    SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.SESSION_ACTIVE);
                    Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置session会话时长"));
                    Duration sessionActive = Duration.parse(SystemConstant.PT + sysConfig.getConfigValue());
                    if (Objects.nonNull(tbSession.getLastAccessTime()) && (System.currentTimeMillis() - tbSession.getLastAccessTime()) / 1000 > sessionActive.getSeconds()) {
                        log.warn("Authorization faile: session active, session active is {}", sessionActive.getSeconds());
                        throw ExceptionResultEnum.NOT_LOGIN.exception();
                    }
                    tbSession.setLastInfo();
                    commonCacheService.setUserSession(accessEntity.getIdentity(), tbSession);
                }
            }
            return auth;
        }
        return false;
    }

    /**
     * 鉴权尾公用
     *
     * @param userId
     * @param type
     * @param path
     * @param request
     * @param response
     * @return
     */
    public boolean authFootCommon(Long userId,
                                  String type,
                                  String path,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        //验证权限
        AuthBean authBean = type.contains(SystemConstant.USER_OAUTH_CACHE) ? authBean = commonCacheService.userAuthCache(userId) : null;
        if (Objects.isNull(authBean)) {
            throw ExceptionResultEnum.ROLE_ENABLE_AUTHORIZATION.exception();
        }
        request.setAttribute(SystemConstant.ORG, authBean.getOrg());

        //超级系统管理员拥有所有权限
        int count = Objects.nonNull(authBean) ? (int) authBean.getRoleList().stream().filter(s -> Objects.equals(s.getName(), RoleTypeEnum.ADMIN.getDesc())).count() : 0;
        if (count > 0) {
            return true;
        }
        //系统公用接口不拦截
        Set<String> sysUrls = commonCacheService.privilegeUrlCache(PrivilegePropertyEnum.SYS);
        int sysCount = Objects.nonNull(sysUrls) ? (int) sysUrls.stream().filter(s -> s.equalsIgnoreCase(path)).count() : 0;
        if (sysCount > 0) {
            return true;
        }
        Set<String> urls = authBean.getUrls();
        int privilegeCount = Objects.nonNull(urls) ? (int) urls.stream().filter(s -> s.equalsIgnoreCase(path)).count() : 0;
        if (privilegeCount == 0) {
            log.warn("Authorization faile: url cannot access");
            throw ExceptionResultEnum.UN_AUTHORIZATION.exception();
        }
        response.setStatus(ExceptionResultEnum.SUCCESS.getCode());
        return true;
    }
}
