package com.wayne.www.waynelib.fdc.log_print;


import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import com.wayne.www.waynelib.ui.BuildConfig;
import com.wayne.www.waynelib.util.DateUtils;

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


/**
 * @desc 日志打印
 * @info Created by LzPeng on 2019/9/22 14:08
 */
public class MyLoggerHs {
    public final static String tag = "";
    private static SaveLogStrategyHs saveLogStrategy;
    private final static boolean logFlag = true;
    private static MyLoggerHs logger;
    private int logLevel = Log.VERBOSE;
    private static boolean isDebug = BuildConfig.DEBUG;
    //    private static boolean isDebug = true;//不管怎样都给保存日志和打印
    private String customTag = null;

    private MyLoggerHs(String customTag) {
        this.customTag = customTag;
    }

    public void initSaveStrategy(Context context) {
        if (null != saveLogStrategy) {
            return;
        }
        final int MAX_BYTES = 1024 * 1024;
        HandlerThread ht = new HandlerThread("MyLoggerHs");
        ht.start();
        Handler handler = new SaveLogStrategyHs.WriteHandler(ht.getLooper(), MAX_BYTES);
        saveLogStrategy = new SaveLogStrategyHs(handler);
    }


    public static MyLoggerHs getLogger(String tag) {
        if (logger == null) {
            logger = new MyLoggerHs(tag);
        }
        logger.setTag(tag);
        return logger;
    }

    private void setTag(String tag) {
        customTag = tag;
    }

    public static MyLoggerHs getLogger() {
        if (logger == null) {
            logger = new MyLoggerHs(tag);
        }
        return logger;
    }

    /**
     * Verbose(2) 级别日志
     *
     * @param str String
     */
    public void v(Object str) {
        logLevel = Log.VERBOSE;
        logPrint(logLevel, str);
    }

    /**
     * Debug(3) 级别日志
     *
     * @param str String
     */
    public void d(Object str) {
        logLevel = Log.DEBUG;
        logPrint(logLevel, str);
    }

    /**
     * Info(4) 级别日志
     *
     * @param str String
     */
    public void i(Object str) {
        logLevel = Log.INFO;
        logPrint(logLevel, str);
    }

    /**
     * Warn(5) 级别日志
     *
     * @param str String
     */
    public void w(Object str) {
        logLevel = Log.WARN;
        logPrint(logLevel, str);
    }

    /**
     * Error(6) 级别日志
     *
     * @param str String
     */
    public void e(Object str) {
        logLevel = Log.ERROR;
        logPrint(logLevel, str);
    }

    private void logPrint(int logLevel, Object msg) {
        if (null != saveLogStrategy) {//日志存储（无论开发还是线上都存）
            saveLogStrategy.log(Log.ERROR, customTag, new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()) + " " + customTag + " - " + msg);
        }
        // FIXME 暂时屏蔽，看着啰嗦
        // Log.println(logLevel, customTag, customTag + " - " + msg);
    }

}
