package httpc

import (
	"net/http"
	"time"

	"golang.org/x/time/rate"
)

type (
	Config struct {
		timeout       time.Duration
		client        *http.Client
		transport     *http.Transport
		redirectFn    func(req *http.Request, via []*http.Request) error
		noCheckStatus bool
		qps           int
		qpsLimiter    *rate.Limiter
	}

	ConfigOpt func(c *Config)
)

func WithTimout(v time.Duration) ConfigOpt {
	return func(c *Config) {
		c.timeout = v
	}
}

func WithClient(v *http.Client) ConfigOpt {
	return func(c *Config) {
		c.client = v
	}
}

func WithTransport(v *http.Transport) ConfigOpt {
	return func(c *Config) {
		c.transport = v
	}
}

func WithRedirectFn(v func(req *http.Request, via []*http.Request) error) ConfigOpt {
	return func(c *Config) {
		c.redirectFn = v
	}
}

func WithNoRedirect() ConfigOpt {
	return func(c *Config) {
		c.redirectFn = func(req *http.Request, via []*http.Request) error {
			return http.ErrUseLastResponse
		}
	}
}

func WithNoCheckStatus(v bool) ConfigOpt {
	return func(c *Config) {
		c.noCheckStatus = v
	}
}

func WithQps(v int) ConfigOpt {
	return func(c *Config) {
		c.qps = v
	}
}

func WithQpsLimiter(v *rate.Limiter) ConfigOpt {
	return func(c *Config) {
		c.qpsLimiter = v
	}
}
