package com.gh.gamecenter.search

import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.db.ISearchHistoryDao

class SearchDefaultViewModel(private val dao: ISearchHistoryDao) : ViewModel() {

    val historySearchLiveData = Transformations.map(dao.historyLiveData){
        it
    }
    var isExistHotSearch: Boolean = false
    var isExistHotAndDiscoveryTag: Boolean = false
    var isExistHistory: Boolean = false
    var isExistRankList: Boolean = false

    fun deleteAll() {
        dao.deleteAll()
    }

    fun add(item: String) {
        val oldList = historySearchLiveData.value
        if (oldList.isNullOrEmpty()) {
            dao.add(item)
            return
        }

        if (!oldList.contains(item)) {
            dao.add(item)
        }
    }

    class Factory(private val dao: ISearchHistoryDao) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return SearchDefaultViewModel(dao) as T
        }
    }

}