package com.gh.gamecenter.collection

import android.app.Application
import com.gh.gamecenter.core.AppExecutor
import com.gh.common.constant.Config
import com.gh.common.history.HistoryDatabase
import com.gh.common.history.HistoryHelper
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.entity.MyVideoEntity
import com.gh.gamecenter.entity.PersonalHistoryEntity
import com.gh.gamecenter.feature.entity.User
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable
import io.reactivex.Single

class VideoViewModel(application: Application) : ListViewModel<MyVideoEntity, MyVideoEntity>(application) {

    var type = ""
    private val mApi = RetrofitManager.getInstance().api

    private fun getFilter(): String = UrlFilterUtils.getFilterQuery("scene", "question_answer", "type", "video")

    override fun provideDataObservable(page: Int): Observable<MutableList<MyVideoEntity>>? {
        if (type == VideoFragment.VideoStyle.COLLECT.value) {
            return mApi.getCollectionVideo(UserManager.getInstance().userId, page, Config.VIDEO_PAGE_SIZE)
        } else if (type == VideoFragment.VideoStyle.MINE.value) {
            return mApi.getPersonalHistory(
                UserManager.getInstance().userId,
                page,
                HaloApp.getInstance().channel,
                getFilter()
            )
                .map {
                    transformMyVideoEntity(it)
                }
        }
        return null
    }

    private fun transformMyVideoEntity(oldList: List<PersonalHistoryEntity>): ArrayList<MyVideoEntity> {
        val newList = ArrayList<MyVideoEntity>()
        oldList.forEach { entity ->
            if (entity.status == "pass") {
                val videoEntity = MyVideoEntity().apply {
                    id = entity.id
                    poster = entity.poster
                    url = entity.url
                    vote = entity.count.vote
                    commentCount = entity.count.comment
                    length = entity.length
                    status = entity.status ?: ""
                    title = entity.title
                    des = entity.des
                    user = with(User()) {
                        id = entity.user?.id ?: ""
                        name = entity.user?.name ?: ""
                        icon = entity.user?.icon ?: ""
                        badge = entity.user?.badge
                        this
                    }
                }
                newList.add(videoEntity)
            }
        }
        return newList
    }

    override fun provideDataSingle(page: Int): Single<MutableList<MyVideoEntity>>? {
        if (type == VideoFragment.VideoStyle.BROWSING_HISTORY.value) {
            if (page > 5) {
                return Single.create { it.onSuccess(arrayListOf()) }
            }
            if (type == VideoFragment.VideoStyle.BROWSING_HISTORY.value) {
                return HistoryDatabase.instance.videoHistoryDao().getVideoWithOffset(20, (page - 1) * 20)
            }
        }
        return null
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { mResultLiveData.postValue(it) }
    }

    fun removeHistory(myVideoEntity: MyVideoEntity) {
        HistoryHelper.deleteVideoEntity(myVideoEntity.id)
        AppExecutor.uiExecutor.executeWithDelay({ load(LoadType.REFRESH) }, 100)
    }

    fun removeHistories(ids: List<String>) {
        mResultLiveData.value?.let { datas ->
            ids.forEach { id ->
                HistoryHelper.deleteVideoEntity(id)
                val entity = datas.find { it.id == id }
                datas.remove(entity)
            }

            if (datas.size == 0) {
                AppExecutor.uiExecutor.executeWithDelay({ load(LoadType.REFRESH) }, 100)
            } else {
                mResultLiveData.postValue(datas)
            }
        }
    }

}