package cn.muchinfo.rma.business.hedge

import cn.muchinfo.rma.business.contract.adapter.ContractAdapter
import cn.muchinfo.rma.business.hedge.adapter.HedgeAdapter
import cn.muchinfo.rma.global.data.ContractData
import cn.muchinfo.rma.global.data.HedgePlanData
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.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 org.jetbrains.annotations.Contract
import java.lang.Exception

/**
 * 套保计划管理类
 */
class HedgeManager {

    /**
     * 套保计划查询
     * @param params Map<String, String>
     * @param responseBack Function3<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] List<ContractData>?, [@kotlin.ParameterName] Error?, Unit>
     */
    fun queryHedgeList(
        params: Map<String, String>,
        responseBack: (isSuccess: Boolean, respData: List<HedgePlanData>?, error: Error?) -> Unit
    ) {
        MyOkHttpUtils().query(
            URL = SPUtils.getInstance()
                .getString(Constant.goCommonSearchUrl) + "/Ermcp/QueryHedgePlan",
            params = params,
            type = "1",
            callback = object : ResponseCallback<BaseResult<List<HedgePlanData>>>() {
                override fun onResponse(response: BaseResult<List<HedgePlanData>>?, id: Int) {
                    responseBack(true, response?.data, null)
                }

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

            }
        )
    }

    /**
     * 套保计划操作请求
     * @param hedgePlanData HedgePlanInfo 套保计划信息资料
     * @param hedgePlanID Long 套保计划id
     * @param operateType Int 操作类型-1:保存草稿2:提交申请3:审核通过4:审核拒绝5:撤回
     * @param remark String 备注
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     */
    fun orderHedge(
        hedgePlanData: HedgePlanInfo = HedgePlanInfo(),
        hedgePlanID: Long = 0,
        operateType: Int = 0,
        remark: String = "",
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        val tradeSocketManager = MyApplication.getInstance()?.tradeSocketManager.guard {
            callback(false, Error("交易链路未初始化"))
            return
        }

        val reqPacket = HedgeAdapter.getHedgeReqInfo(
            hedgePlanData = hedgePlanData,
            hedgePlanID = hedgePlanID,
            operateType = operateType,
            remark = remark
        )

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

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

            }
        )

    }

}