package zs.qimai.com.printer2.manager

import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import zs.qimai.com.printer2.callback.OnConnectCallBack
import zs.qimai.com.printer2.printStatus.PrintStatusCallBack
import zs.qimai.com.printer2.printStatus.PrinterStatusUtils
import java.io.IOException
import java.net.SocketException
import java.util.*

/**
 * <pre>
 *     author : leiwang
 *     e-mail : xxx@xx
 *     time   : 2021/03/18
 *     desc   : ip地址连接
 *     version: 1.0
 * </pre>
 */
class WifiDeviceManager(var ip: String, var port: Int = 9100) : DeviceManager() {
    override var mType: Int = WIFI
    private var len = 0
    lateinit var mPort: EthernetPort
    var mConnectCallBack: OnConnectCallBack? = null
    override fun openPort() {
        GlobalScope.launch(Dispatchers.IO) {
            withContext(Dispatchers.Main) {
                mConnectCallBack?.onConnectStart()
            }
            mPort = EthernetPort(ip, port)
            val isOpenPort = mPort.openPort()
            if (isOpenPort) {
                mOutPutStream = mPort.outputStream
                mInPutStream = mPort.inputStream
                this@WifiDeviceManager.deviceId = ip
                //检查打印机状态
                PrinterStatusUtils(this@WifiDeviceManager).apply {
                    mPrintStatusCallBack = object : PrintStatusCallBack {
                        override fun searchResult(status: Int?) {
                            mPrintMode = status ?: ESC
                            mStatus = true
                            name = deviceId
                            if (name.isNullOrEmpty()) {
                                name = if (mPrintMode == ESC) "小票打印机" else "杯贴打印机"
                            }
                            DeviceManagerUtils.getInstance().addDevice(this@WifiDeviceManager)
                            Log.d(TAG, "searchResult: WifiDeviceManager success")

                            mConnectCallBack?.onConnectSuccess(this@WifiDeviceManager)
                        }
                    }
                }.queryStatus()
            } else {
                withContext(Dispatchers.Main) {
                    mConnectCallBack?.onConnectError(0, "连接失败")
                }
            }
        }


    }

    override fun closePort() {
        try {
            mPort.closePort()
        } catch (e: IOException) {

        }
    }

    override fun readData(bytes: ByteArray): Int {
        var data: Int
        try {
            data = mPort.readData(bytes)
        } catch (e: IOException) {
            Log.d(TAG, "readData: e= $e")
            data = -1

        }
        return data
    }

    override fun writeData(bytes: ByteArray) {
        try {
            mOutPutStream?.write(bytes)
        } catch (e: IOException) {
            Log.d(TAG, "writeData: e=$e")
        } catch (e: SocketException) {
            Log.d(TAG, "writeData: e = $e")
        }
    }

    companion object {
        private const val TAG = "WifiDeviceManager"
    }
}