package com.example.chemical.receiver

import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.rc.core.log.RcLog
import com.rc.httpcore.vo.response.ChemicalBean

class PortScanService : Service(), OnSerialListener {

    companion object {
        const val ACTION_PORT_SCAN = "com.dlc.chemical.serial:PORT_SCAN"
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStart(intent: Intent?, startId: Int) {
        super.onStart(intent, startId)
        val path = intent!!.getStringExtra("path")
//        val baudRate = intent!!.getIntExtra("baudRate", 9600)
//        RcLog.info("==onStartCommand---------------------$path$baudRate")
//        when (intent.getIntExtra("type", -1)) {
//            0 -> {
//                mPortScanManager.connect(this, path, baudRate, null)
//            }
//            1 -> {
//                val chemicalBean = intent.getParcelableExtra("datePrint") as ChemicalBean
//                mPortScanManager.connect(this, path, baudRate, chemicalBean)
//            }
//            else -> {
//                //打印单一  二维码
//                val chemicalBean = intent.getStringExtra("datePrint") as String
//                mPortScanManager.connectTwo(this, path, baudRate, chemicalBean)
//            }
//        }

    }

    override fun onCreate() {
        super.onCreate()

    }

    private val mPortScanManager by lazy {
//        PortScanManager(this)
    }

//    override fun onDestroy() {
//        mPortScanManager.disconnect()
//        super.onDestroy()
//    }

    override fun onConnected(success: Boolean, message: String?) {
        RcLog.info("PortScanService#onConnected() success=${success}, message=${message}")
    }

    override fun onReceive(content: String) {
        // QRCode/IC 扫描返回
        RcLog.info("PortScanService#onReceive() QRCode/IC content=${content}")
        val intent = Intent().apply {
            putExtra("scan_content", content)
            action = ACTION_PORT_SCAN
        }
        sendBroadcast(intent)
    }

//    override fun onPrint(content: Int) {
//        // QRCode/IC 打印返回
//        RcLog.info("PRINT_CONTENT#打印返回  content=${content}")
//        val intent = Intent().apply {
//            putExtra("print_content", content)
//            action = PrinterStateReceiver.ACTION_PRINTER_STATE
//        }
//        sendBroadcast(intent)
//    }
}