package org.springblade.modules.auth.granter;

import javax.servlet.http.HttpServletRequest;

import org.springblade.common.cache.CacheNames;
import org.springblade.common.utils.DigestUtil;
import org.springblade.core.constant.BladeConstant;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.WebUtil;
import org.springblade.core.tool.exception.ServiceException;
import org.springblade.modules.auth.enums.UserEnum;
import org.springblade.modules.auth.provider.ITokenGranter;
import org.springblade.modules.auth.provider.TokenParameter;
import org.springblade.modules.auth.utils.TokenUtil;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.entity.UserInfo;
import org.springblade.modules.system.service.ITenantService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.stereotype.Component;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;

/**
 * 验证码TokenGranter
 *
 * @author Chill
 */
@Component
@AllArgsConstructor
public class CaptchaTokenGranter implements ITokenGranter {

	public static final String GRANT_TYPE = "captcha";

	private final IUserService userService;
	private final ITenantService tenantService;
	private final BladeRedis bladeRedis;

	@Override
	public UserInfo grant(TokenParameter tokenParameter) {
		HttpServletRequest request = WebUtil.getRequest();

		String key = request.getHeader(TokenUtil.CAPTCHA_HEADER_KEY);
		String code = request.getHeader(TokenUtil.CAPTCHA_HEADER_CODE);
		// 获取验证码
		String redisCode = bladeRedis.get(CacheNames.CAPTCHA_KEY + key);
		// 判断验证码
		if (code == null || !StrUtil.equalsIgnoreCase(redisCode, code)) {
			throw new ServiceException(TokenUtil.CAPTCHA_NOT_CORRECT);
		}

//		String tenantId = tokenParameter.getArgs().getStr("tenantId");
		String username = tokenParameter.getArgs().getStr("username");
		String password = tokenParameter.getArgs().getStr("password");
		UserInfo userInfo = null;
		if (StrUtil.isNotBlank(username) && StrUtil.isNotBlank(password)) {
			// 获取租户信息
//			Tenant tenant = tenantService.getByTenantId(tenantId);
//			if (TokenUtil.judgeTenant(tenant)) {
//				throw new ServiceException(TokenUtil.USER_HAS_NO_TENANT_PERMISSION);
//			}
			// 获取用户类型
			String userType = tokenParameter.getArgs().getStr("userType");
			// 根据不同用户类型调用对应的接口返回数据，用户可自行拓展
			if (userType.equals(UserEnum.WEB.getName())) {
				userInfo = userService.login(username, DigestUtil.hex(password), UserEnum.WEB);
			} else if (userType.equals(UserEnum.APP.getName())) {
				userInfo = userService.login(username, DigestUtil.hex(password), UserEnum.APP);
			} else {
				userInfo = userService.login(username, DigestUtil.hex(password), UserEnum.OTHER);
			}
			// 获取租户信息 - 超级管理员不判断租户
			if(userInfo != null && userInfo.getUser() != null && !StrUtil.equalsIgnoreCase(userInfo.getUser().getTenantId(), BladeConstant.ADMIN_TENANT_ID)) {
				Tenant tenant = tenantService.getByTenantId(userInfo.getUser().getTenantId());
				if (TokenUtil.judgeTenant(tenant)) {
					throw new ServiceException(TokenUtil.USER_HAS_NO_TENANT_PERMISSION);
				}
			}
		}
		return userInfo;
	}

}
