/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.onecable.common.util;

import com.ntels.onecable.common.AppConfig;

import android.util.Log;

public class Dlog {

    static final String TAG = "Jino";

    static final int LOG_TYPE_V = 0;
    static final int LOG_TYPE_D = 1;
    static final int LOG_TYPE_I = 2;
    static final int LOG_TYPE_W = 3;
    static final int LOG_TYPE_E = 4;

    /**
     * Log Level Error
     *
     * @param message Log message
     **/
    public static final void e(Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_E, buildLogMsg(message));
    }

    /**
     * Log Level Warning
     *
     * @param message Log message
     **/
    public static final void w(Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_W, buildLogMsg(message));
    }

    /**
     * Log Level Information
     *
     * @param message Log message
     **/
    public static final void i(Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_I, buildLogMsg(message));
    }

    /**
     * Log Level Debug
     *
     * @param message Log message
     **/
    public static final void d(Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_D, buildLogMsg(message));
    }

    /**
     * Log Level Verbose
     *
     * @param message Log message
     **/
    public static final void v(Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_V, buildLogMsg(message));
    }

    /**
     * Log Level Error
     *
     * @param messageTitle Log title
     * @param message      Log message
     **/
    public static final void e(Object messageTitle, Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_E, buildLogMsg(messageTitle, message));
    }

    /**
     * Log Level Warning
     *
     * @param messageTitle Log title
     * @param message      Log message
     **/
    public static final void w(Object messageTitle, Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_W, buildLogMsg(messageTitle, message));
    }

    /**
     * Log Level Information
     *
     * @param messageTitle Log title
     * @param message      Log message
     **/
    public static final void i(Object messageTitle, Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_I, buildLogMsg(messageTitle, message));
    }

    /**
     * Log Level Debug
     *
     * @param messageTitle Log title
     * @param message      Log message
     **/
    public static final void d(Object messageTitle, Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_D, buildLogMsg(messageTitle, message));
    }

    /**
     * Log Level Verbose
     *
     * @param messageTitle Log title
     * @param message      Log message
     **/
    public static final void v(Object messageTitle, Object message) {

        if (AppConfig.DEBUG)
            processLogMsg(LOG_TYPE_V, buildLogMsg(messageTitle, message));
    }

    public static void processLogMsg(int type, String logMsg) {
        if (logMsg.length() > 4000) {
            int chunkCount = logMsg.length() / 4000;     // integer division
            for (int i = 0; i < chunkCount; i++) {
                printLogMsg(type, logMsg.substring(0, 4000));
                logMsg = logMsg.substring(4000);
            }
        }
        printLogMsg(type, logMsg);
//        while (logMsg.length() > 0) {
//            if (logMsg.length() > 4000) {
//                printLogMsg(type, logMsg.substring(0, 4000));
//                logMsg = logMsg.substring(4000);
//            } else {
//                printLogMsg(type, logMsg);
//                break;
//            }
//        }
    }

    public static void printLogMsg(int type, String logMsg) {
        switch (type) {
            case LOG_TYPE_V:
                Log.v(TAG, logMsg);
                break;
            case LOG_TYPE_D:
                Log.d(TAG, logMsg);
                break;
            case LOG_TYPE_I:
                Log.i(TAG, logMsg);
                break;
            case LOG_TYPE_W:
                Log.w(TAG, logMsg);
                break;
            case LOG_TYPE_E:
                Log.e(TAG, logMsg);
                break;
        }
    }

    /**
     * Make log message.
     *
     * @param message Log message
     *
     * @return Log string
     */
    public static String buildLogMsg(Object message) {

        StackTraceElement ste = Thread.currentThread().getStackTrace()[4];

        if (ste == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        sb.append("[");
        sb.append("(");
        sb.append(ste.getFileName());
        sb.append(":");
        sb.append(ste.getLineNumber());
        sb.append(")");
        sb.append("::");
        sb.append(ste.getMethodName());
        sb.append("]");
        sb.append(message);

        return sb.toString();
    }

    /**
     * Make log message.
     *
     * @param messageTitle Log title
     * @param message      Log message
     *
     * @return Log string
     */
    public static String buildLogMsg(Object messageTitle, Object message) {

        StackTraceElement ste = Thread.currentThread().getStackTrace()[4];

        if (ste == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        sb.append("[");
        sb.append("(");
        sb.append(ste.getFileName());
        sb.append(":");
        sb.append(ste.getLineNumber());
        sb.append(")");
        sb.append("::");
        sb.append(ste.getMethodName());
        sb.append("]");
        sb.append(messageTitle);
        sb.append("=");
        sb.append(message);

        return sb.toString();
    }
}