package com.base.scanqrclear.ui.scan

import androidx.activity.addCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.updatePadding
import com.base.scanqrclear.BuildConfig
import com.base.scanqrclear.R
import com.base.scanqrclear.ads.AdsMgr
import com.base.scanqrclear.base.BaseActivity
import com.base.scanqrclear.bean.FunctionUIBean
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_BANKNOTE
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_BUSINESS_CARD
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_COIN
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_DOCUMENT
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_FOOD
import com.base.scanqrclear.databinding.ActivityScanOtherBinding
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.utils.LogEx
import com.base.scanqrclear.utils.ToastUtils.toast
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions.RESULT_FORMAT_JPEG
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions.RESULT_FORMAT_PDF
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions.SCANNER_MODE_FULL
import com.google.mlkit.vision.documentscanner.GmsDocumentScanning
import com.google.mlkit.vision.documentscanner.GmsDocumentScanningResult
import com.gyf.immersionbar.ktx.immersionBar

class ScanOtherActivity : BaseActivity<ActivityScanOtherBinding>(ActivityScanOtherBinding::inflate) {

    private var scannerLauncher: ActivityResultLauncher<IntentSenderRequest>? = null

    override fun initView() {
        super.initView()
        binding.clTop.updatePadding(top = BarUtils.getStatusBarHeight())

        initScannerLauncher()

        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_app_exit)


        val adapter = ScanOtherAdapter()
        binding.rv.adapter = adapter

        adapter.submitList(
            listOf(
                FunctionUIBean(KEY_FOOD, R.mipmap.foodscan, getString(R.string.food_scan)),
                FunctionUIBean(KEY_COIN, R.mipmap.coinscan, getString(R.string.coin_scan)),
                FunctionUIBean(KEY_BANKNOTE, R.mipmap.banknote, getString(R.string.banknote_scan)),
                FunctionUIBean(KEY_DOCUMENT, R.mipmap.document, getString(R.string.document_scan)),
                FunctionUIBean(KEY_BUSINESS_CARD, R.mipmap.business, getString(R.string.business_card))
            ),
        )
        adapter.itemClick = { item ->
            when (item.key) {
                KEY_DOCUMENT -> {
                    val options = GmsDocumentScannerOptions.Builder()
                        .setGalleryImportAllowed(false)
                        .setPageLimit(2)
                        .setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
                        .setScannerMode(SCANNER_MODE_FULL)
                        .build()
                    val scanner = GmsDocumentScanning.getClient(options)

                    scanner.getStartScanIntent(this)
                        .addOnSuccessListener { intentSender ->
                            scannerLauncher?.launch(IntentSenderRequest.Builder(intentSender).build())
                        }
                        .addOnFailureListener {
                            toast("Don't support GmsDocumentScanner", true)
                        }
                }
            }
        }
    }

    private fun initScannerLauncher() {
        scannerLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val data = GmsDocumentScanningResult.fromActivityResultIntent(result.data)
                data?.pages?.forEach { page ->
                    val imageUri = page.imageUri
                    LogEx.logDebug(TAG, "imageUri=$imageUri")
                    if (BuildConfig.DEBUG) {
                        binding.tvTest.append("imageUri=$imageUri\n")
                    }
                }
                val pdfUri = data?.pdf?.uri
                val pdfPageCount = data?.pdf?.pageCount
                LogEx.logDebug(TAG, "pdfUri=$pdfUri")
                binding.tvTest.append("pdfUri=$pdfUri\n")
                LogEx.logDebug(TAG, "pdfPageCount=$pdfPageCount")
                binding.tvTest.append("pdfPageCount=$pdfPageCount")
            }
        }
    }

    override fun initListener() {
        super.initListener()

        onBackPressedDispatcher.addCallback {
            finishToMainTop()
        }

        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

    }

    override fun configSystemBar() {
        immersionBar {
            statusBarColor("#FFFFFFFF")
            statusBarDarkFont(false)
        }
    }
}