package com.wayne.www.waynelib.util;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.wayne.www.waynelib.fdc.log_print.MyLoggerHs;
import com.wayne.www.waynelib.ui.BuildConfig;

/**
 * @desc
 * @info Created by LzPeng on 2020/3/13
 */
public class HsLogUtils {
    //log排查全局关键字
    private static String customTagPrefix = "LzPeng";


    //非release都可打印日志
    private static boolean isRelease() {
//        return BuildConfig.BUILD_TYPE.equals("release");
        return false;
    }

    /**
     * @desc 类名+方法名+行数：MainActivity.onCreate(L:14)
     * @author lzPeng
     * @time 2018/8/10  19:53
     */
    private static String generateTag() {
        StackTraceElement caller = new Throwable().getStackTrace()[2];
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
        return tag;
    }

    /**
     * @desc 自定义tag显示
     * @author lzPeng
     * @time 2019/1/3  20:04
     */
    private static String generateTag(String tagPrefix) {
        StackTraceElement caller = new Throwable().getStackTrace()[2];
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        tag = TextUtils.isEmpty(tagPrefix) ? tag : tagPrefix + ":===" + tag;
        return tag;
    }


    public static void d(String content) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.d(tag, content);
    }

    public static void d(String content, Throwable tr) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.d(tag, content, tr);
    }

    public static void d(String tagV, String content) {
        if (isRelease()) return;
        if (!TextUtils.isEmpty(content))
            Log.d(generateTag(tagV), content);
    }

    public static void e(String content) {
        if (isRelease()) return;
        String tag = generateTag();

//        Log.e(tag, content);
        MyLoggerHs.getLogger(tag).e(content);
    }

    public static void e(String content, Throwable tr) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.e(tag, content, tr);
    }

    public static void e(String tagE, String content) {
        if (isRelease()) return;
        MyLoggerHs.getLogger(tagE).e(content);
//        Log.e(generateTag(tagE), content);
    }

    public static void i(String content) {
        if (isRelease()) return;
        String tag = generateTag();
        Log.i(tag, content);
    }

    public static void i(String tagI, String content) {
        if (isRelease()) return;
        String tag = generateTag(tagI);
        Log.i(tag, content);
    }

    public static void i(String content, Throwable tr) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.i(tag, content, tr);
    }

    public static void v(String content) {
        if (isRelease() || content == null) return;
        String tag = generateTag();
        Log.v(tag, content);
    }


//    public static void v(String tagV, String content) {
//        if (isRelease()) return;
//        if (!TextUtils.isEmpty(content))
//            Log.v(generateTag(tagV), content);
//    }

    public static void o(Object content) {
        if (isRelease()) return;
        String tag = generateTag();
        if (content instanceof String) {
            Log.v(tag, content.toString());
        } else {
            try {
                Log.v(tag, new Gson().toJson(content));
            } catch (Exception e) {
                v("该obj有误");
                return;
            }
        }
    }

    public static void o(String tagO, Object content) {
        if (isRelease()) return;
        String tag = generateTag(tagO);
        if (content instanceof String) {
            v(tag, content.toString());
//            MyLoggerHs.getLogger(tag).v(content.toString());
//            Log.v(tag, content.toString());
        } else {
            try {
                v(tag, new Gson().toJson(content));
//                MyLoggerHs.getLogger(tag).v(new Gson().toJson(content));
//                Log.v(tag, new Gson().toJson(content));
            } catch (Exception e) {
                v(tag, "该obj有误");
//                MyLoggerHs.getLogger(tag).v("该obj有误");
//                v(tag, "该obj有误");
            }
        }
    }

    //信息太长,分段打印
    public static void v(String tag, String msg) {
        if (isRelease() || TextUtils.isEmpty(msg)) return;
        //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
        //  把4*1024的MAX字节打印长度改为2001字符数
        int longCount = 2 * 1024;
        tag = generateTag(tag);
//        int max_str_length = 2001 - tag.length();
        int max_str_length = longCount - tag.length();
        //大于4000时
        while (msg.length() > max_str_length) {
            MyLoggerHs.getLogger(tag).v(msg.substring(0, max_str_length));
//            Log.v(tag, msg.substring(0, max_str_length));
            msg = msg.substring(max_str_length);
        }
        //剩余部分
        MyLoggerHs.getLogger(tag).v(msg);
//        Log.v(tag, msg);
    }

    public static void o(String tagO, String tag1, Object content) {
        if (isRelease()) return;
        String tag = generateTag(tagO);
        if (content instanceof String) {
            v(tag,tag1 + "：" + content.toString());
//            MyLoggerHs.getLogger(tag).v(tag1 + "：" + content.toString());
//            Log.v(tag, content.toString());
        } else {
            try {
                v(tag, tag1 + "：" + new Gson().toJson(content));
//                MyLoggerHs.getLogger(tag).v(tag1 + "：" + new Gson().toJson(content));
//                Log.v(tag, new Gson().toJson(content));
            } catch (Exception e) {
                v(tag, tag1 + "：" + "该obj有误");
//                MyLoggerHs.getLogger(tag).v(tag1 + "：" + "该obj有误");
//                v(tag, "该obj有误");
            }
        }
    }


    public static void v(String content, Throwable tr) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.v(tag, content, tr);
    }

    public static void w(String tagV, String content) {
        if (isRelease()) return;
        if (!TextUtils.isEmpty(content))
            Log.w(generateTag(tagV), content);
    }

    public static void w(String content) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.w(tag, content);
    }


    public static void w(String content, Throwable tr) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.w(tag, content, tr);
    }

    public static void w(Throwable tr) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.w(tag, tr);
    }

    public static void wtf(String content) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.wtf(tag, content);
    }

    public static void wtf(String tagV, String content) {
        if (isRelease()) return;
        if (!TextUtils.isEmpty(content))
            Log.wtf(generateTag(tagV), content);
    }

    public static void wtf(String content, Throwable tr) {
        if (isRelease()) return;
        String tag = generateTag();

        Log.wtf(tag, content, tr);
    }

    public static void wtf(Throwable tr) {
        if (isRelease()) return;
        String tag = generateTag();
        Log.wtf(tag, tr);
    }
}
