package com.ozs.account.util;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import io.jsonwebtoken.Claims;

@Service
public class TokenUtil {

	private final Logger log = LoggerFactory.getLogger(TokenUtil.class);
	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	// 创建token
	public String createToken(String id, String subject, long minutes) {
		try {
			return JWTTool.createJWT(id, subject, 60 * 1000 * minutes);
		} catch (Exception e) {
			log.error(e.getMessage());
			return "创建token异常";
		}
	}

	// 解析用户id
	public String parseTokenId(String jwt) {
		Claims claims = null;
		try {
			claims = JWTTool.parseJWT(jwt);
		} catch (Exception e) {
			log.error(jwt);
			log.error(e.getMessage());
			return null;
		}
		return claims.getId();
	}

	// 解析企业id
	public String parseTokenSubject(String jwt) {
		Claims claims = null;
		try {
			claims = JWTTool.parseJWT(jwt);
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
		return claims.getSubject();
	}

	// 解析用户权限等级，0管理员 1普通用户
	public String parseTokenAud(String jwt) {
		Claims claims = null;
		try {
			claims = JWTTool.parseJWT(jwt);
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
		return claims.getAudience();
	}

	// 保存token
	public String saveToken(String id, String token, int minute) {
		Long expire1 = redisTemplate.getExpire(id, TimeUnit.MINUTES);
		log.info("token剩余时长-刷新前：{}分钟", expire1);
		redisTemplate.opsForValue().set(id, token, minute, TimeUnit.MINUTES);
		Long expire2 = redisTemplate.getExpire(id, TimeUnit.MINUTES);
		log.info("token剩余时长-刷新后：{}分钟", expire2);
		return "1";
	}

	public String deleteToken(String id) {
		log.info("id for token : {}", id);
		Boolean result = false;
		if (id == null) {
			return "1";
		}
		try {
			result = redisTemplate.delete(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			return "-1";
		}
		if (result) {
			return "1";
		} else {
			return "-1";
		}
	}

	public String deleteTokenBatch(List<String> ids) {
		if (ids == null) {
			return "1";
		}
		try {
			redisTemplate.delete(ids);
		} catch (Exception e) {
			log.error(e.getMessage());
			return "-1";
		}
		return "1";
	}

	// 判断账号ID对应的token是否存在
	public String getTokenByAccountId(String accountId) {
		Object token = redisTemplate.opsForValue().get(accountId);
		if (token != null) {
			return token.toString();
		}
		return null;
	}

	// 刷新token
	public String refreshToken(String accountId, int minute) {
		Boolean result = false;
		try {
			Long expire1 = redisTemplate.getExpire(accountId, TimeUnit.MINUTES);
			log.info("token剩余时长-刷新前：{}分钟", expire1);
			result = redisTemplate.expire(accountId, minute, TimeUnit.MINUTES);
			Long expire2 = redisTemplate.getExpire(accountId, TimeUnit.MINUTES);
			log.info("token剩余时长-刷新后：{}分钟", expire2);
		} catch (Exception e) {
			log.error(e.getMessage());
			return "-1";
		}
		if (result) {
			return "1";
		} else {
			return "-1";
		}
	}

}
