// Author: NiuJiuRu
// Email: niujiuru@qq.com

package baseapp

import "C"

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	"github.com/sirupsen/logrus"
	"gopkg.in/ini.v1"
	"gopkg.in/natefinch/lumberjack.v2"
)

// 日志级别
type level uint32

const (
	LEVEL_TRACE level = level(logrus.TraceLevel) // trace
	LEVEL_DEBUG       = level(logrus.DebugLevel) // debug
	LEVEL_INFO        = level(logrus.InfoLevel)  // info
	LEVEL_WARN        = level(logrus.WarnLevel)  // warning
	LEVEL_ERROR       = level(logrus.ErrorLevel) // error
	LEVEL_FATAL       = level(logrus.FatalLevel) // fatal, 会结束程序运行, 会自动调用os.Exit(1)
	LEVEL_PANIC       = level(logrus.PanicLevel) // panic, 会触发程序崩溃, 不处理的话, 程序退出
)

func (l *level) fromString(s string) {
	switch strings.ToLower(s) {
	case "trace":
		*l = LEVEL_TRACE
	case "debug":
		*l = LEVEL_DEBUG
	case "warn":
		*l = LEVEL_WARN
	case "error":
		*l = LEVEL_ERROR
	case "fatal":
		*l = LEVEL_FATAL
	case "panic":
		*l = LEVEL_PANIC
	default:
		*l = LEVEL_INFO
	}
}

// 输出目标
type target uint32

const (
	TARGET_NONE    target = iota      // 不输出日志
	TARGET_CONSOLE target = 1 << iota // 控制台输出
	TARGET_FILE                       // 输出到文件
)

func (t *target) fromString(s string) {
	switch strings.ToLower(s) {
	case "console":
		*t = TARGET_CONSOLE
	case "file":
		*t = TARGET_FILE
	case "all":
		*t = (TARGET_CONSOLE | TARGET_FILE)
	default:
		*t = TARGET_NONE
	}
}

// 输出格式
type formatter struct{}

func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) {
	msg := ""
	pid := os.Getpid()

	if entry.Caller != nil {
		file := filepath.Base(entry.Caller.File)
		line := entry.Caller.Line
		msg = fmt.Sprintf("[%s, PID: %d] [%s] (%s:%d): %s\n", entry.Time.Format("2006-01-02 15:04:05"), pid, entry.Level.String(), file, line, entry.Message)
	} else {
		msg = fmt.Sprintf("[%s, PID: %d] [%s]: %s\n", entry.Time.Format("2006-01-02 15:04:05"), pid, entry.Level.String(), entry.Message)
	}

	return []byte(msg), nil
}

// 日志配置
type config struct {
	// 日志输出级别
	Level level

	// 日志输出目标
	Target target

	// 每个日志文件的最大大下, 以"MB"为单位
	MaxSize int `ini:"MaxFileSize"`

	// 保留日志文件的最大天数, 以"天"为单位
	MaxAge int `ini:"MaxReserveDays"`

	// 保留的最大备份文件数量, 以"个"为单位
	MaxBackups int `ini:"MaxBackupFileCnts"`

	// 轮转文件是否压缩(gzip)减少占用的空间
	Compress bool `ini:"IsCompress"`
}

var (
	Logger        *logrus.Logger
	logFileWriter *lumberjack.Logger
	cfgLog        = &config{
		Level:      LEVEL_TRACE,
		Target:     TARGET_CONSOLE,
		MaxSize:    0,
		MaxAge:     0,
		MaxBackups: 0,
		Compress:   true,
	}
)

func InitLogger() {
	cfgFile := filepath.Join(CFG_DIR, "config.ini")
	cfgIni, err := ini.Load(cfgFile) // 从配置文件中加载相关配置项覆盖默认值
	if err == nil && cfgIni.HasSection("Log") {
		tmpCfgLog := *cfgLog
		err = cfgIni.Section("Log").MapTo(&tmpCfgLog)
		if err != nil {
			fmt.Printf("Failed to load the \"[Log]\" section data from the \"%s\" file: %v\n", cfgFile, err)
		} else {
			*cfgLog = tmpCfgLog
		}
		if cfgIni.Section("Log").HasKey("Level") {
			cfgLog.Level.fromString(cfgIni.Section("Log").Key("Level").String())
		}
		if cfgIni.Section("Log").HasKey("Target") {
			cfgLog.Target.fromString(cfgIni.Section("Log").Key("Target").String())
		}
	}

	Logger = logrus.New()
	Logger.SetLevel(logrus.Level(cfgLog.Level))
	if cfgLog.Level == LEVEL_TRACE || cfgLog.Level == LEVEL_DEBUG {
		Logger.SetReportCaller(true) // 使得输出日志时能够获取到文件名和行号
	}
	Logger.SetFormatter(new(formatter))

	writers := make([]io.Writer, 0)
	if (cfgLog.Target & TARGET_CONSOLE) != 0 {
		writers = append(writers, os.Stdout)
	}
	if (cfgLog.Target & TARGET_FILE) != 0 {
		logFile := filepath.Join(LOG_DIR, EXEC_FILENAME+".log")
		logFileWriter = &lumberjack.Logger{Filename: logFile, MaxSize: cfgLog.MaxSize, MaxAge: cfgLog.MaxAge,
			MaxBackups: cfgLog.MaxBackups, Compress: cfgLog.Compress}
		writers = append(writers, logFileWriter)
	}

	if len(writers) > 0 {
		multiWriter := io.MultiWriter(writers...)
		Logger.SetOutput(multiWriter)
	} else {
		Logger.SetOutput(io.Discard) // 不输出
	}
}

func ExitLogger() {
	if Logger != nil {
		Logger.Out = io.Discard
	}
	if logFileWriter != nil {
		logFileWriter.Close() // 关闭日志文件, 只是将日志提交到内核缓存区, 如果要强制落盘, 避免日志丢失,
		// 需要修改开源库代码, 在关闭函数中调用"l.file.Sync()"方法并等待一会儿, 20251021 by niujiuru.
		logFileWriter = nil
	}
}

//export LogFromGo
func LogFromGo(level *C.char, file *C.char, line C.int, message *C.char) {
	goLevel := C.GoString(level)
	goMessage := C.GoString(message)

	if (cfgLog.Level == LEVEL_TRACE || cfgLog.Level == LEVEL_DEBUG) && line > 0 {
		gofile := filepath.Base(C.GoString(file))
		goline := int(line)
		goMessage = fmt.Sprintf("(from %s:%d): %s", gofile, goline, goMessage)
	}

	switch goLevel {
	case "trace":
		Logger.Tracef("%s", goMessage)
	case "debug":
		Logger.Debugf("%s", goMessage)
	case "info":
		Logger.Infof("%s", goMessage)
	case "warn":
		Logger.Warnf("%s", goMessage)
	case "error":
		Logger.Errorf("%s", goMessage)
	case "fatal":
		Logger.Fatalf("%s", goMessage)
	}
}
