package com.base.pdfviewerscannerwhite.ui.main

import android.app.Dialog
import android.content.Intent
import androidx.lifecycle.lifecycleScope
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.artifex.mupdf.viewer.PdfTestActivity
import com.base.pdfviewerscannerwhite.ui.adapter.DocumentAdapter
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfActivity
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfSplitActivity
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentRenameDialog
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showPdfDetailDialog
import com.base.pdfviewerscannerwhite.utils.PermissionUtils.checkStorePermission
import java.io.File

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

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

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

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

    override fun setView() {
        documentPresenter = DocumentPresenter(this, type, lifecycleScope)
        initAdapter()

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

    private fun initAdapter() {
        adapter = DocumentAdapter()
        adapter.bookmarkAction = { addRemove, path ->
            documentPresenter.saveBookmarkChange(addRemove, path)
        }
        adapter.itemClick = { item ->
            PdfActivity::class.java
//            startActivity(Intent(requireContext(), PdfActivity::class.java).apply {
//                putExtra("path", path)
//            })
            val intent: Intent = Intent(requireContext(), PdfTestActivity::class.java)
            intent.putExtra("path", item.path)
            intent.putExtra("uri", item.uri.toString())
            val size = File(item.path).length()
            intent.putExtra("size", size)
            startActivity(intent)
        }
        adapter.moreAction = { item ->
            if (item.type == TYPE_PDF) {
                requireContext().showPdfDetailDialog(this, item, adapter, dismissAction = { firstDialog = null })
            }
        }
        binding.rv.adapter = adapter
    }

    override fun refreshDocumentRv(documentList: List<DocumentBean>) {
        this@DocumentFragment.documentList = documentList
        adapter.submitList(documentList)
    }

    override fun splitPdf(path: String) {
        startActivity(Intent(requireContext(), PdfSplitActivity::class.java).apply {
            putExtra("path", path)
        })
    }

    override fun deleteDocument(item: DocumentBean) {
        val list = documentList?.toMutableList()
        documentPresenter.deleteDocumentBean(item)
        list?.remove(item)
        adapter.submitList(list)
    }

    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)
    }

    override fun dialogRename(item: DocumentBean) {
        val file = File(item.path)
        requireContext().showDocumentRenameDialog(file.name, firstDialog) { newName ->
            documentPresenter.renameDocumentBean(requireContext(), file, newName)
        }
    }


}