package com.base.pdfreaderallpdfreader.ui.search

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.base.pdfreaderallpdfreader.bean.DocumentBean
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_EXCEL
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_PDF
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_PPT
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_WORD
import com.base.pdfreaderallpdfreader.ui.main.getAllDocument
import com.base.pdfreaderallpdfreader.ui.search.SearchActivity.Companion.SEARCH_MODE_ALL
import com.base.pdfreaderallpdfreader.ui.search.SearchActivity.Companion.SEARCH_MODE_EXCEL
import com.base.pdfreaderallpdfreader.ui.search.SearchActivity.Companion.SEARCH_MODE_PDF
import com.base.pdfreaderallpdfreader.ui.search.SearchActivity.Companion.SEARCH_MODE_PPT
import com.base.pdfreaderallpdfreader.ui.search.SearchActivity.Companion.SEARCH_MODE_WORD
import java.io.File
import java.util.Locale

class SearchViewModel : ViewModel() {

    var searchMode = SEARCH_MODE_ALL


    fun getSearchDocument(context: Context, search: String): List<DocumentBean> {

        val all = getAllDocument(context)
        val typeList: List<DocumentBean>
        when (searchMode) {
            SEARCH_MODE_PDF -> {
                typeList = all.filter { it.type == TYPE_PDF }
            }

            SEARCH_MODE_WORD -> {
                typeList = all.filter { it.type == TYPE_WORD }
            }

            SEARCH_MODE_EXCEL -> {
                typeList = all.filter { it.type == TYPE_EXCEL }
            }

            SEARCH_MODE_PPT -> {
                typeList = all.filter { it.type == TYPE_PPT }
            }

            else -> typeList = all
        }
        return typeList.filter { File(it.path).name.lowercase(Locale.ENGLISH).contains(search.lowercase(Locale.ENGLISH)) }
    }
}