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 MiyaApiRequest {

    companion object {

        private var sMiyaApi: MiyaApi? = null

        private fun getApi(): MiyaApi {
            if (null == sMiyaApi) {
                sMiyaApi = ApiClient.getInstance().creatApi(MiyaApi::class.java)
            }
            return sMiyaApi!!
        }

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

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

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

            if (selfCashierTerminalConfig?.scoRuntimeConfig == null) {
                throw RuntimeException("DataSourceIpUtils SelfCashierTerminalConfig is null")
            }
            val path = "${MiyaApi.UPLOAD_LOG_FILE}?equType=$equType"
            return if (TextUtils.isEmpty(selfCashierTerminalConfig.scoRuntimeConfig.erpIp))
                    (ApiConfig.baseUrl.plus(path))
            else selfCashierTerminalConfig.scoRuntimeConfig.erpIp + path
        }
    }
}