package com.gh.gamecenter.floatingwindow

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.gh.gamecenter.common.base.fragment.BaseLazyFragment
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.core.provider.IFloatingWindowProvider
import com.gh.gamecenter.floatingwindow.retrofit.RetrofitManager
import io.reactivex.disposables.Disposable

@Route(path = RouteConsts.provider.floatingwindow, name = "FloatingWindow 暴露服务")
class FloatingWindowProviderImpl : IFloatingWindowProvider {

    override fun getFloatingWindow(gameId: String,
                                   gameName: String,
                                   fragment: Fragment,
                                   recyclerView: RecyclerView): Disposable {
        return RetrofitManager.api
            .getFloatingWindow(gameId)
            .compose(singleToMain())
            .subscribe { response, throwable ->
                if (response != null) {
                    if (fragment is BaseLazyFragment) {
                        FloatingWindowManager.showFloatingWindowIfNeeded(
                            response,
                            gameId,
                            gameName,
                            fragment,
                            recyclerView)
                    }
                } else throwable?.printStackTrace()
            }
    }

    override fun init(context: Context?) {
        // Do nothing
    }

}