package com.base.pdfviewerscannerwhite.ui.document.pdf

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.activity.addCallback
import androidx.lifecycle.lifecycleScope
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.ads.AdmobHelper
import com.base.pdfviewerscannerwhite.ads.admob.AdmobInterstitialUtils
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.bean.DocumentBean
import com.base.pdfviewerscannerwhite.databinding.ActivityPdfSelectBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.helper.MyApplication
import com.base.pdfviewerscannerwhite.ui.main.DocumentAdapter
import com.base.pdfviewerscannerwhite.ui.main.DocumentPresenter
import com.base.pdfviewerscannerwhite.ui.main.DocumentView
import com.base.pdfviewerscannerwhite.ui.main.MainActivity.Companion.jumpDocument
import com.base.pdfviewerscannerwhite.ui.view.PdfDialog.showPdfPwdDialog
import com.base.pdfviewerscannerwhite.utils.LogEx

class PdfSelectActivity : BaseActivity<ActivityPdfSelectBinding>(), DocumentView {

    private val TAG = "PdfSelectActivity"
    private lateinit var documentPresenter: DocumentPresenter

    private lateinit var adapter: DocumentAdapter

    private var doWhat: String = ""

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

    override fun onResume() {
        super.onResume()
        updateAppLanguage(MyApplication.pdfSelectLanguage) {
            MyApplication.pdfSelectLanguage = it
        }
    }

    override fun initView() {
        doWhat = intent.extras?.getString("doWhat", "") ?: ""
        documentPresenter = DocumentPresenter(this, this, DocumentBean.TYPE_PDF, lifecycleScope)

        initAdapter()

        when (doWhat) {
            DO_SPLIT_PDF -> {
                binding.tvBtnNext.visibility = View.GONE
                binding.tvSelectTip.text = getString(R.string.select_a_project)
                documentPresenter.initData()
            }

            DO_MERGE_PDF -> {
                binding.tvSelectTip.text = getString(R.string.select_a_project)
                documentPresenter.initData()
            }

            DO_LOCK_PDF -> {
                binding.tvBtnNext.visibility = View.GONE
                binding.tvSelectTip.text = getString(R.string.select_a_project)
                documentPresenter.initUnLockData()
            }

            DO_UNLOCK_PDF -> {
                binding.tvBtnNext.visibility = View.GONE
                binding.tvSelectTip.text = getString(R.string.select_a_project)
                documentPresenter.initLoadData()
            }


        }

    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

        binding.tvBtnNext.setOnClickListener {
            if (doWhat == DO_MERGE_PDF) {
                val selectList = adapter.items.filter { it.isSelect }
                PdfMergeActivity.mergePdfList.clear()
                PdfMergeActivity.mergePdfList.addAll(selectList)
                startActivity(Intent(this, PdfMergeActivity::class.java))
            }
        }
    }

    @SuppressLint("StringFormatMatches")
    private fun initAdapter() {
        adapter = DocumentAdapter(this)
        adapter.itemClick = {
            if (doWhat == DO_SPLIT_PDF) {
                if (it.state == 0) {
                    startActivity(Intent(this, PdfSplitActivity::class.java).apply {
                        putExtra("path", it.path)
                    })
                } else {
                    showPdfPwdDialog(
                        state = it.state,
                        path = it.path,
                        isCheckPwd = true,
                        verificationAction = { pwd ->
                            startActivity(Intent(this, PdfSplitActivity::class.java).apply {
                                putExtra("path", it.path)
                                putExtra("pwd", pwd)
                            })
                        })
                }

            }
            if (doWhat == DO_LOCK_PDF) {
                showPdfPwdDialog(state = it.state, path = it.path, encryptionAction = {
                    if (AdmobHelper.canCommonShowAd()) {
                        AdmobInterstitialUtils.showInterstitialAd(this) { flag ->
                            adapter.remove(it)
                        }
                    } else {
                        adapter.remove(it)
                    }
                })
            }
            if (doWhat == DO_UNLOCK_PDF) {
                showPdfPwdDialog(state = it.state, path = it.path, encryptionAction = {
                    if (AdmobHelper.canCommonShowAd()) {
                        AdmobInterstitialUtils.showInterstitialAd(this) { flag ->
                            adapter.remove(it)
                        }
                    } else {
                        adapter.remove(it)
                    }
                })
            }
        }
        adapter.selectAction = {
            LogEx.logDebug(TAG, "selectAction $it")
            if (it == 0) {
                binding.tvSelectTip.visibility = View.INVISIBLE
                binding.tvBtnNext.isEnabled = false
            } else {
                binding.tvBtnNext.isEnabled = true
                binding.tvSelectTip.visibility = View.VISIBLE
                binding.tvSelectTip.text = getString(R.string.items_has_been_selected, it)
            }
        }
        binding.rv.adapter = adapter
    }

    override fun refreshDocumentRv(documentList: List<DocumentBean>) {
        binding.progressBar.visibility = View.GONE
        if (doWhat == DO_MERGE_PDF) {
            LogEx.logDebug(TAG, "refreshDocumentRv 1")
            documentList.map { it.uiType = 1 }
        }
        if (doWhat == DO_SPLIT_PDF) {
            LogEx.logDebug(TAG, "refreshDocumentRv 2")
            documentList.map { it.uiType = 2 }
        }
        if (doWhat == DO_LOCK_PDF || doWhat == DO_UNLOCK_PDF) {
            LogEx.logDebug(TAG, "refreshDocumentRv 3")
            documentList.map { it.uiType = 3 }
        }
        adapter.submitList(documentList)
    }

}