package com.narutohuo.xindazhou.common.networkrx

import io.reactivex.Observable
import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * RxJava API响应转换器
 * 
 * 用于在Observable链中统一处理API响应
 * 
 * 功能：
 * - 线程调度
 * - 响应转换
 * - 错误处理
 * - 统一的响应格式
 * 
 * 使用方式：
 * ```kotlin
 * apiService.login(request)
 *     .compose(RxApiResponseTransformer.transform())
 *     .subscribe({
 *         // 处理成功
 *     }, {
 *         // 处理错误
 *     })
 * ```
 */
object RxApiResponseTransformer {
    
    /**
     * 转换Observable
     * 
     * @return ObservableTransformer
     */
    fun <T> transform(): ObservableTransformer<T, RxApiResponse<T>> {
        return ObservableTransformer {
            it.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map<RxApiResponse<T>> { data ->
                    RxApiResponse.Success(data)
                }
                .onErrorReturn { throwable ->
                    RxApiResponseConverter.convertError<T>(throwable)
                }
        }
    }
    
    /**
     * 转换通用响应
     * 
     * @return ObservableTransformer
     */
    fun <T> transformCommonResponse(): ObservableTransformer<RxCommonResponse<T>, RxApiResponse<T>> {
        return ObservableTransformer {upstream ->
            upstream
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map {
                    RxApiResponseConverter.convert(it)
                }
                .onErrorReturn { throwable ->
                    RxApiResponseConverter.convertError(throwable)
                }
        }
    }
    
    /**
     * 仅进行线程调度
     * 
     * @return ObservableTransformer
     */
    fun <T> schedulers(): ObservableTransformer<T, T> {
        return ObservableTransformer {upstream ->
            upstream
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
        }
    }
    
    /**
     * 带重试的转换器
     * 
     * @param maxRetries 最大重试次数
     * @param retryDelayMillis 重试延迟时间（毫秒）
     * @return ObservableTransformer
     */
    fun <T> transformWithRetry(
        maxRetries: Int = 3,
        retryDelayMillis: Long = 1000
    ): ObservableTransformer<T, RxApiResponse<T>> {
        return ObservableTransformer {
            it.subscribeOn(Schedulers.io())
                .retryWhen {errors ->
                    errors.zipWith(Observable.range(1, maxRetries + 1)) { error, attempt ->
                        if (attempt > maxRetries) {
                            throw error
                        }
                        attempt
                    }
                    .flatMap { Observable.timer(retryDelayMillis, java.util.concurrent.TimeUnit.MILLISECONDS) }
                }
                .observeOn(AndroidSchedulers.mainThread())
                .map<RxApiResponse<T>> { data ->
                    RxApiResponse.Success(data)
                }
                .onErrorReturn { throwable ->
                    RxApiResponseConverter.convertError<T>(throwable)
                }
        }
    }
}