// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package fxevent

import (
	
	
	
)

// ConsoleLogger is an Fx event logger that attempts to write human-readable
// messages to the console.
//
// Use this during development.
type ConsoleLogger struct {
	W io.Writer
}

var _ Logger = (*ConsoleLogger)(nil)

func ( *ConsoleLogger) ( string,  ...interface{}) {
	fmt.Fprintf(.W, "[Fx] "++"\n", ...)
}

// LogEvent logs the given event to the provided Zap logger.
func ( *ConsoleLogger) ( Event) {
	switch e := .(type) {
	case *OnStartExecuting:
		.logf("HOOK OnStart\t\t%s executing (caller: %s)", .FunctionName, .CallerName)
	case *OnStartExecuted:
		if .Err != nil {
			.logf("HOOK OnStart\t\t%s called by %s failed in %s: %+v", .FunctionName, .CallerName, .Runtime, .Err)
		} else {
			.logf("HOOK OnStart\t\t%s called by %s ran successfully in %s", .FunctionName, .CallerName, .Runtime)
		}
	case *OnStopExecuting:
		.logf("HOOK OnStop\t\t%s executing (caller: %s)", .FunctionName, .CallerName)
	case *OnStopExecuted:
		if .Err != nil {
			.logf("HOOK OnStop\t\t%s called by %s failed in %s: %+v", .FunctionName, .CallerName, .Runtime, .Err)
		} else {
			.logf("HOOK OnStop\t\t%s called by %s ran successfully in %s", .FunctionName, .CallerName, .Runtime)
		}
	case *Supplied:
		if .Err != nil {
			.logf("ERROR\tFailed to supply %v: %+v", .TypeName, .Err)
		} else if .ModuleName != "" {
			.logf("SUPPLY\t%v from module %q", .TypeName, .ModuleName)
		} else {
			.logf("SUPPLY\t%v", .TypeName)
		}
	case *Provided:
		var  string
		if .Private {
			 = " (PRIVATE)"
		}
		for ,  := range .OutputTypeNames {
			if .ModuleName != "" {
				.logf("PROVIDE%v\t%v <= %v from module %q", , , .ConstructorName, .ModuleName)
			} else {
				.logf("PROVIDE%v\t%v <= %v", , , .ConstructorName)
			}
		}
		if .Err != nil {
			.logf("Error after options were applied: %+v", .Err)
		}
	case *Replaced:
		for ,  := range .OutputTypeNames {
			if .ModuleName != "" {
				.logf("REPLACE\t%v from module %q", , .ModuleName)
			} else {
				.logf("REPLACE\t%v", )
			}
		}
		if .Err != nil {
			.logf("ERROR\tFailed to replace: %+v", .Err)
		}
	case *Decorated:
		for ,  := range .OutputTypeNames {
			if .ModuleName != "" {
				.logf("DECORATE\t%v <= %v from module %q", , .DecoratorName, .ModuleName)
			} else {
				.logf("DECORATE\t%v <= %v", , .DecoratorName)
			}
		}
		if .Err != nil {
			.logf("Error after options were applied: %+v", .Err)
		}
	case *BeforeRun:
		var  string
		if .ModuleName != "" {
			 = fmt.Sprintf(" from module %q", .ModuleName)
		}
		.logf("BEFORE RUN\t%s: %s%s", .Kind, .Name, )
	case *Run:
		var  string
		if .ModuleName != "" {
			 = fmt.Sprintf(" from module %q", .ModuleName)
		}
		.logf("RUN\t%v: %v in %s%v", .Kind, .Name, .Runtime, )
		if .Err != nil {
			.logf("Error returned: %+v", .Err)
		}

	case *Invoking:
		if .ModuleName != "" {
			.logf("INVOKE\t\t%s from module %q", .FunctionName, .ModuleName)
		} else {
			.logf("INVOKE\t\t%s", .FunctionName)
		}
	case *Invoked:
		if .Err != nil {
			.logf("ERROR\t\tfx.Invoke(%v) called from:\n%+vFailed: %+v", .FunctionName, .Trace, .Err)
		}
	case *Stopping:
		.logf("%v", strings.ToUpper(.Signal.String()))
	case *Stopped:
		if .Err != nil {
			.logf("ERROR\t\tFailed to stop cleanly: %+v", .Err)
		}
	case *RollingBack:
		.logf("ERROR\t\tStart failed, rolling back: %+v", .StartErr)
	case *RolledBack:
		if .Err != nil {
			.logf("ERROR\t\tCouldn't roll back cleanly: %+v", .Err)
		}
	case *Started:
		if .Err != nil {
			.logf("ERROR\t\tFailed to start: %+v", .Err)
		} else {
			.logf("RUNNING")
		}
	case *LoggerInitialized:
		if .Err != nil {
			.logf("ERROR\t\tFailed to initialize custom logger: %+v", .Err)
		} else {
			.logf("LOGGER\tInitialized custom logger from %v", .ConstructorName)
		}
	}
}