package com.base.pdfreaderallpdfreader.ui.main.recent

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.base.pdfreaderallpdfreader.bean.DocumentBean
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_PDF
import com.base.pdfreaderallpdfreader.ui.main.getAllDocument
import com.base.pdfreaderallpdfreader.ui.main.pdfCheck
import com.base.pdfreaderallpdfreader.ui.main.recentFilter
import com.base.pdfreaderallpdfreader.utils.SpStringUtils
import com.base.pdfreaderallpdfreader.utils.SpStringUtils.LAST_VIEW_KEY

class RecentViewModel : ViewModel() {

    private val _text = MutableLiveData<String>().apply {
        value = "This is home Fragment"
    }
    val text: LiveData<String> = _text


    fun getRecentDocument(context: Context): List<DocumentBean> {
        val all = getAllDocument(context)
        val recentList = SpStringUtils.getSpStringList(LAST_VIEW_KEY)
        all.forEach {
            if (it.type == TYPE_PDF) {
                context.pdfCheck(it)
            }
            recentFilter(recentList, it)
        }
        return all.filter { it.lastViewTime != 0L }.sortedByDescending { it.lastViewTime }
    }
}