package com.gh.gamecenter.home.custom.model

import android.annotation.SuppressLint
import com.gh.common.util.AdHelper
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.entity.PKEntity
import com.gh.gamecenter.common.utils.toRequestBody
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.entity.PullDownPush
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.feature.entity.FloatingWindowEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.retrofit.service.ApiService
import com.gh.gamecenter.wrapper.MainWrapperRepository
import com.halo.assistant.HaloApp
import io.reactivex.Observable
import io.reactivex.Single
import okhttp3.ResponseBody

class CustomPageRemoteDataSource(
    private val api: ApiService,
    private val newApi: ApiService,
    private val mainWrapperRepository: MainWrapperRepository
) {
    fun loadSuspendedWindow(pageId: String): Single<List<FloatingWindowEntity>> {
        return newApi.loadSuspendedWindow(pageId, BuildConfig.VERSION_NAME, HaloApp.getInstance().channel)
    }

    fun loadPullDownPush(pageId: String): Single<PullDownPush> {
        return newApi.loadPullDownPush(pageId, BuildConfig.VERSION_NAME, HaloApp.getInstance().channel)
    }

    @SuppressLint("CheckResult")
    fun loadCustomPageData(
        pageId: String,
        page: Int,
        forceLoad: Boolean = false,
    ): Single<CustomPageData> {
        val isDefaultPage = pageId == mainWrapperRepository.defaultCustomPageId

        return if (page == 1 &&
            isDefaultPage &&
            mainWrapperRepository.customPageLiveData.value != null &&
            !forceLoad
        ) {
            Single.create {
                it.onSuccess(mainWrapperRepository.customPageLiveData.value!!)
            }
        } else {
            newApi.getCustomPageData(
                pageId,
                page,
                if (!isDefaultPage) AdHelper.getIdfaString() else ""
            )
        }
    }

    fun loadCollectionContent(item: CustomSubjectCollectionItem): Single<List<CustomPageData.LinkColumnCollection.CustomSubjectEntity>> =
        if (item.isSubjectCollection) {
            newApi.getColumnsCollectionContents(item.data.id, "component", item.loadPage + 1, 1)
        } else {
            newApi.getGameCollectionContents(item.data.id, "component", item.loadPage + 1, 1)
        }

    fun loadChangeSubjectGame(subjectEntity: SubjectEntity): Observable<List<GameEntity>> =
        if (subjectEntity.isQQColumn) {
            newApi.getSubjectQGame(subjectEntity.id)
        } else if (subjectEntity.isWechatColumn) {
            newApi.getSubjectWGame(subjectEntity.id)
        } else {
            api.getSubjectGame(subjectEntity.id)
        }

    fun loadSlideDiscoverCardGames(): Observable<List<GameEntity>> {
        val paramsMap = if (SPUtils.getBoolean(Constants.SP_DISCOVER_FORCE_REFRESH)) {
            mapOf("page_size" to 3, "view" to "sub_slide", "refresh" to "true")
        } else {
            mapOf("page_size" to 3, "view" to "sub_slide")
        }
        return api.getDiscoveryGames(1, paramsMap)
            .map { it.games }
    }

    fun loadPKData(pkIdList: List<String>): Single<List<PKEntity>> {
        val data = mapOf(
            "pk_ids" to pkIdList
        )
        return newApi.getPKList(data.toRequestBody())
    }

    fun postPKVote(pkId: String, isPositive: Boolean): Single<ResponseBody> {
        val data = mapOf(
            "result" to if (isPositive) "option1" else "option2"
        )
        return newApi.postPK(pkId, data.toRequestBody())
    }
}