package com.grkj.iscs.view.presenter

import com.grkj.iscs.R
import com.grkj.iscs.model.Constants.USER_ROLE_LOCKER
import com.grkj.iscs.model.vo.ticket.StepDetailRespVO
import com.grkj.iscs.model.vo.ticket.TicketDetailRespVO
import com.grkj.iscs.model.vo.ticket.TicketUserReqVO
import com.grkj.iscs.model.vo.user.UserListRespVO
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.IWorkerView

class WorkerPresenter : BasePresenter<IWorkerView>() {
    /**
     * 获取作业票详细信息
     */
    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)
            }
        }
    }

    /**
     * @param unitId 9：玛氏内部
     * @param roleId 3：上锁人  4：共锁人
     */
    fun getUserList(
        workstationId: Long? = null,
        roleId: Long? = null,
        unitId: Long? = null,
        callBack: (UserListRespVO?) -> Unit
    ) {
        NetApi.getUserList(1, 1000, workstationId, roleId, unitId) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    fun updateTicketUser(
        ticketId: Long,
        userList: MutableList<TicketUserReqVO>,
        callBack: (Boolean) -> Unit
    ) {
        if (userList.none { it.userRole == USER_ROLE_LOCKER }) {
            ToastUtils.tip(R.string.please_select_locker)
            return
        }
        NetApi.updateTicketUser(ticketId, userList) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

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