package cn.muchinfo.rma.business.contract

import cn.muchinfo.rma.business.account.adapter.AccountAdapter
import cn.muchinfo.rma.business.contract.adapter.ContractAdapter
import cn.muchinfo.rma.global.data.ContractData
import cn.muchinfo.rma.netManage.base.ResponseCallback
import cn.muchinfo.rma.netManage.utils.MyOkHttpUtils
import cn.muchinfo.rma.netcore.packet.Packet50
import cn.muchinfo.rma.netcore.socket.Callback
import cn.muchinfo.rma.protobuf.funcode.FunCode
import cn.muchinfo.rma.protobuf.protoclasses.ErmcpMI1
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.autoWidget.guard
import cn.muchinfo.rma.view.base.app.BaseResult
import cn.muchinfo.rma.view.base.app.Constant
import com.blankj.utilcode.util.SPUtils
import okhttp3.Call
import java.lang.Exception

class ContractManager {

    /**
     * 合同列表
     * @param params Map<String, String>
     * @param responseBack Function3<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] List<ContractData>?, [@kotlin.ParameterName] Error?, Unit>
     */
    fun queryContract(
        params: Map<String, String>,
        responseBack: (isSuccess: Boolean, respData: List<ContractData>?, error: Error?) -> Unit
    ) {
        MyOkHttpUtils().query(
            URL = SPUtils.getInstance()
                .getString(Constant.goCommonSearchUrl) + "/Ermcp/QuerySpotContract",
            params = params,
            type = "1",
            callback = object : ResponseCallback<BaseResult<List<ContractData>>>() {
                override fun onResponse(response: BaseResult<List<ContractData>>?, id: Int) {
                    responseBack(true, response?.data, null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    responseBack(false, null, Error(e?.message))
                }
            }
        )
    }

    /**
     * 合同操作申请
     * @param contractInfo SpotContractInfo 参数
     * @param OperateType String 操作类型 操作类型-1:保存草稿2:提交申请3:审核通过4:审核拒绝5:撤回6:正常完结7:异常终止
     * @param Remark String 备注
     * @param SpotContractID String  现货合同ID(602+Unix秒时间戳(10位)+xxxxxx)（修改时传入）
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     */
    fun orderContract(
        contractInfo: SpotContractInfo,
        OperateType: String,
        Remark: String = "",
        SpotContractID: String = "",
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {

        val tradeSocketManager = MyApplication.getInstance()?.tradeSocketManager.guard {
            callback(false, Error("交易链路未初始化"))
            return
        }

        val reqPacket = ContractAdapter.getContractReqInfo(
            contractInfo = contractInfo,
            OperateType = OperateType,
            Remark = Remark,
            SpotContractID = SpotContractID
        )

        tradeSocketManager.send(
            reqPacket,
            FunCode.SpotContractOperateRsp,
            object : Callback<Packet50> {
                override fun onSuccess(rsp: Packet50?) {
                    val rst = ContractAdapter.analysisContractRsq(rsp!!)
                    callback(rst.first, rst.second)
                }

                override fun onFail(err: Error?) {
                    // 发送数据失败
                    callback(false, err)
                }

            }
        )
    }

    /**
     * 合同操作请求
     * @param info ContractOperateApplyInfo
     * @param operateType Int
     * @param remark String
     * @param operateApplyID Long
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     */
    fun orderPerationContract(
        info: ErmcpMI1.ContractOperateApplyInfo = ErmcpMI1.ContractOperateApplyInfo.newBuilder().build(),
        operateType: Int = 0,
        remark: String = "",
        operateApplyID: Long = 0,
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        val tradeSocketManager = MyApplication.getInstance()?.tradeSocketManager.guard {
            callback(false, Error("交易链路未初始化"))
            return
        }

        val reqPacket = ContractAdapter.getPerationContractReqInfo(
            info = info,
            operateType = operateType,
            remark = remark,
            operateApplyID = operateApplyID
        )

        tradeSocketManager.send(
            reqPacket,
            FunCode.ContractOperateApplyRsp,
            object : Callback<Packet50> {
                override fun onSuccess(rsp: Packet50?) {
                    val rst = ContractAdapter.analysisPerationContractRsq(rsp!!)
                    callback(rst.first, rst.second)
                }

                override fun onFail(err: Error?) {
                    // 发送数据失败
                    callback(false, err)
                }

            }
        )

    }


}