package com.base.pdfreaderallpdfreader.ui.view

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.text.method.PasswordTransformationMethod
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.core.widget.addTextChangedListener
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.databinding.DialogPdfPasswordBinding
import com.base.pdfreaderallpdfreader.utils.LogEx
import com.base.pdfreaderallpdfreader.utils.PdfBoxUtils
import com.base.pdfreaderallpdfreader.utils.PdfBoxUtils.checkPwd
import com.base.pdfreaderallpdfreader.utils.ToastUtils.toast
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import java.io.File

object PwdDialog {

    @SuppressLint("SetTextI18n")
    fun Context.showPdfPwdDialog(
        state: Int,
        path: String = "",
        uri: String? = null,
        firstDialog: Dialog? = null,
        isCheckPwd: Boolean = false,
        verificationAction: ((pwd: String) -> Unit)? = null,
        encryptionAction: (() -> Unit)? = null,
        cancelAction: (() -> Unit)? = null,
    ) {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogPdfPasswordBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        val window = dialog.window
        window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

        dialog.show()

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

        if (!isCheckPwd) {
            if (state == 1) {
                binding.tvTittle.text = getString(R.string.delete_password)
                binding.tvTip.text = getString(R.string.delete_password_the_file_is_not_password_protected)
            }
            if (state == 0) {
                binding.tvTittle.text = getString(R.string.set_password)
                binding.tvTip.text = getString(R.string.set_password_protection_pdf)

            }
        } else {
            binding.tvTittle.text = getString(R.string.input_password)
            val file = File(path)
            binding.tvTip.text = getString(R.string.password_protected, file.name)
            binding.tvInputTip.visibility = View.VISIBLE
        }

        binding.edit.requestFocus()
        binding.edit.addTextChangedListener {
            binding.tvConfirm.isEnabled = it.toString().isNotEmpty()
            binding.tvErrorTip.visibility = View.GONE
        }

        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
            cancelAction?.invoke()
        }
        binding.tvConfirm.setOnClickListener {

            val pwd = binding.edit.text.toString()

            if (!isCheckPwd) {

                //加锁逻辑
                if (state == 0) {
                    PdfBoxUtils.setPassword(path, pwd, pwd)
                    toast("Success Encryption")
                    encryptionAction?.invoke()
                    dialog.dismiss()
                    firstDialog?.dismiss()
                }
                //解锁逻辑
                if (state == 1) {
                    val result = checkPwd(path, pwd, uri)
                    LogEx.logDebug("checkPwd", "result=$result")
                    if (result) {
                        PdfBoxUtils.clearPassword(path, pwd)
                        toast("clear Encryption")
                        encryptionAction?.invoke()
                        dialog.dismiss()
                        firstDialog?.dismiss()
                    } else {
                        binding.tvErrorTip.visibility = View.VISIBLE
                    }
                }
            } else {
                //验证密码逻辑
                val result = checkPwd(path, pwd, uri)
                if (!result) {
                    binding.tvErrorTip.visibility = View.VISIBLE
                    return@setOnClickListener
                }
                dialog.dismiss()
                firstDialog?.dismiss()
                verificationAction?.invoke(pwd)
            }

        }
        binding.ivEye.setOnClickListener {

            if (binding.edit.transformationMethod == null) {
                // 隐藏密码
                binding.edit.transformationMethod = PasswordTransformationMethod()
                binding.ivEye.setImageResource(R.mipmap.weishuru)
            } else {
                // 显示密码
                binding.edit.transformationMethod = null
                binding.ivEye.setImageResource(R.mipmap.yishuru)
            }
        }

    }
}