package com.sixin.module_p.service

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.ktx.loge
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.toJson

/**
 * 条码通知
 * 广播接收后模拟回车输出
 *
 * @author h6900
 */
class ScanCodeBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        loge("监听到信息了")
        intent.extras?.let {
            val keySet = it.keySet()
            val map = hashMapOf<String, Any?>()
            for (key in keySet) {
                map[key] = it.get(key)
            }
            loge("获取扫描广播信息 :\n${map.toJson()}")
        }
        val code = intent.getStringExtra(EXTRA_BARCODE_STRING).noNull().trim()
        if (code.isEmpty()) {
            return
        }
        FlowBus.with<String>(ConstantEvent.SCAN_CODE_RESULT)
            .tryEmit(code)
    }



    companion object {

        // 扫描广播
        const val ACTION: String = "android.intent.ACTION_DECODE_DATA"

        // 扫描结果 (barcode)
        const val EXTRA_BARCODE_STRING: String = "barcode_string"

    }
}