package cn.util.core;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import cn.constant.CharsetKit;

public class LogKit 
{
	private static Logger logger = LoggerFactory.getLogger(LogKit.class);

	private LogKit() {}

	public static void println(Object obj) {
		if(obj instanceof List) {
			((List<?>)obj).forEach(System.out::println);
		}
		else if(obj instanceof Map) {
			((Map<?,?>)obj).forEach((key,value)-> System.out.println(key+"="+value));
		}
		else if(obj instanceof Object[]) {
			System.out.println(Arrays.toString((Object[]) obj));
		}
		else {
			System.out.println(obj);
		} 
	}
	public static void println(HttpServletRequest request)
	{
		//获取请求头信息
        Enumeration<?> headerNames = request.getHeaderNames();
        //使用循环遍历请求头，并通过getHeader()方法获取一个指定名称的头字段
        while (headerNames.hasMoreElements()){
            String headerName = (String) headerNames.nextElement();
            System.out.println(headerName + " : " + request.getHeader(headerName));
        }
        /*Cookie[] cookies = (Cookie[]) request.getCookies();
    	for(Cookie cookie : cookies){
    		 System.out.println(cookie.toString());
    	}*/
	}	
	
	public static void debug(Object msg) {
		if(msg instanceof Exception) {
			logger.debug(((Exception)msg).getMessage());
		}
		else if(msg instanceof String) {
			logger.debug((String)msg);
		}
	} 
	public static void debug(String format, Object... argument) {
		logger.debug(format, argument);
	}

	public static void info(Object msg) {
		if(msg instanceof Exception) {
			logger.info(((Exception)msg).getMessage());
		}
		else if(msg instanceof Throwable) {
			logger.info(((Throwable)msg).getMessage());
		}
		else if(msg instanceof String) {
			logger.info((String)msg);
		}
	}
	public static void info(String format, Object... argument) {
		logger.info(format, argument);
	}

	public static void error(Object msg) {
		if(msg instanceof Exception) {
			logger.error(((Exception)msg).getMessage());
		}
		else if(msg instanceof String) {
			logger.error((String)msg);
		}
	}
	public static void error(String message, Exception ex) {
		logger.error(message, ex);
	}
	public static void error(Integer errCode, String message) {
		logger.error("错误码：" + errCode + "，错误消息：" + message);
	}
	public static void error(final Map<String, Object> model,final HttpStatus status) {
		logger.error("path:{},status:{},trace:{}", model.get("path"), status.value(), model.get("trace"));
	}
	public static void error(String format, Object... argument) {
		logger.error(format, argument);
	}
	public static void error(Integer errCode, String message, Exception ex) {
		logger.error("错误码：" + errCode + "，错误消息：" + message + ",异常信息：" + ex.getMessage());
	}

	public static String getLog(String format, Object[] arg) 
	{
		// 防止空指针
		if (IsKit.isNull(arg)) {
			if (!format.contains(CharsetKit.EMPTY_JSON)) {
				return format;
			}
			return format.replaceFirst(CharsetKit.FILL_FORMAT, "null");
		}
		StringBuilder formatBuilder = new StringBuilder(format);
		for (Object anArg : arg) {
			// 打印异常堆栈信息
			if (anArg instanceof Throwable) {
				Throwable temp = (Throwable) anArg;
				formatBuilder.append(temp.toString()).append("\n");
				StackTraceElement[] stackTrace = temp.getStackTrace();
				for (StackTraceElement aStackTrace : stackTrace) {
					formatBuilder.append(aStackTrace).append("\n");
				}
				continue;
			}
			formatBuilder = new StringBuilder(formatBuilder.toString().replaceFirst(CharsetKit.FILL_FORMAT, anArg == null ? "null" : anArg.toString()));
		}
		format = formatBuilder.toString();
		return format;
	}
}
