package cn.tee3.manager.service;

import cn.tee3.avd.Tlog;

public class LogHelper {

    public static final boolean ENABLE_DEBUG = true;
    private static StringBuilder logBuffer = new StringBuilder();
    private static final String TAG = "LogHelper ";

    private static final String START = " \n\n┏━━━━━━━━━━━━━━━━━━  AVD Log Start  ━━━━━━━━━━━━━━━━━━\n";
    private static final String END =        "┗━━━━━━━━━━━━━━━━━━  AVD Log End    ━━━━━━━━━━━━━━━━━━\n ";

    private LogHelper() {
    }

    public static LogHelper getInstance() {
        return Builder.INSTANCE;
    }

    private static class Builder {
        private static final LogHelper INSTANCE = new LogHelper();
    }

    public LogHelper appendLog(String logStr) {
        synchronized (LogHelper.class) {
            if (ENABLE_DEBUG) {
                if (logBuffer.toString().endsWith(END)) {
                    logBuffer.delete(logBuffer.length() - END.length(), logBuffer.length());
                }

                if (!logBuffer.toString().startsWith(START)) {
                    logBuffer.append(START);
                }

                logStr = "┃\t" + logStr + "\n";
                logBuffer.append(logStr);
            }
            return this;
        }
    }

    public LogHelper printLog() {
        synchronized (LogHelper.class) {
            if (ENABLE_DEBUG) {
                if (!logBuffer.toString().endsWith(END)) {
                    logBuffer.append(END);
                }
                Tlog.w(TAG, logBuffer.toString());
            }
            return this;
        }
    }

    public void logBufferClear() {
        synchronized (LogHelper.class) {
            if (ENABLE_DEBUG) {
                logBuffer.delete(0, logBuffer.length());
            }
        }
    }
}
