package com.ruoyi.common.dev.mybatis;

import com.ruoyi.common.dev.Konsole;
import org.apache.ibatis.logging.Log;

import static java.lang.System.out;
import static java.lang.System.err;

public class MybatisStdOutputLog implements Log {
    private String mapperStatement;
    private boolean disable = false;

    private final static boolean ENABLE_OUTPUT = true;

    static final int SQL_START_STYLE = Konsole.GenStyle(Konsole.FC_BLACK, Konsole.FS_BOLD, Konsole.BC_CYAN, Konsole.S_BG_ENABLED);
    static final int SQL_STATEMENT_STYLE = Konsole.GenStyle(Konsole.FC_BLUE, Konsole.FS_BOLD);
    static final int SQL_PARAMETER_STYLE = Konsole.GenStyle(Konsole.FC_CYAN, Konsole.FS_BOLD);
    static final int SQL_RESULT_STYLE = Konsole.GenStyle(Konsole.FC_PURPLE, Konsole.FS_BOLD, Konsole.BC_YELLOW, Konsole.S_BG_ENABLED);
    static final int SQL_ROWS_STYLE = Konsole.GenStyle(Konsole.FC_GREEN);
    static final int SQL_COLUMNS_STYLE = Konsole.GenStyle(Konsole.FC_YELLOW, Konsole.FS_BOLD);
    static final int SQL_STYLE = Konsole.GenStyle(Konsole.FC_RED);

    // 不输出的查询
    private static final String[] UnhandleClass = {
            "org.activiti.engine.impl.persistence.entity.JobEntityImpl.selectJobsToExecute",
            "org.activiti.engine.impl.persistence.entity.TimerJobEntityImpl.selectTimerJobsToExecute",
            "org.activiti.engine.impl.persistence.entity.JobEntityImpl.selectExpiredJobs",
    };

    public MybatisStdOutputLog(String clazz) {
        SetMapperStatement(clazz);
    }

    private void SetMapperStatement(String clazz)
    {
        mapperStatement = clazz;
        for(String unhandleClass : UnhandleClass)
        {
            if(unhandleClass.equals(mapperStatement))
                disable = true;
        }
    }

    public boolean isDebugEnabled() {
        return ENABLE_OUTPUT && !disable;
    }

    public boolean isTraceEnabled() {
        return ENABLE_OUTPUT && !disable;
    }

    public void error(String s, Throwable e) {
        SQLQueue.Exception(mapperStatement, s, e);
    }

    public void error(String s) {
        SQLQueue.Error(mapperStatement, s);
    }

    public void debug(String s) {
        SQLQueue.Debug(mapperStatement, s);
    }

    public void trace(String s) {
        SQLQueue.Trace(mapperStatement, s);
    }

    public void warn(String s) {
        SQLQueue.Warning(mapperStatement, s);
    }

    static void cout(String str)
    {
        out.println(str);
    }

    static void cerr(String str)
    {
        err.println(str);
    }

    static void clog(String str)
    {
    }

    static void cout()
    {
        out.println();
    }

    static void cerr()
    {
        err.println();
    }

    static void clog()
    {
    }

    static void cout(String str, int style)
    {
        out.println(Konsole.MakeText(str, style));
    }

    static void cerr(String str, int style)
    {
        err.println(Konsole.MakeText(str, style));
    }

    static void clog(String str, int style)
    {
    }
}
