package cn.muchinfo.rma.business.quote.models

import java.nio.charset.Charset

/**
 * 行情订阅商品信息模型类
 * @property subState Int 订阅成功标志；1为成功，0为失败，订阅的时候填0
 * @property exchangeCode Int 交易所代码, 目前2.0固定为250
 * @property goodsCode String 商品代码，64 Byte，不足位使用'\0'补齐
 * @constructor
 */
class MoQuoteSubscriptGoodsInfo(
    var subState: Int = 0,
    var exchangeCode: Int = 250,
    var goodsCode: String = ""
) {
    constructor(data: ByteArray) : this() {
        if (data.size != 66) {
            throw Exception("错误的数据长度")
        }

        subState = data[0].toInt()
        exchangeCode = data[1].toInt()

        goodsCode = String(data.sliceArray(2 until 66), Charsets.US_ASCII).replace("""\0""", "")
    }

    /**
     * 获取行情订阅商品信息对象二进制流
     * @return ByteArray 行情订阅商品信息对象二进制流
     */
    fun getData(): ByteArray {
        var data = byteArrayOf(subState.toByte(), exchangeCode.toByte())

        // 商品代码需要64 Byte，不足补'\0'
        val tmp = goodsCode.toByteArray()
        data += tmp
        val size = 64 - tmp.size
        for (i in 1..size) {
            data += 0
        }

        return data
    }
}