package com.grkj.iscs.view.presenter

import com.grkj.iscs.R
import com.grkj.iscs.model.vo.machinery.MachineryDetailRespVO
import com.grkj.iscs.model.vo.machinery.MachineryPageRespVO
import com.grkj.iscs.model.vo.ticket.LotoMapRespVO
import com.grkj.iscs.model.vo.ticket.StepDetailRespVO
import com.grkj.iscs.model.vo.ticket.TicketDetailRespVO
import com.grkj.iscs.util.Executor
import com.grkj.iscs.util.NetApi
import com.grkj.iscs.util.ToastUtils
import com.grkj.iscs.view.base.BasePresenter
import com.grkj.iscs.view.iview.IStepView

class StepPresenter : BasePresenter<IStepView>() {

    fun getStepDetail(ticketId: Long, callBack: (MutableList<StepDetailRespVO>?) -> Unit) {
        NetApi.getStepDetail(ticketId) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    fun getMachineryDetail(machineryId: Long, callBack: (MachineryDetailRespVO?) -> Unit, mapCallBack: (MutableList<LotoMapRespVO>?) -> Unit) {
        NetApi.getMachineryDetail(machineryId) {
            NetApi.getLotoMapData(it?.lotoId!!) {
                Executor.runOnMain {
                    mapCallBack(it?.sortedWith(compareBy({ it.row }, { it.col }))?.toMutableList())
                }
            }
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    fun cancelTicket(ticketId: Long, callBack: (Boolean) -> Unit) {
        NetApi.cancelTicket(ticketId) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    fun finishTicket(ticketId: Long, callBack: (Boolean) -> Unit) {
        NetApi.finishTicket(ticketId) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    /**
     * 获取作业票详细信息
     */
    fun getTicketDetail(ticketId: Long?, callBack: (TicketDetailRespVO?) -> Unit) {
        if (ticketId == null) {
            ToastUtils.tip(mContext!!.resources.getString(R.string.ticket_id_is_null))
            return
        }
        NetApi.getTicketDetail(ticketId) {
            Executor.runOnMain {
                callBack.invoke(it)
            }
        }
    }

    fun updateStep(stepId: Long, stepStatus: String, callBack: (Boolean) -> Unit) {
        NetApi.updateStep(stepId, stepStatus) {
            Executor.runOnMain {
                callBack.invoke(it)
            }
        }
    }
}