package jj.tech.paolu.config.security.user;


import java.util.HashMap;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jj.tech.paolu.repository.mybatis.entity.SysAdmin;
import jj.tech.paolu.utils.AES;
import jj.tech.paolu.utils.CookieUtil;

public class WebAdminUtil {
	private static Logger logger = LoggerFactory.getLogger(WebAdminUtil.class);
	private static ObjectMapper objectMapper = new ObjectMapper();
	public static long exp = 5 * 60 * 60 * 1000; //过期时间5小时
	public static String WEBADMIN = "webadmin";
	
	static {
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}
	
	
	
	@SuppressWarnings("unchecked")
	public static Boolean verify(HttpServletRequest request, HttpServletResponse response) {
		try {
			String token = CookieUtil.fine(request, WEBADMIN);
			if (StringUtils.isBlank(token)) {
	            return false;
	        }
			AES aes = new AES();
			String jsonUser = aes.decrypt(token);
			HashMap<String,Object> map = objectMapper.readValue(jsonUser, HashMap.class);
//			Long logintime = (Long)map.get("logintime");
//			Long now = System.currentTimeMillis();
//			
//			if(now-logintime > exp) {
//				return false;
//			}
//			
//			//大于30分钟，更新一下用户token
//			if(now-logintime > 30 * 60 * 1000) {
//				map.put("logintime", now);
//				WebAdminUtil.addToCookie(map);
//			}
			
			SysAdmin bean = new SysAdmin();
			BeanUtils.populate(bean, map);
			request.setAttribute(WEBADMIN, bean);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static SysAdmin getUser() {
		try {
			ServletRequestAttributes servlet = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
			HttpServletRequest request = servlet.getRequest();
			SysAdmin curr = (SysAdmin)request.getAttribute(WEBADMIN);
			return curr;
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
			return null;
		}
	}
	
	public static String addToCookie(Object obj) {
		try {
			ServletRequestAttributes servlet = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
			HttpServletResponse response = servlet.getResponse();
			
			String userJson = objectMapper.writeValueAsString(obj);
			AES aes = new AES();
			String token = aes.encrypt(userJson);
			CookieUtil.add(WEBADMIN, token, response);
			return token;
		}catch(Exception e) {
			logger.info(e.getMessage());
			return null;
		}
	}
	
	public static String addUser(SysAdmin entitty) {
		return addToCookie(entitty);
	}
	
	public static void deleteUser() {
		try {
			ServletRequestAttributes servlet = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
			HttpServletResponse response = servlet.getResponse();
			CookieUtil.delete(response, WEBADMIN);
		}catch(Exception e) {
			logger.info(e.getMessage());
		}
	}
	
	
	

}
