package logger

import (
	"context"
	"fmt"
	"io"
	"os"
	"reflect"
	"strings"
	"sync"

	"github.com/fiskerinc/cloud-services/pkg/utils/envtool"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/diode"
	"github.com/rs/zerolog/pkgerrors"
)

const RING_BUFFER_SIZE = 1000

// Logger is the global logger
var Logger *zerolog.Logger
var once sync.Once
var wr diode.Writer

func GetLogger() *zerolog.Logger {
	once.Do(func() {
		envLogLevel := envtool.GetEnv("LOG_LEVEL", "info")
		SetGlobalLevel(envLogLevel)
		zerolog.ErrorFieldName = "message"
		zerolog.ErrorStackMarshaler = MarshalStack

		wr = diode.NewWriter(os.Stdout, RING_BUFFER_SIZE, 0, func(missed int) {
			Logger.Error().Msgf("logger dropped %d messages", missed)
		})
		logger := zerolog.New(wr).With().Timestamp().Caller().Logger()
		Logger = &logger
	})
	return Logger
}

// Cleans up the diode logger
func Close() {
	wr.Close()
}

func MarshalStack(err error) interface{} {
	stack := pkgerrors.MarshalStack(err)
	if stack == nil {
		return nil
	}

	stackS, ok := stack.([]map[string]string)
	if !ok {
		return stack
	}

	res := ""
	for _, v := range stackS {
		s := fmt.Sprintf("%v", v)
		s = strings.TrimPrefix(s, "map[")
		s = strings.TrimSuffix(s, "]")
		res += s + "\n"
	}

	return res
}

// SetGlobalLevel provides static function to zerolog
func SetGlobalLevel(level string) error {
	l, err := zerolog.ParseLevel(level)
	if err != nil {
		return err
	}

	zerolog.SetGlobalLevel(l)
	return nil
}

// Event is a custom event
type Event struct {
	*zerolog.Event
}

// Confidential logs a struct with potentially sensitive fields that should
// be masked
//
// You must call Msg on the returned event in order to send the event
func (e *Event) Confidential(i interface{}) *Event {
	v := reflect.ValueOf(i)
	if v.Type().Kind() != reflect.Ptr {
		v = reflect.New(reflect.TypeOf(i))
	}
	v = v.Elem()

	m := recursiveMask(v)
	for i := 0; i < m.NumField(); i++ {
		varName := m.Type().Field(i).Name
		varValue := m.Field(i).Interface()
		e.Interface(varName, varValue)
	}

	return e
}

// recursiveMask goes through reflect.Value and masks fields
func recursiveMask(v reflect.Value) reflect.Value {
	for i := 0; i < v.NumField(); i++ {
		varName := v.Type().Field(i).Name
		varType := v.Type().Field(i).Type
		varValue := v.Field(i).Interface()

		if varType.Kind() == reflect.Struct {
			v.Field(i).Set(recursiveMask(v.Field(i)))
		} else if varType.Kind() == reflect.Ptr {
			if v.Field(i).IsNil() {
				continue
			}
			o := v.Field(i).Elem()
			if o.Type().Kind() == reflect.Struct {
				recursiveMask(o)
			}
		} else {
			switch varName {
			case "Email":
				v.Field(i).Set(reflect.ValueOf(MaskEmail(varValue.(string))))
			case "Password":
				v.Field(i).Set(reflect.ValueOf(MaskPassword(varValue.(string))))
			case "Phone":
				v.Field(i).Set(reflect.ValueOf(MaskPhoneNumber(varValue.(string))))
			}
		}
	}

	return v
}

// github.com/zerolog/log

// Output duplicates the global logger and sets w as its output.
func Output(w io.Writer) zerolog.Logger {
	return GetLogger().Output(w)
}

// With creates a child logger with the field added to its context.
func With() zerolog.Context {
	return GetLogger().With()
}

// Level creates a child logger with the minimum accepted level set to level.
func Level(level zerolog.Level) zerolog.Logger {
	return GetLogger().Level(level)
}

// Sample returns a logger with the s sampler.
func Sample(s zerolog.Sampler) zerolog.Logger {
	return GetLogger().Sample(s)
}

// Hook returns a logger with the h Hook.
func Hook(h zerolog.Hook) zerolog.Logger {
	return GetLogger().Hook(h)
}

// Err starts a new message with error level with err as a field if not nil or
// with info level if err is nil.
//
// You must call Msg on the returned event in order to send the event.
func Err(err error) *Event {
	return &Event{GetLogger().Err(err)}
}

// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func Trace() *Event {
	return &Event{GetLogger().Trace()}
}

// Debug starts a new message with debug level.
//
// You must call Msg on the returned event in order to send the event.
func Debug() *Event {
	return &Event{GetLogger().Debug()}
}

// Info starts a new message with info level.
//
// You must call Msg on the returned event in order to send the event.
func Info() *Event {
	return &Event{GetLogger().Info()}
}

// Warn starts a new message with warn level.
//
// You must call Msg on the returned event in order to send the event.
func Warn() *Event {
	return &Event{GetLogger().Warn().Stack()}
}

// Error starts a new message with error level.
//
// You must call Msg on the returned event in order to send the event.
func Error() *Event {
	return &Event{GetLogger().Error().Stack()}
}

// Fatal starts a new message with fatal level. The os.Exit(1) function
// is called by the Msg method.
//
// You must call Msg on the returned event in order to send the event.
func Fatal() *Event {
	return &Event{GetLogger().Fatal().Stack()}
}

// Panic starts a new message with panic level. The message is also sent
// to the panic function.
//
// You must call Msg on the returned event in order to send the event.
func Panic() *Event {
	return &Event{GetLogger().Panic().Stack()}
}

// WithLevel starts a new message with level.
//
// You must call Msg on the returned event in order to send the event.
func WithLevel(level zerolog.Level) *Event {
	return &Event{GetLogger().WithLevel(level)}
}

// Log starts a new message with no level. Setting zerolog.GlobalLevel to
// zerolog.Disabled will still disable events produced by this method.
//
// You must call Msg on the returned event in order to send the event.
func Log() *Event {
	return &Event{GetLogger().Log()}
}

// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
	GetLogger().Print(v...)
}

// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
	GetLogger().Printf(format, v...)
}

// Ctx returns the Logger associated with the ctx. If no logger
// is associated, a disabled logger is returned.
func Ctx(ctx context.Context) *zerolog.Logger {
	return zerolog.Ctx(ctx)
}

func At(level *Event, id string, logtype string) *zerolog.Event {
	return level.Str("id", id).Str("type", logtype)
}
