@file:Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")

package com.img.rabbit.provider.api

import com.img.rabbit.BuildConfig
import com.img.rabbit.config.Constants
import com.img.rabbit.provider.utils.HeaderInterceptor
import com.img.rabbit.provider.utils.RequestInterceptor
import com.img.rabbit.provider.utils.ResponseInterceptor
import com.img.rabbit.provider.utils.getUnsafeOkHttpClient
import com.img.rabbit.viewmodel.`interface`.ServiceVo
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object ApiManager {
    private lateinit var retrofit: Retrofit
    private lateinit var unsafeRetrofit: Retrofit
    lateinit var serviceVo: ServiceVo

    init {
        initialize()
    }

    // 初始化方法，抽离出来以便重新初始化
    private fun initialize() {
        // 获取基础URL并确保其包含http/https协议
        val baseUrl = if (BuildConfig.DEBUG) {
            Constants.DEBUG_BASE_URL
        } else {
            Constants.RELEASE_BASE_URL
        }

        if (!::retrofit.isInitialized) {
            val client = OkHttpClient().newBuilder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(RequestInterceptor())
                .addInterceptor(HeaderInterceptor())
                .build()

            retrofit = Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }

        if (!::unsafeRetrofit.isInitialized) {
            val client = getUnsafeOkHttpClient().newBuilder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(HeaderInterceptor())
                .addInterceptor(RequestInterceptor())
                .addInterceptor(ResponseInterceptor())
                .build()

            unsafeRetrofit = Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }

        if (!::serviceVo.isInitialized) {
            serviceVo = unsafeRetrofit.create(ServiceVo::class.java)
        }
    }
    
    // 添加重新初始化方法，用于在baseUrl改变时调用
    fun reinitialize() {
        // 重置所有实例，使其在下一次访问时重新初始化
        if (::retrofit.isInitialized) {
            synchronized(this) {
                // 标记为未初始化
                @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
                (this as Object).getClass().getDeclaredField("retrofit").apply {
                    isAccessible = true
                    set(this@ApiManager, null)
                }
            }
        }
        
        if (::unsafeRetrofit.isInitialized) {
            synchronized(this) {
                @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
                (this as Object).getClass().getDeclaredField("unsafeRetrofit").apply {
                    isAccessible = true
                    set(this@ApiManager, null)
                }
            }
        }
        
        if (::serviceVo.isInitialized) {
            synchronized(this) {
                @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
                (this as Object).getClass().getDeclaredField("serviceVo").apply {
                    isAccessible = true
                    set(this@ApiManager, null)
                }
            }
        }
        
        // 重新初始化
        initialize()
    }
}