package com.module.pay.strategy

import com.module.pay.serialport.SerialPortUtil
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.common.util.ByteUtils
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 单例
 */
object PaySendDataMDBStrategy : PaySendDataStrategy {
    private var serialPortUtil: SerialPortUtil? = null

    init {
        //工厂构建
        val createStrategy = SerialPortFactory().createStrategy(StrategyType.MDB)
        serialPortUtil = SerialPortUtil(createStrategy)
        serialPortUtil?.openSerialPort(
            "/dev/" + SpUtils.getString(MMKVName.TTYS_MDB, "ttyS5"),
            9600,
            0,
            0
        )
    }

    override fun billInit() {
        CoroutineScope(Dispatchers.IO).launch {
            squeezeMoney();
            delay(5000)
            serialPortUtil?.sendSerialPort("31")
        }

    }

    override fun billEnable(enableMoney: String, enableColl: String) {
        if (SpUtils.getBoolean(MMKVName.BILL_COLLOCATION,false)!!) {
            serialPortUtil?.sendSerialPort("34$enableMoney$enableColl")
        }else{
            serialPortUtil?.sendSerialPort("34$enableMoney"+"0000")
        }
    }

    private suspend fun initL2() {
        serialPortUtil?.sendSerialPort("10")//重置
        delay(500)
        serialPortUtil?.sendSerialPort("110002010101")//设置级别
        delay(500)
        serialPortUtil?.sendSerialPort("110100640000")//设置最大最小金额
        delay(500)
        serialPortUtil?.sendSerialPort("1401")//使能


    }

    private suspend fun initL3() {
//        serialPortUtil?.sendSerialPort("10")//重置
//        delay(500)
//        serialPortUtil?.sendSerialPort("110003000000")//设置级别
//        delay(500)
//        serialPortUtil?.sendSerialPort("110100640000")//设置最大最小金额
//        delay(500)
//        serialPortUtil?.sendSerialPort("170400000020")//L3 必须要发。
//        delay(500)
//        serialPortUtil?.sendSerialPort("1401")//使能

        serialPortUtil?.sendSerialPort("10")//重置
        delay(500)
        serialPortUtil?.sendSerialPort("12")//重置
        delay(500)
        serialPortUtil?.sendSerialPort("110003000000")//设置级别
        delay(500)
        serialPortUtil?.sendSerialPort("12")//重置
        delay(500)
        serialPortUtil?.sendSerialPort("110100640000")//设置最大最小金额
        delay(500)
        serialPortUtil?.sendSerialPort("17004350493132333435363738393041424142434445464748494A4B4C78D0")
        delay(500)
        serialPortUtil?.sendSerialPort("12")//重置
        delay(500)
        serialPortUtil?.sendSerialPort("170400000020")//L3 必须要发。
        delay(500)
        serialPortUtil?.sendSerialPort("12")//重置
        delay(500)
        serialPortUtil?.sendSerialPort("1401")//使能
    }

    override suspend fun initCreditCard() {
        if (SpUtils.getBoolean(MMKVName.MDB_LEVEL, true)!!) {
            // true L3
            initL3()
        } else {
            // true L2
            initL2()
        }
    }

    override fun squeezeMoney() {
        serialPortUtil?.sendSerialPort("3501")
    }

    override fun creditPoll() {
        serialPortUtil?.sendSerialPort("12")
    }

    override fun creditCardPay(price: Double, number: String) {
        serialPortUtil?.sendSerialPort(
            "1300" + ByteUtils.decimal2fitHex(
                (price * 100).toLong(),
                4
            ) + ByteUtils.decimal2fitHex(number.toLong(), 4)
        )

    }

    override fun creditCardCancel() {
        serialPortUtil?.sendSerialPort("1301")
    }

    override fun creditCardSessionComplete() {
        serialPortUtil?.sendSerialPort("1304")
    }

    override fun creditCardVendSuccess(number: String) {
        serialPortUtil?.sendSerialPort("1302" + ByteUtils.decimal2fitHex(number.toLong(), 4))

    }

    override fun creditCardCashSale(price: Double, number: String) {
        //是否需要发送 mdb cash sale
        if (SpUtils.getBoolean(MMKVName.MDB_CASH_SALE, true) == false) {
            return
        }
        serialPortUtil?.sendSerialPort(
            "1305" + ByteUtils.decimal2fitHex(
                (price * 100).toLong(),
                4
            ) + ByteUtils.decimal2fitHex(number.toLong(), 4)
        )

    }
}