package com.base.pdfviewerscannerwhite.ui.main

import android.content.Intent
import android.net.Uri
import androidx.lifecycle.lifecycleScope
import com.base.pdfviewerscannerwhite.bean.ConstObject.DO_LOCK_PDF
import com.base.pdfviewerscannerwhite.bean.ConstObject.DO_MERGE_PDF
import com.base.pdfviewerscannerwhite.bean.ConstObject.DO_SPLIT_PDF
import com.base.pdfviewerscannerwhite.bean.ConstObject.DO_UNLOCK_PDF
import com.base.pdfviewerscannerwhite.databinding.FragmentToolBinding
import com.base.pdfviewerscannerwhite.helper.BaseFragment
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfActivity
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfBoxUtils
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfSelectActivity
import com.base.pdfviewerscannerwhite.ui.set.SetActivity
import com.base.pdfviewerscannerwhite.ui.view.PdfDialog.showPdfPwdDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class ToolFragment : BaseFragment<FragmentToolBinding>() {

    private val TAG = "ToolFragment"

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

    override fun setView() {
    }

    override fun setListener() {
        super.setListener()
        binding.llManager.setOnClickListener {
            val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
            intent.addCategory(Intent.CATEGORY_OPENABLE)
            intent.setType("application/pdf")
            intent.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION


            (requireActivity() as MainActivity).launcher.launch(intent) {
                val uri = it.data?.data ?: Uri.EMPTY

                lifecycleScope.launch(Dispatchers.IO) {
                    val state = PdfBoxUtils.checkPdfEncryption(uri.toString())
                    if (state == 0) {
                        launch(Dispatchers.Main) {
                            startActivity(Intent(requireContext(), PdfActivity::class.java).apply {
                                putExtra("uri", uri.toString())
                            })
                        }
                    } else {
                        launch(Dispatchers.Main) {
                            requireContext().showPdfPwdDialog(
                                state = state,
                                uri = uri.toString(),
                                isCheckPwd = true,
                                verificationAction = { pwd ->
                                    startActivity(Intent(requireContext(), PdfActivity::class.java).apply {
                                        putExtra("uri", uri.toString())
                                        putExtra("pwd", pwd)
                                    })
                                })
                        }
                    }
                }


            }
        }
        binding.llMerge.setOnClickListener {
            startActivity(Intent(requireContext(), PdfSelectActivity::class.java).apply {
                putExtra("doWhat", DO_MERGE_PDF)
            })
        }
        binding.llSplit.setOnClickListener {
            startActivity(Intent(requireContext(), PdfSelectActivity::class.java).apply {
                putExtra("doWhat", DO_SPLIT_PDF)
            })
        }
        binding.llLock.setOnClickListener {
            startActivity(Intent(requireContext(), PdfSelectActivity::class.java).apply {
                putExtra("doWhat", DO_LOCK_PDF)
            })
        }
        binding.llUnlock.setOnClickListener {
            startActivity(Intent(requireContext(), PdfSelectActivity::class.java).apply {
                putExtra("doWhat", DO_UNLOCK_PDF)
            })
        }
        binding.llSet.setOnClickListener {
            startActivity(Intent(requireContext(), SetActivity::class.java))
        }
    }

}