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

import android.content.Intent
import androidx.activity.addCallback
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.bean.ConstObject
import com.base.pdfviewerscannerwhite.bean.PdfPageBean
import com.base.pdfviewerscannerwhite.databinding.ActivityPdfSplitBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.helper.MyApplication
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentRenameDialog

class PdfSplitActivity : BaseActivity<ActivityPdfSplitBinding>(), PdfView {

    private lateinit var pdfPresenter: PdfPresenter
    private lateinit var pdfPagerAdapter: PdfPagerAdapter
    private var path: String = ""
    private var pwd: String? = ""

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

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

    override fun initView() {
        pdfPresenter = PdfPresenter(this, this)
        path = intent.extras?.getString("path", "") ?: ""
        pwd = intent.extras?.getString("pwd", "") ?: ""
        pdfPresenter.password = pwd

        initAdapter()

        pdfPresenter.iniPdfPage(path)
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.ivSelector.setOnClickListener {
            binding.ivSelector.isSelected = !binding.ivSelector.isSelected
            pdfPagerAdapter.toggleSelect(binding.ivSelector.isSelected)
        }
        binding.tvBtnSplit.setOnClickListener {
            val splitIndex = pdfPagerAdapter.items.filter { it.isSelect }.map { it.pageIndex }
            showDocumentRenameDialog(okAction = { newName ->
                startActivity(Intent(this, PdfLoadingActivity::class.java).apply {
                    putExtra("doWhat", ConstObject.DO_SPLIT_PDF)
                    putExtra("srcPath", path)
                    putExtra("pwd", pwd)
                    putExtra("newPath", pdfPresenter.createNewPdfPath(newName))
                    putExtra("splitIndex", splitIndex.joinToString(separator = ","))
                })
                finish()
            })
        }
    }

    private fun initAdapter() {
        pdfPagerAdapter = PdfPagerAdapter(path, null, R.layout.item_pdf_pager_split)
        pdfPagerAdapter.mPassword = pwd
        pdfPagerAdapter.selectAction = { enable, allSelect ->
            binding.tvBtnSplit.isEnabled = enable
            binding.ivSelector.isSelected = allSelect
        }
        binding.rv.adapter = pdfPagerAdapter

    }

    override fun initPdfPageRv(items: List<PdfPageBean>) {
        pdfPagerAdapter.submitList(items)
    }

}