package com.miya.fastcashier.net

import android.text.TextUtils
import com.fastcashier.lib_common.function.account.SelfCashierAccountInfo
import com.fastcashier.lib_common.function.account.SelfCashierTerminalConfig
import com.fastcashier.lib_common.net.*
import com.miya.fastcashier.net.param.LoginParams
import okhttp3.MultipartBody


class ApiRequest {

    companion object {

//        private var apiService: ApiService? = null
//        private var sInstance: ApiRequest? = null
//
//        @Synchronized
//        fun getInstance(): ApiRequest {
//            if (null == sInstance) {
//                sInstance = ApiRequest()
//            }
//            return sInstance!!
//        }
//
//        private fun getApiService(): ApiService {
//            if (null == apiService) {
//                val httpLoggingInterceptor =
//                    MiyaHttpLoggingInterceptor {
//                        Log.e("####", it)
//                        if (!LogFileUtils.isProhibitWrite) {
//                            LogFileUtils.writeLog(BaseApplication.getApplication(),it)
//                        }
//                    }.apply { level = MiyaHttpLoggingInterceptor.Level.BODY }
//
//
//                val socketFactory = arrayOfNulls<SSLSocketFactory>(1)
//                val trustManager = arrayOfNulls<X509TrustManager>(1)
//                SSLCertificatesInit.init(socketFactory, trustManager)
//
//                val client = OkHttpClient.Builder()
//                    .addInterceptor(httpLoggingInterceptor)
//                    .addInterceptor(RequestSignInterceptor())
//                    .sslSocketFactory(socketFactory[0]!!, trustManager[0]!!)
//                    .build()
//
//                val retrofit = Retrofit.Builder()
//                    .baseUrl(ApiConfig.baseUrl)
//                    .client(client)
//                    .addConverterFactory(GsonConverterFactory.create())
//                    .build()
//
//                apiService = retrofit.create(ApiService::class.java)
//            }
//
//            return apiService!!
//        }

        private fun getBusinessApi(): ApiService {
            return NetClient.getInstance().getApi(ApiService::class.java)
        }

        fun login(
            loginParams: LoginParams,
            commonCallback: CommonCallback<SelfCashierAccountInfo>
        ) {
            getBusinessApi().login(loginParams).enqueue(commonCallback)
        }

        fun uploadFile(
            url: String,
            part: MultipartBody.Part,
            commonCallback: CommonCallback<String>
        ) {
            getBusinessApi().uploadFile(url, part).enqueue(commonCallback)
        }

        fun getDataSourceIp(
            selfCashierTerminalConfig: SelfCashierTerminalConfig,
            equType: String
        ): String? {

            if (selfCashierTerminalConfig?.scoRuntimeConfig == null) {
                throw RuntimeException("DataSourceIpUtils SelfCashierTerminalConfig is null")
            }

            val path = "${ApiService.UPLOAD_LOG_FILE}?equType=$equType"
            return if (TextUtils.isEmpty(selfCashierTerminalConfig.scoRuntimeConfig.erpIp)) (ApiConfig.baseUrl + path)
            else selfCashierTerminalConfig.scoRuntimeConfig.erpIp + path
        }
    }

//    fun login(
//        loginParams: LoginParams,
//        commonCallback: CommonCallback<SelfCashierAccountInfo>
//    ) {
//        val apiService = ApiClient.getInstance().getApi(ApiService.javaClass) as ApiService
//        apiService.login(loginParams).enqueue(commonCallback)
//    }
//
//    fun uploadFile(
//        url: String,
//        part: MultipartBody.Part,
//        commonCallback: CommonCallback<String>
//    ) {
//        val apiService = ApiClient.getInstance().getApi(ApiService.javaClass) as ApiService
//        apiService.uploadFile(url, part).enqueue(commonCallback)
//    }
//
//    fun getDataSourceIp(
//        selfCashierTerminalConfig: SelfCashierTerminalConfig,
//        equType: String
//    ): String? {
//
//        if (selfCashierTerminalConfig?.scoRuntimeConfig == null) {
//            throw RuntimeException("DataSourceIpUtils SelfCashierTerminalConfig is null")
//        }
//
//        val path = "${ApiService.UPLOAD_LOG_FILE}?equType=$equType"
//        return if (TextUtils.isEmpty(selfCashierTerminalConfig.scoRuntimeConfig.erpIp)) (ApiConfig.baseUrl + path)
//        else selfCashierTerminalConfig.scoRuntimeConfig.erpIp + path
//    }
}