/*
 * Copyright 2023 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 streamcall

import (
	"strings"
	"time"

	"github.com/cloudwego/kitex/client/callopt"
)

// These options are directly translated from callopt.Option(s). If you can't find the option with the
// same name in callopt.Option(s), most probably it means it's not for streaming clients.

// WithHostPort specifies the target address for an RPC call.
func WithHostPort(hostPort string) Option {
	return ConvertOptionFrom(callopt.WithHostPort(hostPort))
}

// WithURL specifies the target for a RPC call with url.
// The given url will be resolved to hostport and overwrites the result from Resolver.
func WithURL(url string) Option {
	return ConvertOptionFrom(callopt.WithURL(url))
}

// WithConnectTimeout specifies the connection timeout for a RPC call.
func WithConnectTimeout(d time.Duration) Option {
	return ConvertOptionFrom(callopt.WithConnectTimeout(d))
}

// WithTag sets the tags for service discovery for an RPC call.
func WithTag(key, val string) Option {
	return ConvertOptionFrom(callopt.WithTag(key, val))
}

// WithRecvTimeout add recv timeout for stream.Recv function.
// NOTICE: ONLY effective for ttheader streaming protocol for now.
func WithRecvTimeout(d time.Duration) Option {
	return Option{f: func(o *callopt.CallOptions, di *strings.Builder) {
		di.WriteString("WithRecvTimeout(")
		di.WriteString(d.String())
		di.WriteString(")")

		o.StreamOptions.RecvTimeout = d
	}}
}
