package logger

import (
	"os"
	"time"

	formatter "github.com/antonfisher/nested-logrus-formatter"
	"github.com/sirupsen/logrus"

	"proxy/util/logger_conf"
	"proxy/util/logger_util"
)

var (
	log                    *logrus.Logger
	AppLog                 *logrus.Entry
	InitLog                *logrus.Entry
	CfgLog                 *logrus.Entry
	MysqlLog               *logrus.Entry
	/*Bdtpolicylog           *logrus.Entry
	PolicyAuthorizationlog *logrus.Entry
	AMpolicylog            *logrus.Entry
	SMpolicylog            *logrus.Entry
	Consumerlog            *logrus.Entry
	UtilLog                *logrus.Entry
	CallbackLog            *logrus.Entry
	OamLog                 *logrus.Entry
	CtxLog                 *logrus.Entry
	ConsumerLog            *logrus.Entry
	GinLog                 *logrus.Entry
	NotifyEventLog         *logrus.Entry*/
	ProvLog                *logrus.Entry
	RedisLog               *logrus.Entry
	RestLog                *logrus.Entry
	CanalLog               *logrus.Entry
)

const (
	FieldRemoteAddr     string = "remote_addr"
)

func init() {
	log = logrus.New()
	log.SetReportCaller(true)

	log.Formatter = &formatter.Formatter{
		TimestampFormat: time.RFC3339,
		TrimMessages:    true,
		NoFieldsSpace:   true,
		HideKeys:        true,
		FieldsOrder:     []string{"component", "category", FieldRemoteAddr},
	}

	free5gcLogHook, err := logger_util.NewFileHook(logger_conf.Free5gcLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
	if err == nil {
		log.Hooks.Add(free5gcLogHook)
	}

	AppLog = log.WithFields(logrus.Fields{"component": "proxy", "category": "App"})
	InitLog = log.WithFields(logrus.Fields{"component": "proxy", "category": "Init"})
	CfgLog = log.WithFields(logrus.Fields{"component": "proxy", "category": "Cfg"})
	MysqlLog = log.WithFields(logrus.Fields{"component": "proxy", "category": "Mysql"})
	RedisLog = log.WithFields(logrus.Fields{"component": "proxy", "category": "Redis"})
	RestLog = log.WithFields(logrus.Fields{"component": "proxy", "category": "Rest"})
	CanalLog = log.WithFields(logrus.Fields{"component": "proxy", "category": "Canal"})
	ProvLog = log.WithFields(logrus.Fields{"component": "proxy", "category": "Prov"})
}

func SetLogLevel(level logrus.Level) {
	log.SetLevel(level)
}

func SetReportCaller(set bool) {
	log.SetReportCaller(set)
}
