package com.grkj.iscs.view.presenter

import com.grkj.iscs.R
import com.grkj.iscs.view.base.BasePresenter
import com.grkj.iscs.view.iview.ICreateTicketView
import com.grkj.iscs.model.Constants.USER_ROLE_COLOCKER
import com.grkj.iscs.model.Constants.USER_ROLE_LOCKER
import com.grkj.iscs.model.UrlConsts
import com.grkj.iscs.model.vo.ticket.CreateTicketReqVO
import com.grkj.iscs.model.vo.sop.SopPageRespVO
import com.grkj.iscs.model.vo.ticket.TicketTypeRespVO
import com.grkj.iscs.util.Executor
import com.grkj.iscs.util.NetApi
import com.grkj.iscs.util.ToastUtils
import com.grkj.iscs.util.log.LogUtil

class CreateTicketPresenter : BasePresenter<ICreateTicketView>() {

    var mSopList = mutableListOf<SopPageRespVO.Record>()
    var mTicketTypeList = mutableListOf<TicketTypeRespVO>()

    fun initData() {
        NetApi.getTicketType {
            mTicketTypeList = it as MutableList<TicketTypeRespVO>
        }

//        NetApi.getSopPage(0, 10) {
//            Executor.runOnMain {
//                mSopList = it?.records as MutableList<SopPageRespVO.Record>
//                mvpView?.showSopList(mSopList)
//            }
//        }

        NetApi.getDeptList(0, 10) {
            LogUtil.i("getDeptList : $it")
        }

        NetApi.getRoleList(0, 10, USER_ROLE_LOCKER) {
            LogUtil.i("Locker getRoleList : $it")
            mvpView?.updateLockerList(it?.rows?.map { it.nickName!! })
        }
        NetApi.getRoleList(0, 10, USER_ROLE_COLOCKER) {
            LogUtil.i("Colocker getRoleList : $it")
        }
    }

    fun getAutoCode(callBack: (String?) -> Unit) {
        NetApi.getAutoCode(UrlConsts.AUTOCODE_TICKET_NUMBER) {
            callBack.invoke(it)
        }
    }

    fun getSopInfo(sopId: Long, callBack: (List<String?>?) -> Unit) {
        NetApi.getSopInfo(sopId) {
            LogUtil.i("getSopInfo : $it")
            callBack.invoke(it?.pointDetailVOList?.map { it.pointName })
        }
    }

    fun getTicketTypeName(type: String): String? {
        return mTicketTypeList.find { it.dictValue == type }?.dictLabel
    }

    fun createTicket(ticketVO: CreateTicketReqVO) {
        if (ticketVO.ticketCode.isNullOrEmpty()) {
            ToastUtils.tip(mContext?.resources?.getString(R.string.ticket_code_is_null))
            return
        }
        if (ticketVO.ticketName.isNullOrEmpty()) {
            ToastUtils.tip(mContext?.resources?.getString(R.string.ticket_name_is_null))
            return
        }
        if (ticketVO.workshopId == null) {
            ToastUtils.tip(mContext?.resources?.getString(R.string.work_shop_is_null))
            return
        }
        if (ticketVO.workareaId == null) {
            ToastUtils.tip(mContext?.resources?.getString(R.string.work_area_is_null))
            return
        }
        if (ticketVO.sopId == null) {
            ToastUtils.tip(mContext?.resources?.getString(R.string.sop_is_null))
            return
        }
        if (ticketVO.ticketType.isNullOrEmpty()) {
            ToastUtils.tip(mContext?.resources?.getString(R.string.ticket_type_is_null))
            return
        }

    }
}