package com.base.scanqrclear.ui.main

import androidx.activity.addCallback
import com.base.scanqrclear.ads.AdsMgr
import com.base.scanqrclear.ads.AdsShowCallBack
import com.base.scanqrclear.base.BaseActivity
import com.base.scanqrclear.bean.config.AdConfigBean
import com.base.scanqrclear.databinding.ActivityScanBinding
import com.base.scanqrclear.qr.CameraUtils
import com.base.scanqrclear.utils.PermissionUtils.checkCameraPermission

/**
 *Create by SleepDog on 2025-01-20
 */
class ScanAc : BaseActivity<ActivityScanBinding>(ActivityScanBinding::inflate) {

    private var isPopupIn = false//通知进来的

    var cameraUtils: CameraUtils? = null
    override fun initView() {
        super.initView()
        cameraUtils = CameraUtils(this)
        isPopupIn = intent.extras?.getBoolean("isPopupIn", false) ?: false
    }

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        if (AdConfigBean.adsConfigBean.functionInShowAd) {
            if (isPopupIn) return
            if (!checkCameraPermission()) return
            AdsMgr.showInsert(this, false, object : AdsShowCallBack() {
                override fun close(where: Int) {
                }

                override fun failed(where: Int) {
                }

                override fun googleFailed(where: Int) {
                }
            })
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            if (AdConfigBean.adsConfigBean.functionBackShowAd) {
                AdsMgr.showInsert(this@ScanAc, false, object : AdsShowCallBack() {
                    override fun close(where: Int) {
                        finishToMainTop()
                    }

                    override fun failed(where: Int) {
                        finishToMainTop()
                    }

                    override fun googleFailed(where: Int) {
                        finishToMainTop()
                    }
                })
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        cameraUtils?.unBind()
        cameraUtils = null
    }
}