package com.grkj.iscs_mc.view.presenter

import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.extentions.serialNo
import com.grkj.iscs_mc.model.vo.material.MaterialManualReplacementReqVO
import com.grkj.iscs_mc.model.vo.material.MaterialPropertyValuePageRespVO
import com.grkj.iscs_mc.model.vo.material.MaterialTypePageRespVO
import com.grkj.iscs_mc.model.vo.replacement.ManualReplacementListRespVO
import com.grkj.iscs_mc.util.Executor
import com.grkj.iscs_mc.util.NetApi
import com.grkj.iscs_mc.util.ToastUtils
import com.grkj.iscs_mc.view.base.BasePresenter
import com.grkj.iscs_mc.view.iview.IMaterialManualReplacementView

class MaterialManualReplacementPresenter : BasePresenter<IMaterialManualReplacementView>() {

    fun getManualReplacementList(callBack: (MutableList<ManualReplacementListRespVO>?) -> Unit) {
        NetApi.getManualReplacementList(mContext!!.serialNo()) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    fun getMaterialType(callBack: (MaterialTypePageRespVO?) -> Unit) {
        NetApi.getMaterialTypePage(1, -1) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    fun getMaterialProperty(callBack: (MutableList<MaterialPropertyValuePageRespVO.Record>?) -> Unit) {
        NetApi.getMaterialPropertyValuePage(1, -1) {
            Executor.runOnMain {
                callBack(it?.records)
            }
        }
    }

    fun checkData(vo: ManualReplacementListRespVO.MaterialsPageVO): Boolean {
        if (vo.newName.isNullOrEmpty()) {
            ToastUtils.tip(R.string.manual_replacement_name_check_tip)
            return false
        }
        if (vo.selectedTypeId == null) {
            ToastUtils.tip(R.string.manual_replacement_type_check_tip)
            return false
        }
        if (vo.selectedModelId == null) {
            ToastUtils.tip(R.string.manual_replacement_mode_check_tip)
            return false
        }
        if (vo.newRfid.isNullOrEmpty()) {
            ToastUtils.tip(R.string.manual_replacement_mode_rfid_tip)
            return false
        }
        if (!vo.isValidNewDate()) {
            ToastUtils.tip(R.string.manual_replacement_mode_date_tip)
            return false
        }
        return true
    }

    fun submit(
        list: MutableList<ManualReplacementListRespVO>,
        propertyList: MutableList<MaterialPropertyValuePageRespVO.Record>,
        callBack: (Boolean) -> Unit
    ) {
        if (list.isEmpty()) {
            return
        }
        val filterList = list.flatMap { it.materials ?: emptyList() }
            .filter { it.isDone }   // 只提交保存的
            .distinctBy { it.materialsId }  // 不同tab页数据去重
            .toMutableList()
        if (filterList.isEmpty()) {
            return
        }
        val submitList = mutableListOf<MaterialManualReplacementReqVO>()
        filterList.forEach { itFilter ->
            val property = propertyList.find { it.recordId == itFilter.selectedModelId }
            submitList.add(
                MaterialManualReplacementReqVO(
                    itFilter.materialsId,
                    itFilter.newName,
                    itFilter.selectedTypeId,
                    property?.recordId?.toString(),
                    property?.valueName,
                    property?.propertyId?.toString(),
                    property?.propertyName,
                    itFilter.newRfid,
                    itFilter.newDate,
                )
            )
        }
        NetApi.manualReplacement(submitList) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    /**
     * 同步不同tab标签的数据
     */
    fun syncData(
        list: MutableList<ManualReplacementListRespVO>,
        material: ManualReplacementListRespVO.MaterialsPageVO
    ) {
        list.forEach {
            it.materials?.find { it.materialsId == material.materialsId }?.let { itVO ->
                itVO.newName = material.newName
                itVO.newRfid = material.newRfid
                itVO.newDate = material.newDate
                itVO.selectedTypeId = material.selectedTypeId
                itVO.selectedModelId = material.selectedModelId
                itVO.isDone = material.isDone
            }
        }
    }
}