/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.authcore.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.codvision.authcore.bean.PageData;
import com.codvision.authcore.bean.SysMenuBean;
import com.codvision.authcore.bean.SysUserBean;
import com.codvision.authcore.config.AuthProperties;
import com.codvision.authcore.remote.RemoteAuthService;
import com.codvision.authcore.service.IAuthService;
import com.codvision.authcore.util.RequestUtil;
import com.codvision.cachecore.utils.RedisUtil;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.utils.ResponseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.codvision.authcore.constants.AuthCoreConstant.IframeDownFlag;
import static com.codvision.authcore.constants.AuthCoreConstant.LOGIN_USER_EXPIRE_PREFIX;

/**
 * 认证服务
 *
 * @author lingee
 * @date 2023/7/17
 */
public class AuthService implements IAuthService {

    @Resource
    private RemoteAuthService remoteAuthService;

    @Resource
    private AuthProperties authProperties;

    @Override
    public SysUserBean getCurrentUserInfo(String token) {
        String key = LOGIN_USER_EXPIRE_PREFIX + token;
        if (RedisUtil.hasKey(key)) {
            SysUserBean sysUser = RedisUtil.get(key);
            if (ObjectUtil.isNotEmpty(sysUser)) {
                return sysUser;
            }
        }

        ResponseEntity<SysUserBean> response = remoteAuthService.getUserInfo(token);
        if (!ResponseUtil.isSuccess(response)) {
            return null;
        }
        SysUserBean sysUser = response.getData();
        RedisUtil.set(key, sysUser);
        return sysUser;
    }

    @Override
    public String getAuthToken(HttpServletRequest request) {
        String tokenName = authProperties.getTokenName();
        String cookieName = authProperties.getCookieName();
        String token = request.getParameter(tokenName);

        if (StrUtil.isNotBlank(token)) {
            return token;
        }

        token = RequestUtil.getFromCookie(request, cookieName);
        if (StrUtil.isNotBlank(token)) {
            return token;
        }

        token = request.getHeader(tokenName);
        if (StrUtil.isNotBlank(token)) {
            return token;
        }

        final String iframeDown = request.getParameter(IframeDownFlag);
        if (StrUtil.isNotBlank(iframeDown)) {
            token = request.getParameter(tokenName);
        }
        return token;
    }

    @Override
    public List<SysMenuBean> getMenuList(String token) {
        ResponseEntity<PageData<SysMenuBean>> response = remoteAuthService.getUserMenus(token, 1, 10000);
        if (!ResponseUtil.isSuccess(response)) {
            return null;
        }
        PageData<SysMenuBean> data = response.getData();
        return data.getList();
    }

    @Override
    public SysUserBean loginByUsernameAndPassword(String username, String password) {
        return null;
    }

}
