package com.gh.common.provider

import com.gh.common.util.PackageUtils
import android.annotation.SuppressLint
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.core.provider.IAcceleratorDataHolderProvider
import com.gh.gamecenter.feature.entity.BaseEntity
import com.gh.gamecenter.feature.entity.VipEntity
import com.gh.gamecenter.login.retrofit.RetrofitManager
import com.gh.gamecenter.login.user.UserManager
import com.halo.assistant.accelerator.repository.AcceleratorDataHolder

@com.therouter.inject.ServiceProvider
class IAcceleratorDataHolderProviderImpl : IAcceleratorDataHolderProvider {

    override fun isPaidVip(): Boolean =
        AcceleratorDataHolder.instance.isPaidVip

    override fun getGhVersionName(): String {
        return PackageUtils.getGhVersionName()
    }

    /**
     *   请注意，由于光环后端无法获取 奇游时长套餐 加速时长，这里的 vipStatus不准，
     *   这里只做参考，具体的状态需要奇游sdk提供，如果奇游sdk返回失败，则以这条接口结果为准
     */
    @SuppressLint("CheckResult")
    override fun loadVipEntityFromGh(userId: String, callBack: (Any?) -> Unit) {
        RetrofitManager.getInstance().newApi.getVipStatus(
            userId.ifBlank { UserManager.getInstance().userId },
            "gjonline_vip",
            true
        )
            .compose(singleToMain())
            .subscribe(object : BiResponse<BaseEntity<VipEntity>>() {
                override fun onSuccess(data: BaseEntity<VipEntity>) {
                    callBack(data.data)
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    callBack(null)
                }

            })
    }

    override fun updateVipEntity(data: Any) {
        if (data is VipEntity) {
            AcceleratorDataHolder.instance.setVipEntity(data)
        }
    }


    override fun clear() {
        AcceleratorDataHolder.instance.clear()
    }
}