// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

// Code generated by "make app-layers"
// DO NOT EDIT

package opentracing

import (
	"github.com/opentracing/opentracing-go/ext"
	spanlog "github.com/opentracing/opentracing-go/log"
)

type {{.Name}} struct {
	app app.AppIface

	srv *app.Server

	log              *mlog.Logger
	notificationsLog *mlog.Logger

	accountMigration einterfaces.AccountMigrationInterface
	cluster          einterfaces.ClusterInterface
	compliance       einterfaces.ComplianceInterface
	dataRetention    einterfaces.DataRetentionInterface
	searchEngine     *searchengine.Broker
	ldap             einterfaces.LdapInterface
	messageExport    einterfaces.MessageExportInterface
	metrics          einterfaces.MetricsInterface
	notification     einterfaces.NotificationInterface
	saml             einterfaces.SamlInterface

	httpService httpservice.HTTPService
	imageProxy  *imageproxy.ImageProxy
	timezones   *timezones.Timezones

	ctx context.Context
}

{{range $index, $element := .Methods}}
func (a *{{$.Name}}) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
	origCtx := a.ctx
	span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.{{$index}}")

	a.ctx = newCtx
	a.app.Srv().Store().SetContext(newCtx)
	defer func() {
		a.app.Srv().Store().SetContext(origCtx)
		a.ctx = origCtx
	}()
	{{range $paramIdx, $param := $element.Params}}
		{{ shouldTrace $element.ParamsToTrace $param.Name }}
	{{end}}
	defer span.Finish()
	{{- if $element.Results | len | eq 0}}
		a.app.{{$index}}({{$element.Params | joinParams}})
	{{else}}
		{{$element.Results | genResultsVars}} := a.app.{{$index}}({{$element.Params | joinParams}})
		{{if $element.Results | errorPresent}}
			if {{$element.Results | errorVar}} != nil {
				span.LogFields(spanlog.Error({{$element.Results | errorVar}}))
				ext.Error.Set(span, true)
			}
		{{end}}
		return {{$element.Results | genResultsVars -}}
	{{end}}}
{{end}}

func NewOpenTracingAppLayer(childApp app.AppIface, ctx context.Context) *{{.Name}} {
	newApp := {{.Name}}{
		app: childApp,
		ctx: ctx,
	}

	newApp.srv = childApp.Srv()
	newApp.log = childApp.Log()
	newApp.notificationsLog = childApp.NotificationsLog()
	newApp.accountMigration = childApp.AccountMigration()
	newApp.cluster = childApp.Cluster()
	newApp.compliance = childApp.Compliance()
	newApp.dataRetention = childApp.DataRetention()
	newApp.searchEngine = childApp.SearchEngine()
	newApp.ldap = childApp.Ldap()
	newApp.messageExport = childApp.MessageExport()
	newApp.metrics = childApp.Metrics()
	newApp.notification = childApp.Notification()
	newApp.saml = childApp.Saml()
	newApp.httpService = childApp.HTTPService()
	newApp.imageProxy = childApp.ImageProxy()
	newApp.timezones = childApp.Timezones()

	return &newApp
}


func (a *{{.Name}}) Srv() *app.Server {
	return a.srv
}
func (a *{{.Name}}) Log() *mlog.Logger {
	return a.log
}
func (a *{{.Name}}) NotificationsLog() *mlog.Logger {
	return a.notificationsLog
}
func (a *{{.Name}}) AccountMigration() einterfaces.AccountMigrationInterface {
	return a.accountMigration
}
func (a *{{.Name}}) Cluster() einterfaces.ClusterInterface {
	return a.cluster
}
func (a *{{.Name}}) Compliance() einterfaces.ComplianceInterface {
	return a.compliance
}
func (a *{{.Name}}) DataRetention() einterfaces.DataRetentionInterface {
	return a.dataRetention
}
func (a *{{.Name}}) Ldap() einterfaces.LdapInterface {
	return a.ldap
}
func (a *{{.Name}}) MessageExport() einterfaces.MessageExportInterface {
	return a.messageExport
}
func (a *{{.Name}}) Metrics() einterfaces.MetricsInterface {
	return a.metrics
}
func (a *{{.Name}}) Notification() einterfaces.NotificationInterface {
	return a.notification
}
func (a *{{.Name}}) Saml() einterfaces.SamlInterface {
	return a.saml
}
func (a *{{.Name}}) HTTPService() httpservice.HTTPService {
	return a.httpService
}
func (a *{{.Name}}) ImageProxy() *imageproxy.ImageProxy {
	return a.imageProxy
}
func (a *{{.Name}}) Timezones() *timezones.Timezones {
	return a.timezones
}
func (a *{{.Name}}) SetServer(srv *app.Server) {
       a.srv = srv
}