package com.sobot.network.http.log;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 网络日志工具类
 * @Author: znw
 * @Date: 2021/7/15 下午5:46
 * @Version: 1.0
 */
public class SobotNetLogUtils {

    // 日志显示开关
    public static boolean isDebug = false;
    // 日志缓存各级别开关
    public static boolean allowD = false;
    public static boolean allowE = false;
    public static boolean allowI = false;
    public static boolean allowV = false;
    public static boolean allowW = false;
    public static boolean allowWtf = false;


    private SobotNetLogUtils() {
    }


    /**
     * 日志显示设置
     *
     * @param isShowLog true 显示日志信息 默认false不显示
     */
    public static void setShowDebug(Boolean isShowLog) {
        if (isShowLog) {
            SobotNetLogUtils.isDebug = true;
            SobotNetLogUtils.allowI = true;
            SobotNetLogUtils.allowE = true;
            SobotNetLogUtils.allowD = true;
        } else {
            SobotNetLogUtils.isDebug = false;
            SobotNetLogUtils.allowI = false;
            SobotNetLogUtils.allowE = false;
            SobotNetLogUtils.allowD = true;
        }
    }

    /**
     * 获取日志过滤 tag
     *
     * @return
     */
    public static String generateTag() {
        return "sobot_log";
    }


    /**
     * 获取时间并且格式化
     */
    private static String getCurrentTime(String format) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String currentTime = dateFormat.format(date);
        return currentTime;
    }

    public static void d(String content) {
        if (isDebug && allowD) {
            String tag = generateTag();
            Log.d(tag, content);
        }
    }

    public static void d(String content, Throwable tr) {
        if (isDebug && allowD) {
            String tag = generateTag();
            Log.d(tag, content, tr);
        }
    }

    public static void e(String content) {
        if (isDebug && allowE) {
            String tag = generateTag();
            Log.e(tag, content);
        }
    }

    public static void e(String content, Throwable tr) {
        if (isDebug && allowE) {
            String tag = generateTag();
            Log.e(tag, content, tr);
        }
    }

    //分段打印日志每次最大长度
    private static final int MAX_LENGTH = 3 * 1024;

    public static void i(String str) {
        if (isDebug && allowI) {
            String tag = generateTag();
            if(str==null){
                return;
            }
            if(str.length()>4*MAX_LENGTH){
                str = str.substring(0,4*MAX_LENGTH);
            }
            StringBuilder content = new StringBuilder(str);
            //采取分段打印日志的方法：当长度超过4000时，我们就来分段截取打印
            if (content.length() > MAX_LENGTH) {
                String part = content.substring(0, MAX_LENGTH);
                Log.i(tag + "分段打印开始", part);

                part = content.substring(MAX_LENGTH, content.length());
                if ((content.length() - MAX_LENGTH) > MAX_LENGTH) {
                    i(part);
                } else {
                    Log.i(tag + "分段打印结束", part);
                }
            } else {
                //直接打印
                Log.i(tag, str);
            }
        }
    }

    public static void i(String content, Throwable tr) {
        if (isDebug && allowI) {
            String tag = generateTag();
            Log.i(tag, content, tr);
        }
    }

    public static void v(String content) {
        if (isDebug && allowV) {
            String tag = generateTag();
            Log.v(tag, content);
        }
    }

    public static void v(String content, Throwable tr) {
        if (isDebug && allowV) {
            String tag = generateTag();
            Log.v(tag, content, tr);
        }
    }

    public static void w(String content) {
        if (isDebug && allowW) {
            String tag = generateTag();
            Log.w(tag, content);
        }
    }

    public static void w(String content, Throwable tr) {
        if (isDebug && allowW) {
            String tag = generateTag();
            Log.w(tag, content, tr);
        }
    }

    public static void w(Throwable tr) {
        if (isDebug && allowW) {
            String tag = generateTag();
            Log.w(tag, tr);
        }
    }

    public static void wtf(String content) {
        if (isDebug && allowWtf) {
            String tag = generateTag();
            Log.wtf(tag, content);
        }
    }

    public static void wtf(String content, Throwable tr) {
        if (isDebug && allowWtf) {
            String tag = generateTag();
            Log.wtf(tag, content, tr);
        }
    }

    public static void wtf(Throwable tr) {
        if (isDebug && allowWtf) {
            String tag = generateTag();
            Log.wtf(tag, tr);
        }
    }
}