/*
 * 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 callopt

import (
	"context"
	"fmt"
	"strings"
	"testing"
	"time"

	"github.com/cloudwego/kitex/internal/client"
	"github.com/cloudwego/kitex/internal/test"
	"github.com/cloudwego/kitex/pkg/fallback"
	"github.com/cloudwego/kitex/pkg/http"
	"github.com/cloudwego/kitex/pkg/retry"
	"github.com/cloudwego/kitex/pkg/rpcinfo"
	"github.com/cloudwego/kitex/pkg/rpcinfo/remoteinfo"
)

// Mock value
var (
	option     Option
	applyRes   string
	rpcConfig  = rpcinfo.AsMutableRPCConfig(rpcinfo.NewRPCConfig())
	remoteInfo = remoteinfo.NewRemoteInfo(&rpcinfo.EndpointBasicInfo{
		ServiceName: "service",
		Method:      "method0",
	}, "method1")
)

const (
	mockHostPort          = "127.0.0.1:8888"
	mockURL               = "www.cloudwego.com"
	mockHTTPHost          = "www.cloudwego.com"
	mockRPCTimeout        = 1000 * time.Millisecond
	mockRPCTimeoutStr     = "1000ms"
	mockConnectTimeout    = 500 * time.Millisecond
	mockConnectTimeoutStr = "500ms"
	mockKey               = "mock_key"
	mockVal               = "mock_val"
)

// TestApply apply different option
func TestApply(t *testing.T) {
	// WithHostPort
	option = WithHostPort(mockHostPort)
	applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, applyRes == fmt.Sprintf("[WithHostPort(%s),]", mockHostPort))

	// WithURL
	option = WithURL(mockURL)
	applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, applyRes == fmt.Sprintf("[WithURL(%s),]", mockURL))

	// WithHTTPHost
	option = WithHTTPHost(mockHTTPHost)
	applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	httpHost, exist := remoteInfo.Tag(rpcinfo.HTTPHost)
	test.Assert(t, httpHost == mockHTTPHost, applyRes)
	test.Assert(t, exist)

	// WithRPCTimeout
	option = WithRPCTimeout(mockRPCTimeout)
	applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, applyRes == "[WithRPCTimeout()]", applyRes)
	test.Assert(t, rpcConfig.ImmutableView().RPCTimeout() == mockRPCTimeout, rpcConfig.ImmutableView().RPCTimeout())
	test.Assert(t, rpcConfig.ImmutableView().ReadWriteTimeout() == mockRPCTimeout, rpcConfig.ImmutableView().ReadWriteTimeout())

	// WithConnectTimeout
	option = WithConnectTimeout(mockConnectTimeout)
	applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, applyRes == "[WithConnectTimeout()]", applyRes)
	test.Assert(t, rpcConfig.ImmutableView().ConnectTimeout() == mockConnectTimeout, rpcConfig.ImmutableView().ConnectTimeout())

	// WithTag
	option = WithTag(mockKey, mockVal)
	applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	v, exist := remoteInfo.Tag(mockKey)
	test.Assert(t, exist)
	test.Assert(t, v == mockVal, v)

	// WithRetryPolicy
	option = WithRetryPolicy(retry.BuildFailurePolicy(retry.NewFailurePolicy()))
	_, co := Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, co.RetryPolicy.Enable)
	test.Assert(t, co.RetryPolicy.FailurePolicy != nil)

	// WithRetryPolicy
	option = WithRetryPolicy(retry.BuildMixedPolicy(retry.NewMixedPolicy(10)))
	_, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, co.RetryPolicy.Enable)
	test.Assert(t, co.RetryPolicy.MixedPolicy != nil)

	// WithRetryPolicy
	option = WithRetryPolicy(retry.BuildBackupRequest(retry.NewBackupPolicy(10)))
	_, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, co.RetryPolicy.Enable)
	test.Assert(t, co.RetryPolicy.BackupPolicy != nil)

	// WithRetryPolicy pass empty struct
	option = WithRetryPolicy(retry.Policy{})
	_, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, !co.RetryPolicy.Enable)

	// WithFallback
	option = WithFallback(fallback.ErrorFallback(fallback.UnwrapHelper(func(ctx context.Context, req, resp interface{}, err error) (fbResp interface{}, fbErr error) {
		return
	})).EnableReportAsFallback())
	_, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, co.Fallback != nil)

	// WithFallback pass nil
	option = WithFallback(nil)
	_, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
	test.Assert(t, co.Fallback == nil)
}

func BenchmarkStringsBuilder(b *testing.B) {
	var cos []Option
	cos = append(cos, WithRPCTimeout(time.Second))
	cos = append(cos, WithTag("cluster", "mock"))
	cos = append(cos, WithTag("idc", "mock"))
	b.ReportAllocs()
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			co := callOptionsPool.Get().(*CallOptions)
			co.configs = rpcinfo.NewRPCConfig().(rpcinfo.MutableRPCConfig)
			co.svr = remoteinfo.NewRemoteInfo(&rpcinfo.EndpointBasicInfo{}, "method")
			var buf strings.Builder
			buf.Grow(64)

			buf.WriteByte('[')
			for i := range cos {
				if i > 0 {
					buf.WriteByte(',')
				}
				cos[i].f(co, &buf)
			}
			buf.WriteByte(']')
			_ = buf.String()
		}
	})
}
