package com.base.pdfreader2.ui.view

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.FileProvider
import com.base.pdfreader2.R
import com.base.pdfreader2.bean.DocumentBean
import com.base.pdfreader2.databinding.DialogPdfHomeMoreBinding
import com.base.pdfreader2.databinding.DialogPdfMoreBinding
import com.base.pdfreader2.ui.main.DocumentFragment
import com.base.pdfreader2.ui.main.DocumentPresenter
import com.base.pdfreader2.ui.pdf.PdfView
import com.base.pdfreader2.ui.view.DialogView.showDeleteDialog
import com.base.pdfreader2.ui.view.DocumentDialog.showDocumentDetail
import com.base.pdfreader2.ui.view.NameDialog.showDocumentRenameDialog
import com.base.pdfreader2.ui.view.PwdDialog.showPdfPwdDialog
import com.base.pdfreader2.utils.IntentShareUtils
import com.base.pdfreader2.utils.KotlinExt.toFormatSize
import com.base.pdfreader2.utils.KotlinExt.toFormatTime
import com.base.pdfreader2.utils.LogEx
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import java.io.File

object PdfDialog {

    //pdf首页弹窗
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    fun Context.showPdfHomeMoreDialog(
        item: DocumentBean,
        documentFragment: DocumentFragment,
        documentPresenter: DocumentPresenter,
    ): BottomSheetDialog {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogPdfHomeMoreBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(false)

        dialog.show()

        val parentView = binding.root.parent as View
        val behavior = BottomSheetBehavior.from(parentView)
        //展开
        behavior.state = BottomSheetBehavior.STATE_EXPANDED

        val file = File(item.path)
        binding.tvName.text = file.name
        binding.tvInfo.text = file.lastModified().toFormatTime() + " " + file.length().toFormatSize()
        if (item.isBookmarked) {
            binding.ivBookmark.setImageResource(R.mipmap.h_soucang_s)
        } else {
            binding.ivBookmark.setImageResource(R.mipmap.h_soucang_n)
        }
        binding.ivBookmark.setOnClickListener {
            item.isBookmarked = !item.isBookmarked
            if (item.isBookmarked) {
                binding.ivBookmark.setImageResource(R.mipmap.h_soucang_s)
            } else {
                binding.ivBookmark.setImageResource(R.mipmap.h_soucang_n)
            }
            documentPresenter.saveBookmarkChange(item.isBookmarked, item.path)
            documentFragment.refreshData()
        }
        binding.llRename.setOnClickListener {
            showDocumentRenameDialog(file.name, okAction = { newName ->
                dialog.dismiss()
                documentPresenter.renameDocumentBean(file, newName, documentFragment)
            })
        }
        binding.llSplit.setOnClickListener {
            dialog.dismiss()
            if (item.state == 1) {
                showPdfPwdDialog(
                    state = item.state, path = item.path, firstDialog = dialog, isCheckPwd = true, verificationAction = {
                        documentFragment.splitPdf(item.path, it)
                    })
            } else {
                documentFragment.splitPdf(item.path, null)
            }
        }
        binding.llMerge.setOnClickListener {
            dialog.dismiss()
            documentFragment.mergePdf(item.path)
        }
        binding.llDelete.setOnClickListener {
            dialog.dismiss()
            showDeleteDialog {
                documentPresenter.deleteDocument(item, documentFragment)
            }
        }
        binding.llDetail.setOnClickListener {
            showDocumentDetail(item.path)
        }
        binding.llShare.setOnClickListener {
            val intent = IntentShareUtils.sharePdfIntent(item.uri)
            runCatching {
                startActivity(intent)
            }
        }
        if (item.state == 1) {
            binding.tvLock.text = getString(R.string.unlock_pdf)
        }
        if (item.state == 0) {
            binding.tvLock.text = getString(R.string.lock_pdf)
        }
        binding.llLock.setOnClickListener {
            showPdfPwdDialog(state = item.state, path = item.path,
                firstDialog = dialog,
                isCheckPwd = false,
                encryptionAction = {
                    dialog.dismiss()
                    documentFragment.changePdfLock(item)
                })
        }
        dialog.setOnDismissListener {
        }
        return dialog
    }

    fun Activity.showPdfMoreDialog(
        pdfView: PdfView,
        pageNumber: Int,
        pafPath: String,
    ) {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogPdfMoreBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(false)

        dialog.show()

        val parentView = binding.root.parent as View
        val behavior = BottomSheetBehavior.from(parentView)
        //展开
        behavior.state = BottomSheetBehavior.STATE_EXPANDED

        binding.llJump.setOnClickListener {
//            showJumpPageNumberDialog(pageNumber) { pageIndex ->
//                dialog.dismiss()
//                pdfView.jumpPage(pageIndex)
//            }
        }
        binding.llSplit.setOnClickListener {
            dialog.dismiss()
            pdfView.jumpSplit()
        }
        binding.llDetail.setOnClickListener {
            showDocumentDetail(pafPath)
        }
        binding.llShare.setOnClickListener {
            dialog.dismiss()
            runCatching {
                val pkg = this.packageName
                LogEx.logDebug("showPdfMoreDialog", "pkg=$pkg")
                val uri = FileProvider.getUriForFile(
                    this, this.packageName + ".provider", File(pafPath)
                )
                startActivity(Intent.createChooser(IntentShareUtils.sharePdfIntent(uri), "Share PDF"))
            }
        }

    }

}