// Copyright 2021 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package thriftgo

const File = `
{{define "file"}}
// Code generated by Kitex {{Version}}. DO NOT EDIT.

package {{.PkgName}}

` + ImportInsertPoint + `

{{InsertionPoint "KitexUnusedProtection"}}
// unused protection
var (
	_ = fmt.Formatter(nil) {{- UseStdLibrary "fmt"}}
	_ = (*bytes.Buffer)(nil) {{- UseStdLibrary "bytes"}}
	_ = (*strings.Builder)(nil) {{- UseStdLibrary "strings"}}
	_ = reflect.Type(nil) {{- UseStdLibrary "reflect"}}
	_ = thrift.STOP {{- UseLib "github.com/cloudwego/gopkg/protocol/thrift" ""}}
	{{- if and (and GenerateArgsResultTypes Features.KeepUnknownFields) (ne (len .Scope.Services) 0)}}
	{{- UseStdLibrary "unknown"}}
	{{- end}}
)

{{template "body" .}}

{{end}}{{/* define "file" */}}
`

const Imports = `
{{define "imports"}}
import (
	{{PrintImports .Imports}}
)

{{- if gt (len .Includes) 0}}
var (
	{{- range .Includes }}
	_ = {{.PackageName}}.KitexUnusedProtection
	{{- end}}
)
{{- end}}
{{end}}{{/* define "imports" */}}
`

const Body = `
{{define "body"}}

{{- range .Scope.StructLikes}}
{{template "StructLikeCodec" .}}
{{- end}}

{{- range .Scope.Services}}
{{template "Processor" .}}
{{- end}}

{{template "ArgsAndResult" .}}

{{template "ExtraTemplates" .}}
{{- end}}{{/* define "body" */}}
`

const PatchArgsAndResult = `
{{define "ArgsAndResult"}}
{{range $svc := .Scope.Services}}
{{range .Functions}}

{{$argType := .ArgType}}
{{$resType := .ResType}}


{{- if GenerateArgsResultTypes}}
{{- $withFieldMask := (SetWithFieldMask false) }}
{{template "StructLike" $argType}}
{{- $_ := (SetWithFieldMask $withFieldMask) }}
{{- end}}{{/* if GenerateArgsResultTypes */}}
func (p *{{$argType.GoName}}) GetFirstArgument() interface{} {
	return {{if .Arguments}}p.{{(index $argType.Fields 0).GoName}}{{else}}nil{{end}}
}

{{if not .Oneway}}
{{- if GenerateArgsResultTypes}}
{{- $withFieldMask := (SetWithFieldMask false) }}
{{template "StructLike" $resType}}
{{- $_ := (SetWithFieldMask $withFieldMask) }}
{{- end}}{{/* if GenerateArgsResultTypes */}}
func (p *{{$resType.GoName}}) GetResult() interface{} {
	return {{if .Void}}nil{{else}}p.Success{{end}}
}

{{- end}}{{/* if not .Oneway */}}
{{- end}}{{/* range Functions */}}
{{- end}}{{/* range .Scope.Service */}}
{{- end}}{{/* define "FirstResult" */}}
`

var basicTemplates = []string{
	PatchArgsAndResult,
	File,
	Imports,
	Body,
	RegisterHessian,
}
