package httpx

import (
	
	
	
	
	
	

	
)

// File attaches a file from disk as multipart form data.
// @group Upload Options
//
// Applies to individual requests only.
// Example: upload a file
//
//	c := httpx.New()
//	_ = httpx.Post[any, string](c, "https://example.com/upload", nil, httpx.File("file", "/tmp/report.txt"))
func (,  string) OptionBuilder {
	return OptionBuilder{}.File(, )
}

func ( OptionBuilder) (,  string) OptionBuilder {
	return .add(requestOnly(func( *req.Request) {
		.SetFile(, )
	}))
}

// Files attaches multiple files from disk as multipart form data.
// @group Upload Options
//
// Applies to individual requests only.
// Example: upload multiple files
//
//	c := httpx.New()
//	_ = httpx.Post[any, string](c, "https://example.com/upload", nil, httpx.Files(map[string]string{
//		"fileA": "/tmp/a.txt",
//		"fileB": "/tmp/b.txt",
//	}))
func ( map[string]string) OptionBuilder {
	return OptionBuilder{}.Files()
}

func ( OptionBuilder) ( map[string]string) OptionBuilder {
	return .add(requestOnly(func( *req.Request) {
		.SetFiles()
	}))
}

// FileBytes attaches a file from bytes as multipart form data.
// @group Upload Options
//
// Applies to individual requests only.
// Example: upload bytes as a file
//
//	c := httpx.New()
//	_ = httpx.Post[any, string](c, "https://example.com/upload", nil, httpx.FileBytes("file", "report.txt", []byte("hello")))
func (,  string,  []byte) OptionBuilder {
	return OptionBuilder{}.FileBytes(, , )
}

func ( OptionBuilder) (,  string,  []byte) OptionBuilder {
	return .add(requestOnly(func( *req.Request) {
		.SetFileUpload(req.FileUpload{
			ParamName: ,
			FileName:  ,
			FileSize:  int64(len()),
			GetFileContent: func() (io.ReadCloser, error) {
				return io.NopCloser(bytes.NewReader()), nil
			},
		})
	}))
}

// FileReader attaches a file from a reader as multipart form data.
// @group Upload Options
//
// Applies to individual requests only.
// Example: upload from reader
//
//	c := httpx.New()
//	_ = httpx.Post[any, string](c, "https://example.com/upload", nil, httpx.FileReader("file", "report.txt", strings.NewReader("hello")))
func (,  string,  io.Reader) OptionBuilder {
	return OptionBuilder{}.FileReader(, , )
}

func ( OptionBuilder) (,  string,  io.Reader) OptionBuilder {
	return .add(requestOnly(func( *req.Request) {
		 := int64(0)
		switch v := .(type) {
		case interface{ () int64 }:
			 = .()
		case interface{ () int }:
			 = int64(.())
		case io.Seeker:
			,  := .Seek(0, io.SeekCurrent)
			if  == nil {
				,  := .Seek(0, io.SeekEnd)
				if  == nil {
					 = 
					_, _ = .Seek(, io.SeekStart)
				} else {
					_, _ = .Seek(, io.SeekStart)
				}
			}
		}

		.SetFileUpload(req.FileUpload{
			ParamName: ,
			FileName:  ,
			FileSize:  ,
			GetFileContent: func() (io.ReadCloser, error) {
				if ,  := .(io.ReadCloser);  {
					return , nil
				}
				return io.NopCloser(), nil
			},
		})
	}))
}

// UploadCallback registers a callback for upload progress.
// @group Upload Options
//
// Applies to individual requests only.
// Example: track upload progress
//
//	c := httpx.New()
//	_ = httpx.Post[any, string](c, "https://example.com/upload", nil,
//		httpx.File("file", "/tmp/report.bin"),
//		httpx.UploadCallback(func(info req.UploadInfo) {
//			percent := float64(info.UploadedSize) / float64(info.FileSize) * 100
//			fmt.Printf("\rprogress: %.1f%%", percent)
//			if info.FileSize > 0 && info.UploadedSize >= info.FileSize {
//				fmt.Print("\n")
//			}
//		}),
//	)
func ( req.UploadCallback) OptionBuilder {
	return OptionBuilder{}.UploadCallback()
}

func ( OptionBuilder) ( req.UploadCallback) OptionBuilder {
	if  == nil {
		return 
	}
	return .add(requestOnly(func( *req.Request) {
		var  sync.Mutex
		var  req.UploadInfo
		var  bool
		var  bool
		.SetUploadCallback(func( req.UploadInfo) {
			.Lock()
			 = 
			 = true
			if .FileSize > 0 && .UploadedSize >= .FileSize {
				 = true
			}
			.Unlock()
			()
		})
		.OnAfterResponse(func( *req.Client,  *req.Response) error {
			.Lock()
			 := 
			 := 
			 := 
			.Unlock()
			if ! {
				return nil
			}
			if ! {
				if .FileSize == 0 {
					.FileSize = .UploadedSize
				}
				if .FileSize > 0 {
					.UploadedSize = .FileSize
				}
				()
			}
			return nil
		})
	}))
}

// UploadCallbackWithInterval registers a callback for upload progress with a minimum interval.
// @group Upload Options
//
// Applies to individual requests only.
// Example: throttle upload progress updates
//
//	c := httpx.New()
//	_ = httpx.Post[any, string](c, "https://example.com/upload", nil,
//		httpx.File("file", "/tmp/report.bin"),
//		httpx.UploadCallbackWithInterval(func(info req.UploadInfo) {
//			percent := float64(info.UploadedSize) / float64(info.FileSize) * 100
//			fmt.Printf("\rprogress: %.1f%% (%.0f bytes)", percent, float64(info.UploadedSize))
//			if info.FileSize > 0 && info.UploadedSize >= info.FileSize {
//				fmt.Print("\n")
//			}
//		}, 200*time.Millisecond),
//	)
func ( req.UploadCallback,  time.Duration) OptionBuilder {
	return OptionBuilder{}.UploadCallbackWithInterval(, )
}

func ( OptionBuilder) ( req.UploadCallback,  time.Duration) OptionBuilder {
	if  == nil {
		return 
	}
	return .add(requestOnly(func( *req.Request) {
		var  sync.Mutex
		var  req.UploadInfo
		var  bool
		var  bool
		.SetUploadCallbackWithInterval(func( req.UploadInfo) {
			.Lock()
			 = 
			 = true
			if .FileSize > 0 && .UploadedSize >= .FileSize {
				 = true
			}
			.Unlock()
			()
		}, )
		.OnAfterResponse(func( *req.Client,  *req.Response) error {
			.Lock()
			 := 
			 := 
			 := 
			.Unlock()
			if ! {
				return nil
			}
			if ! {
				if .FileSize == 0 {
					.FileSize = .UploadedSize
				}
				if .FileSize > 0 {
					.UploadedSize = .FileSize
				}
				()
			}
			return nil
		})
	}))
}

// UploadProgress enables a default progress spinner and bar for uploads.
// @group Upload Options
// Applies to individual requests only.
//
// Example: upload with automatic progress
//
//	c := httpx.New()
//	_ = httpx.Post[any, string](c, "https://example.com/upload", nil,
//		httpx.File("file", "/tmp/report.bin"),
//		httpx.UploadProgress(),
//	)
func () OptionBuilder {
	return OptionBuilder{}.UploadProgress()
}

func ( OptionBuilder) () OptionBuilder {
	return .add(requestOnly(func( *req.Request) {
		var  sync.Mutex
		var  string
		var  int64
		 := []string{"|", "/", "-", "\\"}
		 := 0
		 := 20

		.SetUploadCallback(func( req.UploadInfo) {
			.Lock()
			defer .Unlock()

			if .FileSize > 0 {
				 = .FileSize
			}
			 = ( + 1) % len()

			if  > 0 {
				 := float64(.UploadedSize) / float64() * 100
				 := int( / 100 * float64())
				 := strings.Repeat("=", ) + strings.Repeat("-", -)
				 = fmt.Sprintf(
					"\r%s upload [%s] %.1f%% (%s/%s)",
					[],
					,
					,
					formatBytes(.UploadedSize),
					formatBytes(),
				)
			} else {
				 = fmt.Sprintf("\r%s upload %s", [], formatBytes(.UploadedSize))
			}
			fmt.Print()
		})

		.OnAfterResponse(func( *req.Client,  *req.Response) error {
			.Lock()
			defer .Unlock()
			if  == "" {
				return nil
			}
			if  > 0 {
				fmt.Printf(
					"\r%s upload [%s] 100.0%% (%s/%s)\n",
					[],
					strings.Repeat("=", ),
					formatBytes(),
					formatBytes(),
				)
			} else {
				fmt.Print("\n")
			}
			return nil
		})
	}))
}

func formatBytes( int64) string {
	if  < 1024 {
		return fmt.Sprintf("%d B", )
	}
	 := []string{"KiB", "MiB", "GiB", "TiB", "PiB"}
	 := float64()
	 := "B"
	for ,  := range  {
		 /= 1024
		 = 
		if  < 1024 {
			break
		}
	}
	return fmt.Sprintf("%.1f %s", , )
}