package cn.tee3.avd;

import android.util.Log;

import tee3.webrtc.Loggable;
import tee3.webrtc.Logging;

/**
 * Created by Administrator on 2017/4/22.
 */

public class Tlog {
    public static final int VERBOSE = 2;

    /**
     * Priority constant for the println method; use Tlog.d.
     */
    public static final int DEBUG = 3;

    /**
     * Priority constant for the println method; use Tlog.i.
     */
    public static final int INFO = 4;

    /**
     * Priority constant for the println method; use Tlog.w.
     */
    public static final int WARN = 5;

    /**
     * Priority constant for the println method; use Tlog.e.
     */
    public static final int ERROR = 6;


    private Tlog() {
    }

    static boolean log2sdk = false;
    static boolean log2sdk_v = false;

    static private class RtcLogger implements Loggable {
        public RtcLogger(){
        }
        @Override
        public void onLogMessage(String message, Logging.Severity severity, String tag) {
            switch (severity) {
                case LS_ERROR:
                    Tlog.e(tag, message);
                    break;
                case LS_WARNING:
                    Tlog.w(tag, message);
                    break;
                case LS_INFO:
                    Tlog.i(tag, message);
                    break;
                case LS_VERBOSE:
                    Tlog.d(tag, message);
                    break;
                default:
                    //Tlog.v(tag, message);
                    break;
            }
        }
    }
    static RtcLogger rtclogger;
    public static Loggable getRtcLogger() {
        if (null == rtclogger) {
            rtclogger = new RtcLogger();
        }
        return rtclogger;
    }

    public static void enableLog2SDK(boolean enable) {
        Log.i("Tlog", "enableLog2SDK:"+enable);
        log2sdk = enable;
    }

    public static int v(String tag, String msg) {
        if (!log2sdk_v)
            return 0;
        return log2sdk_v ? sdk_v(tag, msg) : Log.v(tag, msg);
    }

    public static int d(String tag, String msg) {
        return log2sdk ? sdk_d(tag, msg) : Log.d(tag, msg);
    }

    public static int i(String tag, String msg) {
        return log2sdk ? sdk_i(tag, msg) : Log.i(tag, msg);
    }

    public static int w(String tag, String msg) {
        return log2sdk ? sdk_w(tag, msg) : Log.w(tag, msg);
    }
    public static int w(String tag, String amsg, Throwable tr) {
        String msg = amsg + '\n' + Log.getStackTraceString(tr);
        return log2sdk ? sdk_w(tag, msg) : Log.w(tag, msg);
    }

    public static int e(String tag, String msg) {
        return log2sdk ? sdk_e(tag, msg) : Log.e(tag, msg);
    }
    public static int e(String tag, String amsg, Throwable tr) {
        String msg = amsg + '\n' + Log.getStackTraceString(tr);
        return log2sdk ? sdk_e(tag, msg) : Log.e(tag, msg);
    }

    static String mergeTagMsg(String tag, String amsg) {
        return (tag+"::"+amsg);
    }
    static int sdk_v(String tag, String amsg) {
        String msg = mergeTagMsg(tag,amsg);
        AVDEngine.log2SDK(AVDEngine.LoggingSeverity.LS_VERBOSE, msg);
        return 0;
    }
    static int sdk_d(String tag, String amsg) {
        String msg = mergeTagMsg(tag,amsg);
        AVDEngine.log2SDK(AVDEngine.LoggingSeverity.LS_VERBOSE, msg);
        return 0;
    }
    static int sdk_i(String tag, String amsg) {
        String msg = mergeTagMsg(tag,amsg);
        AVDEngine.log2SDK(AVDEngine.LoggingSeverity.LS_INFO, msg);
        return 0;
    }
    static int sdk_w(String tag, String amsg) {
        String msg = mergeTagMsg(tag,amsg);
        AVDEngine.log2SDK(AVDEngine.LoggingSeverity.LS_WARNING, msg);
        return 0;
    }
    static int sdk_e(String tag, String amsg) {
        String msg = mergeTagMsg(tag,amsg);
        AVDEngine.log2SDK(AVDEngine.LoggingSeverity.LS_ERROR, msg);
        return 0;
    }

}
