/**
 * HTTP请求统一工具类
 * 基于BaseRequest类提供标准化的请求配置和执行方法
 */

import { CONFIG_TEMPLATES, LOADING_TEXTS, createRequestConfig } from '../config/constants.js'

// ==================== BaseRequest工具类 ====================

/**
 * 统一的HTTP请求工具类
 * 提供标准化的请求方法，基于uni.$u.http实现
 */
class BaseRequest {
	/**
	 * 执行GET请求
	 * @param {string} url 请求URL
	 * @param {Object} params 查询参数
	 * @param {string} template 配置模板
	 * @param {Object} config 自定义配置
	 * @returns {Promise} 请求Promise
	 */
	static get(url, params = {}, template = 'AUTHENTICATED_QUERY', config = {}) {
		const requestConfig = createRequestConfig(template, config)

		return uni.$u.http.get(url, {
			...(Object.keys(params).length > 0 && { params }),
			...requestConfig
		})
	}

	/**
	 * 执行POST请求
	 * @param {string} url 请求URL
	 * @param {Object} data 请求数据
	 * @param {string} template 配置模板
	 * @param {string} loadingText loading文本
	 * @param {Object} config 自定义配置
	 * @returns {Promise} 请求Promise
	 */
	static post(url, data = {}, template = 'AUTHENTICATED_UPDATE', loadingText = null, config = {}) {
		const requestConfig = createRequestConfig(template, config, loadingText)

		return uni.$u.http.post(url, data, requestConfig)
	}

	/**
	 * 执行PUT请求
	 * @param {string} url 请求URL
	 * @param {Object} data 请求数据
	 * @param {string} template 配置模板
	 * @param {string} loadingText loading文本
	 * @param {Object} config 自定义配置
	 * @returns {Promise} 请求Promise
	 */
	static put(url, data = {}, template = 'AUTHENTICATED_UPDATE', loadingText = null, config = {}) {
		const requestConfig = createRequestConfig(template, config, loadingText)

		return uni.$u.http.put(url, data, requestConfig)
	}

	/**
	 * 执行DELETE请求
	 * @param {string} url 请求URL
	 * @param {Object} data 请求数据
	 * @param {string} template 配置模板
	 * @param {string} loadingText loading文本
	 * @param {Object} config 自定义配置
	 * @returns {Promise} 请求Promise
	 */
	static delete(url, data = {}, template = 'AUTHENTICATED_DELETE', loadingText = null, config = {}) {
		const requestConfig = createRequestConfig(template, config, loadingText)

		return uni.$u.http.delete(url, data, requestConfig)
	}

	/**
	 * 执行上传请求
	 * @param {string} url 上传URL
	 * @param {Object} uploadData 上传数据
	 * @param {string} loadingText loading文本
	 * @param {Object} config 自定义配置
	 * @returns {Promise} 请求Promise
	 */
	static upload(url, uploadData, loadingText, config = {}) {
		const requestConfig = createRequestConfig('AUTHENTICATED_UPLOAD', config, loadingText)

		return uni.$u.http.upload(url, {
			filePath: uploadData.filePath,
			name: uploadData.name || 'file',
			formData: uploadData.formData || {},
			...requestConfig
		})
	}

	/**
	 * 执行下载请求
	 * @param {string} url 下载URL
	 * @param {string} loadingText loading文本
	 * @param {Object} config 自定义配置
	 * @returns {Promise} 请求Promise
	 */
	static download(url, loadingText, config = {}) {
		const requestConfig = createRequestConfig('PUBLIC_DOWNLOAD', config, loadingText)

		return uni.$u.http.download(url, {
			...(config.savePath && { savePath: config.savePath }),
			...requestConfig
		})
	}

	/**
	 * 批量上传多个文件
	 * @param {Array} fileList 文件列表数组
	 * @param {string} url 上传URL
	 * @param {Object} config 自定义配置
	 * @returns {Promise} 请求Promise
	 */
	static async batchUpload(fileList, url, config = {}) {
		const { maxConcurrent = 3, showProgress = true } = config

		// 显示整体进度loading
		if (showProgress) {
			uni.showLoading({
				title: config.custom?.loadingText || LOADING_TEXTS.UPLOAD_IMAGES
			})
		}

		try {
			// 分批并发上传
			const results = []
			for (let i = 0; i < fileList.length; i += maxConcurrent) {
				const batch = fileList.slice(i, i + maxConcurrent)
				const batchPromises = batch.map(fileData => {
					return BaseRequest.upload(url, fileData, null, {
						...config,
						custom: {
							loading: false, // 批量上传时不显示单个loading
							...config.custom
						}
					})
				})

				const batchResults = await Promise.all(batchPromises)
				results.push(...batchResults)
			}

			return results
		} finally {
			if (showProgress) {
				uni.hideLoading()
			}
		}
	}
}

// ==================== 导出 ====================

// 默认导出BaseRequest类
export default BaseRequest

// 向后兼容的命名导出
export const executeGetRequest = BaseRequest.get
export const executePostRequest = BaseRequest.post
export const executePutRequest = BaseRequest.put
export const executeDeleteRequest = BaseRequest.delete
export const executeUploadRequest = BaseRequest.upload
export const executeDownloadRequest = BaseRequest.download
export const executeBatchUploadRequest = BaseRequest.batchUpload

// 导出配置相关（从constants.js重新导出）
export { CONFIG_TEMPLATES, LOADING_TEXTS, createRequestConfig }
