package com.grkj.iscs_mc.view.presenter

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.grkj.iscs_mc.extentions.serialNo
import com.grkj.iscs_mc.model.vo.inspection.InsertCheckRecordReqVO
import com.grkj.iscs_mc.model.vo.inspection.InspectionCheckTableDataRespVO
import com.grkj.iscs_mc.util.Executor
import com.grkj.iscs_mc.util.NetApi
import com.grkj.iscs_mc.util.log.LogUtil
import com.grkj.iscs_mc.view.base.BasePresenter
import com.grkj.iscs_mc.view.iview.IMaterialInspectionTableView

class MaterialInspectionTablePresenter : BasePresenter<IMaterialInspectionTableView>() {

    fun getCheckTableData(planId: Long, callBack: (InspectionCheckTableDataRespVO?) -> Unit) {
        NetApi.getCheckTableData(planId, mContext!!.serialNo()) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    fun insertCheckRecord(list: MutableList<InspectionCheckTableDataRespVO.IsMaterialsCheckRecord>, callBack: (Boolean) -> Unit) {
        if (list.isEmpty()) {
            LogUtil.w("No check record to save")
            return
        }
        val saveList: MutableList<InsertCheckRecordReqVO> =
            Gson().fromJson(
                Gson().toJson(list),
                object : TypeToken<MutableList<InspectionCheckTableDataRespVO>>() {}.type
            )
        NetApi.insertCheckRecord(saveList) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }
}