package com.ntels.kneet.v3.common.util;

import android.util.Log;

import com.ntels.kneet.v3.BuildConfig;

/**
 * 1. ClassName :   LogUtil
 * 2. FileName  :   LogUtil.java
 * 3. Package   :   com.android.doubleplace.util
 * 4. Date      :   2015-12-28
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 */
public class LogUtil
{
    public static final int V = 0;
    
    public static final int D = 1;
    
    public static final int I = 2;
    
    public static final int W = 3;
    
    public static final int E = 4;
    
    private static int LEVEL = V;

    public static void v(String tag, String msg)
    {
        // if (DEBUG) if (tag != null && msg != null) Log.v(tag, msg);
        Exception e = new Exception();
        StackTraceElement ste[] = e.getStackTrace();
        String logMsg = "[VERBOSE] at "
                + ste[1].getClassName()
                .substring(ste[1].getClassName().lastIndexOf(".") + 1)
                + "." + ste[1].getMethodName() + "("
                + ste[1].getFileName() + ":" + ste[1].getLineNumber()
                + ")" + ("".equals(msg) ? "" : " : " + msg);
//        printLogFile('V', tag, logMsg);

        if (LEVEL > V || !BuildConfig.DEBUG) return;
        
        Log.v(tag, logMsg);
    }
    
    public static void d(String tag, String msg)
    {
        // if (DEBUG) if (tag != null && msg != null) Log.d(tag, msg);
        Exception e = new Exception();
        StackTraceElement ste[] = e.getStackTrace();
        String logMsg = "[DEBUG] at "
                + ste[1].getClassName()
                .substring(ste[1].getClassName().lastIndexOf(".") + 1)
                + "." + ste[1].getMethodName() + "("
                + ste[1].getFileName() + ":" + ste[1].getLineNumber()
                + ")" + ("".equals(msg) ? "" : " : " + msg);
//        printLogFile('D', tag, logMsg);

        if (LEVEL > D || !BuildConfig.DEBUG) return;

        Log.d(tag, logMsg);
    }
    
    public static void i(String tag, String msg)
    {
        // if (DEBUG) if (tag != null && msg != null) Log.i(tag, msg);
        Exception e = new Exception();
        StackTraceElement ste[] = e.getStackTrace();
        String logMsg = "[INFO] at "
                + ste[1].getClassName()
                .substring(ste[1].getClassName().lastIndexOf(".") + 1)
                + "." + ste[1].getMethodName() + "("
                + ste[1].getFileName() + ":" + ste[1].getLineNumber()
                + ")" + ("".equals(msg) ? "" : " : " + msg);
//        printLogFile('I', tag, logMsg);

        if (LEVEL > I || !BuildConfig.DEBUG) return;

        Log.i(tag, logMsg);
    }
    
    public static void w(String tag, String msg)
    {
        // if (DEBUG) if (tag != null && msg != null) Log.w(tag, msg);
        Exception e = new Exception();
        StackTraceElement ste[] = e.getStackTrace();
        String logMsg = "[WARNING] at "
                + ste[1].getClassName()
                .substring(ste[1].getClassName().lastIndexOf(".") + 1)
                + "." + ste[1].getMethodName() + "("
                + ste[1].getFileName() + ":" + ste[1].getLineNumber()
                + ")" + ("".equals(msg) ? "" : " : " + msg);
//        printLogFile('W', tag, logMsg);

        if (LEVEL > W || !BuildConfig.DEBUG) return;

        Log.w(tag, logMsg);
    }
    
    public static void e(String tag, String msg)
    {
        // if (DEBUG) if (tag != null && msg != null) Log.e(tag, msg);
        Exception e = new Exception();
        StackTraceElement ste[] = e.getStackTrace();
        String logMsg = "[ERROR] at "
                + ste[1].getClassName()
                .substring(ste[1].getClassName().lastIndexOf(".") + 1)
                + "." + ste[1].getMethodName() + "("
                + ste[1].getFileName() + ":" + ste[1].getLineNumber()
                + ")" + ("".equals(msg) ? "" : " : " + msg);
//        printLogFile('E', tag, logMsg);

        if (LEVEL > E || !BuildConfig.DEBUG) return;

        Log.e(tag, logMsg);
    }

    /**
     * 로그 파일 생성
     *
     * @param log_type 로그 종류
     * @param tag 로그 태그
     * @param str 로그 문구
     * @return void
     */
    /*private static void printLogFile(char log_type, String tag, String str){
        if (!BuildConfig.DEBUG) {
            return;
        }

        try
        {
            File file = new File(DPConstants.SD_PATH + DPConstants.LOG_APP_FILE);
            FileOutputStream fos = new FileOutputStream(file, true);

            Calendar calendar = Calendar.getInstance();
            String time = String.format("%tY%tm%td%tH%tM%tS%tL", calendar, calendar, calendar, calendar, calendar, calendar, calendar);

            String log_str = log_type + "|" + time + "|" + tag + "|" + str + "\n";
            fos.write(log_str.getBytes());
            fos.close();
        }
        catch (IOException e)
        {
            ExceptionUtil.processException(e);
            e.printStackTrace();
        }
    }*/
}
