package com.gh.gamecenter.catalog

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.filter.RegionSettingHelper
import com.gh.common.util.LogUtils
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.gamecenter.entity.SpecialCatalogEntity
import com.gh.gamecenter.feature.exposure.ExposureSource
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable
import io.reactivex.Single

class SpecialCatalogViewModel(
    application: Application,
    private val mCatalogId: String,
    private val mCatalogTitle: String,
    private val mIsCategoryV2: Boolean,
    private val mExposureSourceList: List<ExposureSource>?
) : ListViewModel<SpecialCatalogEntity, SpecialCatalogItemData>(application) {

    private val mApi = RetrofitManager.getInstance().api
    val basicExposureSource by lazy {
        arrayListOf<ExposureSource>().apply {
            if (!mExposureSourceList.isNullOrEmpty()) {
                addAll(mExposureSourceList)
            }
            add(ExposureSource("分类", mCatalogTitle))
        }
    }
    var catalogEntrance = ""

    override fun provideDataObservable(page: Int): Observable<List<SpecialCatalogEntity>>? = null

    override fun provideDataSingle(page: Int): Single<List<SpecialCatalogEntity>> {
        return when (mIsCategoryV2) {
            true -> mApi.getSpecialCategories(mCatalogId, page)
            false -> mApi.getSpecialCatalogs(mCatalogId, page)
        }
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { list ->
            val itemDataList = arrayListOf<SpecialCatalogItemData>()
            list.forEachIndexed { index, it ->
                when (it.type) {
                    "轮播图" -> itemDataList.add(SpecialCatalogItemData(banner = it).apply { position = index })

                    "图片" -> itemDataList.add(SpecialCatalogItemData(bigImage = it).apply { position = index })

                    "专题" -> {
                        itemDataList.add(SpecialCatalogItemData(header = it).apply { position = index })
                        itemDataList.add(SpecialCatalogItemData(subject = it).apply { position = index })
                        it.link.data = RegionSettingHelper.filterGame(it.link.data)
                        it.link.data.forEach { game ->
                            game.subjectId = it.link.id
                            game.containerType =
                                if (mIsCategoryV2) ExposureEntity.CATEGORY_V2_ID else ExposureEntity.CATEGORY_ID
                            game.containerId = mCatalogId
                        }
                    }

                    "专题合集" -> {
                        itemDataList.add(SpecialCatalogItemData(header = it).apply { position = index })
                        itemDataList.add(SpecialCatalogItemData(subjectCollection = it).apply { position = index })
                    }
                }
            }
            mResultLiveData.postValue(itemDataList)
        }
    }

    fun logSpecialCatalogContentClick(contentType: String, contentName: String, listPosition: Int) {
        if (mIsCategoryV2) {
            LogUtils.logSpecialCategoryV2ContentClickEvent(
                catalogEntrance,
                mCatalogTitle,
                "${contentType}_${if (contentName.isBlank()) "图片" else contentName}",
                listPosition
            )
        } else {
            LogUtils.logSpecialCatalogContentClickEvent(
                catalogEntrance,
                "${mCatalogTitle}_${contentType}_${contentName}",
                0,
                listPosition
            )
        }
    }

    fun logSpecialCatalogSpecificContentClick(
        contentType: String,
        contentName: String,
        targetName: String,
        listPosition: Int,
        specificListPosition: Int
    ) {
        if (mIsCategoryV2) {
            LogUtils.logSpecialCategoryV2SpecificContentClickEvent(
                catalogEntrance,
                mCatalogTitle,
                "${contentType}_${contentName}_${targetName}",
                listPosition,
                specificListPosition
            )
        } else {
            LogUtils.logSpecialCatalogSpecificContentClickEvent(
                catalogEntrance,
                "${mCatalogTitle}_${contentType}_${contentName}_${targetName}",
                0,
                listPosition,
                specificListPosition
            )
        }
    }

    fun logSpecialCatalogBannerClick(bannerTitle: String, contentName: String, bannerPosition: Int) {
        if (mIsCategoryV2) {
            LogUtils.logSpecialCategoryV2BannerClickEvent(
                catalogEntrance,
                mCatalogTitle,
                "${if (bannerTitle.isBlank()) "空" else bannerTitle}_${contentName}",
                bannerPosition
            )
        } else {
            LogUtils.logSpecialCatalogBannerClickEvent("${mCatalogTitle}_${bannerTitle}_${contentName}", bannerPosition)
        }
    }

    class Factory(
        private val mCatalogId: String,
        private val mCatalogTitle: String,
        private val mIsCategoryV2: Boolean,
        private val mExposureSourceList: List<ExposureSource>?
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return SpecialCatalogViewModel(
                HaloApp.getInstance().application,
                mCatalogId,
                mCatalogTitle,
                mIsCategoryV2,
                mExposureSourceList
            ) as T
        }
    }
}