package com.yutou.bilibili.Tools;


import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Log {
    private static Logger logger;

    public static void i(Object log) {
        if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "logout"))) {
            return;
        }
        System.out.printf("[%s]%s%n",
                AppTools.getToDayNowTimeToString(),
                log
        );

    }

    public static void e(Exception e) {
        if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "logout"))) {
            return;
        }
        i(e.getMessage());
        e.printStackTrace();
    }

    public static void i(String timer, int roomId, Object log) {
        String logFlag = RedisTools.get("live-log", 1);
        if (logFlag!=null&&logFlag.startsWith("true")) {
            if (logFlag.contains("|")) {
                if (log.toString().contains(logFlag.split("\\|")[1])) {
                    getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString());
                }
            } else {
                getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString());
            }
        }
    }

    public static Logger getLogger(String fileName) {
        if (logger == null) {
            try {
                if (!new File("logs").exists()) {
                    new File("logs").mkdirs();
                }
                logger = Logger.getLogger("Live-Log");
                FileHandler handler = new FileHandler("logs" + File.separator + fileName + ".log");
                logger.addHandler(handler);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return logger;
    }
}
