package com.base.pdfreaderallpdfreader.ui.pdf

import android.content.Intent
import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.base.BaseActivity
import com.base.pdfreaderallpdfreader.bean.ConstObject
import com.base.pdfreaderallpdfreader.databinding.ActivityPdfSplitBinding
import com.base.pdfreaderallpdfreader.ui.view.NameDialog.showDocumentRenameDialog
import com.base.pdfreaderallpdfreader.utils.BarUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class PdfSplitActivity : BaseActivity<ActivityPdfSplitBinding>() {

    private lateinit var pdfViewModel: PdfViewModel
    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() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        pdfViewModel = ViewModelProvider(this)[PdfViewModel::class.java]
        path = intent.extras?.getString("path", "") ?: ""
        pwd = intent.extras?.getString("pwd", "") ?: ""
        pdfViewModel.password = pwd

        initAdapter()
        pdfViewModel.initPdfPageRv = { items ->
            lifecycleScope.launch(Dispatchers.Main) {
                pdfPagerAdapter.submitList(items)
            }
        }
        pdfViewModel.iniPdfPage(this, 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", pdfViewModel.createNewPdfPath(this@PdfSplitActivity, 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

    }


}