package cn.muchinfo.rma.business.future.adapter

import cn.muchinfo.rma.business.global.MessageHeadModel
import cn.muchinfo.rma.global.ErrorMessageUtils
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.netcore.packet.Packet50
import cn.muchinfo.rma.protobuf.funcode.FunCode
import cn.muchinfo.rma.protobuf.protoclasses.MSIM
import com.blankj.utilcode.util.TimeUtils
import java.io.ByteArrayOutputStream
import java.lang.Exception
import java.util.*

/**
 * 交易委托装解箱
 */
object FutureAdapter {

    /**
     * 交易委托的报文装箱
     * @param data ChannelOrderReqData
     * @return Packet50
     */
    fun getChannelOrderReqInfo(data: ChannelOrderReqData): Packet50 {
        val builder: MSIM.ChannelOrderReq.Builder = MSIM.ChannelOrderReq.newBuilder()
        builder.accountID = data.accountID
        builder.buyOrSell = data.buyOrSell
        builder.channelBuildType = data.channelBuildType
        builder.channelOperateType = data.channelOperateType
        builder.channelOrderSrc = data.channelOrderSrc
        builder.clientOrderTime = data.clientOrderTime
        builder.clientSerialNo = UUID.randomUUID().toString().replace("-", "")
        builder.clientType = data.clientType
        builder.closeType = data.closeType
        builder.goodsID = data.goodsID
        builder.loginID = data.loginID
        builder.marketID = data.marketID
        builder.operatorID = data.operatorID
        builder.orderPrice = data.orderPrice
        builder.hedgeFlag = data.hedgeFlag
        builder.orderQty = data.orderQty
        builder.priceMode = data.priceMode
        builder.relatedID = data.relatedID
        builder.serviceTime = data.serviceTime
        builder.validType = data.validType
        builder.validtime = data.validtime
//        builder.setIP(data.ip)
//        builder.closeTodayQty = data.closeTodayQty
        builder.timeValidType = data.timeValidType
        val loginInfo = GlobalDataCollection.instance?.loginRsp!!
        // FIXME: - 250000000005
        builder.setHeader(
            MessageHeadModel.getHead(
                FunCode.ChannelOrderReq,
                loginInfo.userID,
                GlobalDataCollection.instance?.accountId ?: 0,
                data.goodsID,
                data.marketID
            )
        )

        val arrayOutputStream = ByteArrayOutputStream();
        builder.build().writeTo(arrayOutputStream)
        return Packet50(FunCode.ChannelOrderReq, arrayOutputStream.toByteArray())
    }

    /**
     * 交易委托的报文解析
     * @param packet50 Packet50
     * @return Triple<Boolean, Error?, MSIM.ChannelOrderRsp?>
     */
    fun analysisChannelOrderRsq(packet50: Packet50): Triple<Boolean, Error?, MSIM.ChannelOrderRsp?> {
        return try {
            val loginRsp = MSIM.ChannelOrderRsp.parseFrom(packet50.content)
            if (loginRsp.retCode == 0) {
                // 操作成功
                Triple(true, null, loginRsp)
            } else {
                // 操作失败
                Triple(false, Error(ErrorMessageUtils.getErrorString(loginRsp.retCode)), null)
            }
        } catch (e: Exception) {
            // 操作失败
            Triple(false, Error("装箱失败"), null)
        }
    }


    /**
     * 委托撤单的报文装箱
     */
    fun getCancelOrderReqInfo(orderId : Long,marketId : Int ,accountId : Long,goodsId : Int): Packet50 {
        val builder: MSIM.CancelOrderReq.Builder = MSIM.CancelOrderReq.newBuilder()

        builder.setAccountID(accountId)
        builder.setOldOrderId(orderId)
        builder.setClientType(3)
        builder.clientSerialNo = UUID.randomUUID().toString().replace("-", "")//撤单时传的唯一标识
        builder.clientOrderTime = TimeUtils.getNowString()
        builder.setOrderSrc(1)

        val loginInfo = GlobalDataCollection.instance?.loginRsp!!
        builder.setOperatorID(loginInfo.userID.toLong())
        // FIXME: - 250000000005
        builder.setHeader(
            MessageHeadModel.getHead(
                FunCode.CANCELORDERREQ,
                loginInfo.userID,
                GlobalDataCollection.instance?.accountId ?: 0,
                goodsId,
                marketId
            )
        )

        val arrayOutputStream = ByteArrayOutputStream();
        builder.build().writeTo(arrayOutputStream)
        return Packet50(FunCode.ChannelOrderReq, arrayOutputStream.toByteArray())
    }

    /**
     * 委托撤单报文解析
     * @param packet50 Packet50
     * @return Triple<Boolean, Error?, MSIM.CancelOrderRsp?>
     */
    fun analysisCancelOrderRsq(packet50: Packet50) : Triple<Boolean, Error?, MSIM.CancelOrderRsp?>{
        return try {
            val loginRsp = MSIM.CancelOrderRsp.parseFrom(packet50.content)
            if (loginRsp.retCode == 0) {
                // 操作成功
                Triple(true, null, loginRsp)
            } else {
                // 操作失败
                Triple(false, Error(ErrorMessageUtils.getErrorString(loginRsp.retCode)), null)
            }
        } catch (e: Exception) {
            // 操作失败
            Triple(false, Error("装箱失败"), null)
        }
    }

}