package com.base.pdfreaderallpdfreader.ui.pdf

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.activity.addCallback
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.ads.AdmobHelper
import com.base.pdfreaderallpdfreader.ads.admob.AdmobInterstitialUtils
import com.base.pdfreaderallpdfreader.base.BaseActivity
import com.base.pdfreaderallpdfreader.bean.ConstObject.DO_LOCK_PDF
import com.base.pdfreaderallpdfreader.bean.ConstObject.DO_MERGE_PDF
import com.base.pdfreaderallpdfreader.bean.ConstObject.DO_SPLIT_PDF
import com.base.pdfreaderallpdfreader.bean.ConstObject.DO_UNLOCK_PDF
import com.base.pdfreaderallpdfreader.bean.DocumentBean
import com.base.pdfreaderallpdfreader.databinding.ActivityPdfSelectBinding
import com.base.pdfreaderallpdfreader.ui.main.getPdfDocument
import com.base.pdfreaderallpdfreader.ui.view.PwdDialog.showPdfPwdDialog
import com.base.pdfreaderallpdfreader.utils.BarUtils
import com.base.pdfreaderallpdfreader.utils.LogEx
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class PdfSelectActivity : BaseActivity<ActivityPdfSelectBinding>() {

    private val TAG = "PdfSelectActivity"
    private lateinit var pdfViewModel: PdfViewModel

    private lateinit var adapter: DocumentPdfAdapter

    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() {

//        if (checkNeedRecreate(MyApplication.pdfSelectLanguage)) {
//            return
//        }

        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        pdfViewModel = ViewModelProvider(this)[PdfViewModel::class.java]

        doWhat = intent.extras?.getString("doWhat", "") ?: ""
        LogEx.logDebug(TAG, "doWhat=$doWhat")


        initAdapter()

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

            }

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

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

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

        }

    }

    private fun getPdfData(uiType: Int) {
        lifecycleScope.launch(Dispatchers.IO) {
            val list = getPdfDocument(this@PdfSelectActivity)
            list.forEach { it.uiType = uiType }
            launch(Dispatchers.Main) {
                refreshUI(list)
            }
        }
    }

    private fun refreshUI(list: List<DocumentBean>) {
        adapter.submitList(list)
        binding.llEmpty.isVisible = list.isEmpty()
        binding.progressBar.visibility = View.GONE
    }

    override fun initListener() {
        super.initListener()

//        if (checkNeedRecreate(MyApplication.pdfSelectLanguage)) {
//            return
//        }

        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 = DocumentPdfAdapter()
        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)
                            binding.llEmpty.isVisible = adapter.items.isEmpty()
                        }
                    } else {
                        adapter.remove(it)
                        binding.llEmpty.isVisible = adapter.items.isEmpty()
                    }
                })
            }
            if (doWhat == DO_UNLOCK_PDF) {
                showPdfPwdDialog(state = it.state, path = it.path, encryptionAction = {
                    if (AdmobHelper.canCommonShowAd()) {
                        AdmobInterstitialUtils.showInterstitialAd(this) { flag ->
                            adapter.remove(it)
                            binding.llEmpty.isVisible = adapter.items.isEmpty()
                        }
                    } else {
                        adapter.remove(it)
                        binding.llEmpty.isVisible = adapter.items.isEmpty()
                    }
                })
            }
        }
        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
    }


}