package log

import (
	"os"
	"path/filepath"

	"github.com/adamweixuan/getty"
)

var (
	runLogger     getty.Logger
	eventLogger   getty.Logger
	heartLogger   getty.Logger
	latencyLogger getty.Logger
)

const (
	logDir  = "./output/log/"
	run     = "run.log"
	event   = "event.log"
	heart   = "heart.log"
	latency = "latency.log"
)

func getCurPath() string {
	exePath, err := os.Executable()
	if err != nil {
		panic(err)
	}
	return filepath.Dir(exePath)
}

func Init(level int) {
	curPath := getCurPath()
	base := filepath.Join(curPath, logDir)
	if _, err := os.Stat(base); err != nil {
		if os.IsNotExist(err) {
			if err := os.MkdirAll(base, os.ModePerm); err != nil {
				panic(err)
			}
		} else {
			panic(err)
		}
	}

	if len(curPath) == 0 {
		panic("invalid current path")
	}
	runLogger = getty.NewLogger(filepath.Join(base, run), getty.LoggerLevel(level))
	eventLogger = getty.NewLogger(filepath.Join(base, event), getty.LoggerLevel(level))
	heartLogger = getty.NewLogger(filepath.Join(base, heart), getty.LoggerLevel(level))
	latencyLogger = getty.NewLogger(filepath.Join(base, latency), getty.LoggerLevel(level))
}

func Run() getty.Logger {
	return runLogger
}

func Event() getty.Logger {
	return eventLogger
}

func Heart() getty.Logger {
	return heartLogger
}

func Latency() getty.Logger {
	return latencyLogger
}
