package com.base.pdfviewerscannerwhite.ui.document

import android.content.Intent
import androidx.lifecycle.lifecycleScope
import com.base.pdfviewerscannerwhite.bean.ConstObject
import com.base.pdfviewerscannerwhite.bean.DocumentBean
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_PDF
import com.base.pdfviewerscannerwhite.databinding.FragmentDocumentBinding
import com.base.pdfviewerscannerwhite.helper.BaseFragment
import com.base.pdfviewerscannerwhite.ui.adapter.DocumentAdapter
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfActivity
import com.base.pdfviewerscannerwhite.utils.PermissionUtils.checkStorePermission
import com.base.pdfviewerscannerwhite.utils.SpStringUtils
import com.base.pdfviewerscannerwhite.utils.SpStringUtils.BOOKMARK_KEY
import com.base.pdfviewerscannerwhite.utils.getMediaFile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File


class DocumentFragment() : BaseFragment<FragmentDocumentBinding>() {

    private var type = TYPE_PDF
    private var documentList: List<DocumentBean>? = null
    private lateinit var adapter: DocumentAdapter

    constructor(type: String) : this() {
        this.type = type
    }

    override val binding: FragmentDocumentBinding by lazy {
        FragmentDocumentBinding.inflate(layoutInflater)
    }

    override fun setView() {
        initAdapter()

        if (documentList != null) {
            adapter.submitList(documentList)
        } else {
            if (requireContext().checkStorePermission()) {
                initData()
            }
        }

    }

    private fun initAdapter() {
        adapter = DocumentAdapter()
        adapter.bookmarkAction = { addRemove, path ->
            if (addRemove) {
                SpStringUtils.addSpString(BOOKMARK_KEY, path)
            } else {
                SpStringUtils.deleteSpString(BOOKMARK_KEY, path)
            }
        }
        adapter.itemClick = { path ->
            startActivity(Intent(requireContext(), PdfActivity::class.java).apply {
                putExtra("path", path)
            })
        }
        binding.rv.adapter = adapter
    }

    private fun initData() = lifecycleScope.launch(Dispatchers.IO) {

        val selectionArgs = arrayOf(ConstObject.MIME_TYPE_PDF)
        if (type == TYPE_PDF) {

        }
        val bookmarkList = SpStringUtils.getSpStringList(BOOKMARK_KEY)

        val list = requireContext().getMediaFile(selectionArgs = selectionArgs)
        val documentList = list.map {
            DocumentBean(it.path, type, bookmarkList.contains(it.path))
        }
        this@DocumentFragment.documentList = documentList

        launch(Dispatchers.Main) {
            adapter.submitList(documentList)
        }
    }

    fun setRecentList() {
        val recentList = documentList?.filter {
            (System.currentTimeMillis() - File(it.path).lastModified()) < 300L * 24 * 60 * 60 * 1000
        }
        adapter.submitList(recentList)
    }

    fun setAllList() {
        adapter.submitList(documentList)
    }

    fun setBookmarkList() {
        val bookmarkList = documentList?.filter { it.isBookmarked }
        adapter.submitList(bookmarkList)
    }


}