package tilog

import (
	"context"

	"github.com/pingcap/log"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Debug(ctx context.Context, msg string, fields ...zap.Field) {
	WithContextL(ctx).WithOptions(zap.AddCallerSkip(1)).Debug(msg, fields...)
}

// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Info(ctx context.Context, msg string, fields ...zap.Field) {
	WithContextL(ctx).WithOptions(zap.AddCallerSkip(1)).Info(msg, fields...)
}

// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Warn(ctx context.Context, msg string, fields ...zap.Field) {
	WithContextL(ctx).WithOptions(zap.AddCallerSkip(1)).Warn(msg, fields...)
}

// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Error(ctx context.Context, msg string, fields ...zap.Field) {
	WithContextL(ctx).WithOptions(zap.AddCallerSkip(1)).Error(msg, fields...)
}

// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
func Panic(ctx context.Context, msg string, fields ...zap.Field) {
	WithContextL(ctx).WithOptions(zap.AddCallerSkip(1)).Panic(msg, fields...)
}

// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func Fatal(ctx context.Context, msg string, fields ...zap.Field) {
	WithContextL(ctx).WithOptions(zap.AddCallerSkip(1)).Fatal(msg, fields...)
}

// PlainXXX is alias to the orgional log function without Context
// It doesn't log out trace id
func PlainDebug(msg string, fields ...zap.Field) {
	log.L().WithOptions(zap.AddCallerSkip(1)).Debug(msg, fields...)
}

func PlainInfo(msg string, fields ...zap.Field) {
	log.L().WithOptions(zap.AddCallerSkip(1)).Info(msg, fields...)
}

func PlainWarn(msg string, fields ...zap.Field) {
	log.L().WithOptions(zap.AddCallerSkip(1)).Warn(msg, fields...)
}

func PlainError(msg string, fields ...zap.Field) {
	log.L().WithOptions(zap.AddCallerSkip(1)).Error(msg, fields...)
}

func PlainPanic(msg string, fields ...zap.Field) {
	log.L().WithOptions(zap.AddCallerSkip(1)).Panic(msg, fields...)
}

func PlainFatal(msg string, fields ...zap.Field) {
	log.L().WithOptions(zap.AddCallerSkip(1)).Fatal(msg, fields...)
}

// With creates a child logger and adds structured context to it.
// Fields added to the child don't affect the parent, and vice versa.
func With(fields ...zap.Field) *zap.Logger {
	return log.With(fields...)
}

// SetLevel alters the logging level.
func SetLevel(l zapcore.Level) {
	log.SetLevel(l)
}

// GetLevel gets the logging level.
func GetLevel() zapcore.Level {
	return log.GetLevel()
}
