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 static long nativeListener;


    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;
    }

    /**
     * 开启日志采集
     * @param param 日志参数配置，示例："debug verbose stats append realtstamp"
     *              调试模式 [debug]
     *              日志等级 [sensitive verbose info warning error none ] 以上只可取一个值
     *              统计输出 [stats] 是否输出网络流量等统计信息
     *              日志格式 [realtstamp] 长日志格式，格式为：MM-DD hh:mm:ss.xxx，缺省是紧缩格式显示为[秒.微秒]表示程序开始后的时间间隔。
     *              每一次初始化引擎后，打印的日志都追加到当前日志文件末尾 [append]
     * @param listener 回调接口
     * @return “0”代表启动正常，“-1”代表启动失败
     */
    public static int startLogTrace(String param, AVDEngine.ILogListener listener) {
        if (listener == null) {
            return -1;
        }
        AVDEngine.instance();
        nativeListener = AVDEngine.startLogTrace(param, listener);
        return 0;
    }

    /**
     * 停止日志采集
     */
    public static void stopLogTrace() {
        if (nativeListener != 0) {
            AVDEngine.stopLogTrace(nativeListener);
            nativeListener = 0;
        }
    }
}
