package cn.muchinfo.rma.business.hedge.adapter

import cn.muchinfo.rma.business.global.MessageHeadModel
import cn.muchinfo.rma.business.hedge.HedgePlanInfo
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.ErmcpMI1
import java.io.ByteArrayOutputStream
import java.lang.Exception
import java.util.*

/**
 * 套保计划相关装解箱
 */
object HedgeAdapter {

    /**
     * 获取新套保计划报文
     * @param hedgePlanData HedgePlanData
     * @param hedgePlanID Long
     * @param operateType Int
     * @param remark String
     * @return Packet50
     */
    fun getHedgeReqInfo(
        hedgePlanData: HedgePlanInfo = HedgePlanInfo(),
        hedgePlanID : Long = 0,
        operateType : Int = 0,
        remark : String = ""
    ): Packet50 {
        val builder = ErmcpMI1.HedgePlanOperateReq.newBuilder()
        val hedgePlanInfoOrBuilder = ErmcpMI1.HedgePlanInfo.newBuilder()
        hedgePlanData.let {
            hedgePlanInfoOrBuilder.areaUserID = hedgePlanData.AreaUserID
            hedgePlanInfoOrBuilder.contractType = hedgePlanData.ContractType
            hedgePlanInfoOrBuilder.hedgePlanNo = hedgePlanData.HedgePlanNo
            hedgePlanInfoOrBuilder.deliveryGoodsID = hedgePlanData.DeliveryGoodsID
            hedgePlanInfoOrBuilder.wrStandardID = hedgePlanData.WrStandardID
            hedgePlanInfoOrBuilder.productType = hedgePlanData.ProductType
            hedgePlanInfoOrBuilder.spotGoodsDesc = hedgePlanData.SpotGoodsDesc
            hedgePlanInfoOrBuilder.convertFactor = hedgePlanData.ConvertFactor
            hedgePlanInfoOrBuilder.planTime = hedgePlanData.PlanTime
            hedgePlanInfoOrBuilder.planQty = hedgePlanData.PlanQty
        }
        val loginInfo = GlobalDataCollection.instance?.loginRsp
        // FIXME: - 250000000005
        builder.setHeader(
            MessageHeadModel.getHead(
                FunCode.SpotContractOperateReq,
                loginInfo?.userID ?: 0,
                GlobalDataCollection.instance?.accountId ?: 0,
                0,
                18
            )
        )

        builder.setClientTicket(UUID.randomUUID().toString().replace("-", ""))
        if (operateType == 1 || operateType == 2){
            builder.info = hedgePlanInfoOrBuilder.build()
        }
        builder.userID = loginInfo?.loginID ?: 0
        builder.remark = remark
        builder.operateType = operateType
        builder.operateSrc = 2
        if(hedgePlanID != 0L){
            builder.hedgePlanID = hedgePlanID
        }
        val arrayOutputStream = ByteArrayOutputStream();
        builder.build().writeTo(arrayOutputStream)
        return Packet50(FunCode.HedgePlanOperateReq, arrayOutputStream.toByteArray())
    }


    /**
     * 套保计划报文解析
     * @param packet50 Packet50
     * @return Triple<Boolean, Error?, ErmcpMI1.SpotContractOperateRsp?>
     */
    fun analysisHedgeRsq(packet50: Packet50): Triple<Boolean, Error?, ErmcpMI1.HedgePlanOperateRsp?> {
        return try {
            val loginRsp = ErmcpMI1.HedgePlanOperateRsp.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)
        }
    }
}