package com.xixun.display;

import android.annotation.SuppressLint;
import android.util.Log;

import com.xixun.lay.safe.Safe;

public class MyLog {
	private static final String TAG = "DisplayAPP";

	public static void sleepForMillisecond(long ms) {
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void sleepForSecond(long second) {
		sleepForMillisecond(second * 1000);
	}
	private static String formatStr(String msg) {
		StringBuilder show=new StringBuilder("");

		show.append(msg);		
		for (int index = 70; index < show.length(); index += 70) {
			if (' ' == show.charAt(index - 1)) {
				show.insert(index - 1, '\n');
			} else if (' ' == show.charAt(index - 2)) {
				show.insert(index - 2, '\n');
			} else {
				show.insert(index, '\n');
			}
		}
		
		return show.toString();
	}
	
	public static void i(String msg) {
		Log.i(TAG, formatStr(msg));
	}
	
	public static void d(String msg) {
		Log.d(TAG, formatStr(msg));
	}
	
	public static void e(String msg) {
		Log.e(TAG, formatStr(msg));
	}
	
	public static void v(String msg) {
		Log.v(TAG, formatStr(msg));
	}
	
	public static void w(String msg) {
		Log.w(TAG, formatStr(msg));
	}
	
	@SuppressLint("DefaultLocale")
	public static void showBytes(String title, byte[] msg, int len) {
		StringBuilder show=new StringBuilder("");
		int showNum = 0;
		int iData = 0;

		if ((null != title) && (!title.equals(""))) {
			show.append(title + ": ");
		} else {
			show.append("Hex: ");
		}
		if (null != msg) {
			showNum = (0 == len)? msg.length: len;
			if (showNum > msg.length) {
				showNum = msg.length;
			}
			for (int i = 0; i < showNum; ++i) {
				iData = msg[i] & 0xff;
				if (iData < 16) {
					show.append('0');
				}
				show.append(Integer.toHexString(iData).toUpperCase());
				show.append(' ');
			}
		} else {
			show.append("Message is null.");
		}		
		d(show.toString());
//		MyApp.writeLog(show.toString(), false);
	}
	
	public static void debugLine() {
		debugLine(null);
	}
	
	public static void debugLine(String msg) {
		StackTraceElement stack = Thread.currentThread().getStackTrace()[(null == msg)? 4: 3];
		Log.d(TAG, stack.getClassName() 
				+ " -> " + stack.getMethodName() 
				+ " -> " + stack.getLineNumber()
				+ ((null != msg)? (": " + msg): ""));
	}
	
	/*public static void json(Object obj) {
		try {
			Log.i(TAG, MyApp.getMapper().writeValueAsString(obj));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
	}*/
}
