package com.lukouguoji.module_base.model

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.widget.Toast
import com.lukouguoji.module_base.ktx.showToast
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import com.yzq.zxinglibrary.android.CaptureActivity
import com.yzq.zxinglibrary.bean.ZxingConfig
import com.yzq.zxinglibrary.common.Constant
import dev.utils.app.AppUtils
import dev.utils.app.IntentUtils

object ScanModel {

    /**
     * 打开扫描页面
     */
    fun startScan(activity: Activity, requestCode: Int) {
        AndPermission.with(activity)
            .runtime()
            .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE)
            .onGranted { data: List<String?>? ->
                val intent = Intent(activity, CaptureActivity::class.java)
                val config = ZxingConfig()
                config.isFullScreenScan = false //是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                intent.putExtra(Constant.INTENT_ZXING_CONFIG, config)
                activity.startActivityForResult(intent, requestCode)
            }
            .onDenied {
                val intent = IntentUtils.getLaunchAppDetailsSettingsIntent(
                    AppUtils.getPackageName(), true
                )
                activity.startActivity(intent)
                showToast("没有权限无法扫描呦")
            }
            .start()
    }

}