package req

import (
	
	
	
	
	
	
)

const (
	traceFmt = `TotalTime         : %v
DNSLookupTime     : %v
TCPConnectTime    : %v
TLSHandshakeTime  : %v
FirstResponseTime : %v
ResponseTime      : %v
IsConnReused:     : false
RemoteAddr        : %v
LocalAddr         : %v`
	traceReusedFmt = `TotalTime         : %v
FirstResponseTime : %v
ResponseTime      : %v
IsConnReused:     : true
RemoteAddr        : %v
LocalAddr         : %v`
)

// Blame return the human-readable reason of why request is slowing.
func ( TraceInfo) () string {
	if .RemoteAddr == nil {
		return "trace is not enabled"
	}
	var  string
	var  time.Duration
	 := map[string]time.Duration{
		"on dns lookup":    .DNSLookupTime,
		"on tcp connect":   .TCPConnectTime,
		"on tls handshake": .TLSHandshakeTime,
		"from connection ready to server respond first byte":   .FirstResponseTime,
		"from server respond first byte to request completion": .ResponseTime,
	}
	for ,  := range  {
		if  >  {
			 = 
			 = 
		}
	}
	if  == "" {
		return "nothing to blame"
	}
	return fmt.Sprintf("the request total time is %v, and costs %v %s", .TotalTime, , )
}

// String return the details of trace information.
func ( TraceInfo) () string {
	if .RemoteAddr == nil {
		return "trace is not enabled"
	}
	if .IsConnReused {
		return fmt.Sprintf(traceReusedFmt, .TotalTime, .FirstResponseTime, .ResponseTime, .RemoteAddr, .LocalAddr)
	}
	return fmt.Sprintf(traceFmt, .TotalTime, .DNSLookupTime, .TCPConnectTime, .TLSHandshakeTime, .FirstResponseTime, .ResponseTime, .RemoteAddr, .LocalAddr)
}

// TraceInfo represents the trace information.
type TraceInfo struct {
	// DNSLookupTime is a duration that transport took to perform
	// DNS lookup.
	DNSLookupTime time.Duration

	// ConnectTime is a duration that took to obtain a successful connection.
	ConnectTime time.Duration

	// TCPConnectTime is a duration that took to obtain the TCP connection.
	TCPConnectTime time.Duration

	// TLSHandshakeTime is a duration that TLS handshake took place.
	TLSHandshakeTime time.Duration

	// FirstResponseTime is a duration that server took to respond first byte since
	// connection ready (after tls handshake if it's tls and not a reused connection).
	FirstResponseTime time.Duration

	// ResponseTime is a duration since first response byte from server to
	// request completion.
	ResponseTime time.Duration

	// TotalTime is a duration that total request took end-to-end.
	TotalTime time.Duration

	// IsConnReused is whether this connection has been previously
	// used for another HTTP request.
	IsConnReused bool

	// IsConnWasIdle is whether this connection was obtained from an
	// idle pool.
	IsConnWasIdle bool

	// ConnIdleTime is a duration how long the connection was previously
	// idle, if IsConnWasIdle is true.
	ConnIdleTime time.Duration

	// RemoteAddr returns the remote network address.
	RemoteAddr net.Addr

	// LocalAddr returns the local network address.
	LocalAddr net.Addr
}

type clientTrace struct {
	getConn              time.Time
	dnsStart             time.Time
	dnsDone              time.Time
	connectDone          time.Time
	tlsHandshakeStart    time.Time
	tlsHandshakeDone     time.Time
	gotConn              time.Time
	gotFirstResponseByte time.Time
	endTime              time.Time
	gotConnInfo          httptrace.GotConnInfo
}

func ( *clientTrace) ( context.Context) context.Context {
	return httptrace.WithClientTrace(
		,
		&httptrace.ClientTrace{
			DNSStart: func( httptrace.DNSStartInfo) {
				.dnsStart = time.Now()
			},
			DNSDone: func( httptrace.DNSDoneInfo) {
				.dnsDone = time.Now()
			},
			ConnectStart: func(,  string) {
				if .dnsDone.IsZero() {
					.dnsDone = time.Now()
				}
				if .dnsStart.IsZero() {
					.dnsStart = .dnsDone
				}
			},
			ConnectDone: func(,  string,  error) {
				.connectDone = time.Now()
			},
			GetConn: func( string) {
				.getConn = time.Now()
			},
			GotConn: func( httptrace.GotConnInfo) {
				.gotConn = time.Now()
				.gotConnInfo = 
			},
			GotFirstResponseByte: func() {
				.gotFirstResponseByte = time.Now()
			},
			TLSHandshakeStart: func() {
				.tlsHandshakeStart = time.Now()
			},
			TLSHandshakeDone: func( tls.ConnectionState,  error) {
				.tlsHandshakeDone = time.Now()
			},
		},
	)
}