package com.gh.gamecenter.subject.tab

import android.app.Application
import android.content.Context
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.toJson
import com.gh.gamecenter.core.utils.GsonUtils
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.entity.SubjectData
import com.gh.gamecenter.livedata.Event
import com.halo.assistant.HaloApp

class ColumnCollectionTabViewModel(
    application: Application,
    private val columnCollectionId: String,
    private val customSize: Int,
    private val subjectDataList: List<SubjectData>
) : AndroidViewModel(application) {
    private val columnCollectionSp by lazy {
        HaloApp.getInstance().getSharedPreferences(Constants.SP_COLUMN_COLLECTION_CUSTOM_TAB, Context.MODE_PRIVATE)
    }

    val tabListLiveData = MutableLiveData<List<SubjectData>>()
    val followListLiveData = MutableLiveData<List<SubjectData>>()
    val moreListLiveData = MutableLiveData<List<SubjectData>>()

    val addFollowLiveData = MutableLiveData<Event<SubjectData>>()
    val addMoreLiveData = MutableLiveData<Event<SubjectData>>()

    init {
        initData()
    }

    private fun initData() {
        val customTabIdList = GsonUtils.fromJsonList<String>(SPUtils.getString(columnCollectionSp, columnCollectionId))
        if (customTabIdList.isEmpty()) {
            val followTabList = subjectDataList.take(customSize)
            tabListLiveData.postValue(followTabList)
            followListLiveData.postValue(followTabList)
            moreListLiveData.postValue(subjectDataList - followTabList.toSet())
        } else {
            val newAddedForbiddenList = subjectDataList.filter { it.isForbidden && it.subjectId !in customTabIdList }
            val tabList = arrayListOf<SubjectData>().apply { addAll(newAddedForbiddenList) }

            customTabIdList.forEach { subjectId->
                subjectDataList.find { subjectId == it.subjectId }?.let {
                    tabList.add(it)
                }
            }

            val followTabList = tabList.take(CUSTOM_MAX_TAB).ifEmpty { subjectDataList.take(customSize) }
            tabListLiveData.postValue(followTabList)
            followListLiveData.postValue(followTabList)
            moreListLiveData.postValue(subjectDataList - followTabList.toSet())
        }
    }

    fun addFollow(subjectData: SubjectData) {
        addFollowLiveData.postValue(Event(subjectData))
    }

    fun addMore(subjectData: SubjectData) {
        addMoreLiveData.postValue(Event(subjectData))
    }

    fun saveCustomTab(followTabList: List<SubjectData>) {
        tabListLiveData.postValue(followTabList)
        followListLiveData.postValue(followTabList)
        moreListLiveData.postValue(subjectDataList - followTabList.toSet())

        val followSubjectIdList = followTabList.map { it.subjectId ?: "" }.distinct()
        SPUtils.setString(columnCollectionSp, columnCollectionId, followSubjectIdList.toJson())
    }

    companion object {
        const val CUSTOM_MAX_TAB = 15
    }

    class Factory(
        private val columnCollectionId: String,
        private val customSize: Int,
        private val subjectDataList: List<SubjectData>
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return ColumnCollectionTabViewModel(
                HaloApp.getInstance().application,
                columnCollectionId,
                customSize,
                subjectDataList
            ) as T
        }
    }
}