package cn.muchinfo.rma.view.base.home.customerdata

import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.UserInfoData
import cn.muchinfo.rma.netManage.base.InteractiveException
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.autoWidget.guard
import cn.muchinfo.rma.view.base.BaseViewModel
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import mtp.polymer.com.autowidget.utils.TaskUiModel

class CustomerDataViewModel : BaseViewModel() {

    /**
     * 加载状态控制
     */
    val loadingDialogStatus: MutableLiveData<TaskUiModel> = MutableLiveData()

    /**
     * 客户资料 - 未提交
     */
    val unSubmitList: MutableLiveData<List<UserInfoData>> = MutableLiveData()

    /**
     * 客户资料 - 待审核
     */
    val toAuditList: MutableLiveData<List<UserInfoData>> = MutableLiveData()

    /**
     * 客户资料 - 正常
     */
    val normalList: MutableLiveData<List<UserInfoData>> = MutableLiveData()

    /**
     * 客户资料 - 停用
     */
    val disableList: MutableLiveData<List<UserInfoData>> = MutableLiveData()

    /**
     * 对于列表点击展开后的数据处理
     * @param index Int 点击的item数据在列表的index
     * @param type String 点击的列表类型
     */
    fun resetDataList(index: Int, type: String) {
        val datanewlist = arrayListOf<UserInfoData>()
        when (type) {
            "1" -> {
                unSubmitList.value?.forEach {
                    if (index == unSubmitList.value?.indexOf(it)) {
                        if (it.indexSelect == 0) {
                            datanewlist.add(it.copy(indexSelect = 1))
                        } else {
                            datanewlist.add(it.copy(indexSelect = 0))
                        }
                    } else {
                        datanewlist.add(it)
                    }
                }
                unSubmitList.postValue(datanewlist)
            }
            "2" -> {
                toAuditList.value?.forEach {
                    if (index == toAuditList.value?.indexOf(it)) {
                        if (it.indexSelect == 0) {
                            datanewlist.add(it.copy(indexSelect = 1))
                        } else {
                            datanewlist.add(it.copy(indexSelect = 0))
                        }
                    } else {
                        datanewlist.add(it)
                    }
                }
                toAuditList.postValue(datanewlist)
            }
            "3" -> {
                normalList.value?.forEach {
                    if (index == normalList.value?.indexOf(it)) {
                        if (it.indexSelect == 0) {
                            datanewlist.add(it.copy(indexSelect = 1))
                        } else {
                            datanewlist.add(it.copy(indexSelect = 0))
                        }
                    } else {
                        datanewlist.add(it)
                    }
                }
                normalList.postValue(datanewlist)
            }
            "4" -> {
                disableList.value?.forEach {
                    if (index == disableList.value?.indexOf(it)) {
                        if (it.indexSelect == 0) {
                            datanewlist.add(it.copy(indexSelect = 1))
                        } else {
                            datanewlist.add(it.copy(indexSelect = 0))
                        }
                    } else {
                        datanewlist.add(it)
                    }
                }
                disableList.postValue(datanewlist)
            }
        }

    }

    /**
     * 查询客户资料列表
     */
    fun queryUserInfo(type: String) {
//        unSubmitList.value = arrayListOf(UserInfoData(address = "1"),UserInfoData(address = "2"),UserInfoData(address = "3"))
        loadingDialogStatus.value = TaskUiModel.inFlight()
        val params = mutableMapOf<String, String>().apply {
            put("MemberUserID", GlobalDataCollection.instance?.loginRsp?.memberUserID.toString())
            put("queryType", type)
        }
        MyApplication.getInstance()?.customerManage?.queryUserInfo(params) { isSuccess, respData, error ->
            if (isSuccess) {
                when (type) {
                    "1" -> {    // 未提交
                        unSubmitList.postValue(respData)
                    }
                    "2" -> {    // 待审核
                        toAuditList.postValue(respData)
                    }
                    "3" -> {    // 正常
                        normalList.postValue(respData)
                    }
                    "4" -> {    // 停用
                        disableList.postValue(respData)
                    }
                }
            } else {
                // 查询失败
                loadingDialogStatus.value =
                    TaskUiModel.failed(InteractiveException(errorMessage = error?.message ?: ""))
            }
        }
    }

    /**
     * 新增客户申请
     * @param params Map<String, String> 请求参数
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit> 回调
     */
    fun addUserInfoApply(
        params: Map<String, Any>,
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        val app = MyApplication.getInstance().guard {
            callback(false, Error("Application未初始化"))
            return
        }
        val customerManage = app.customerManage.guard {
            callback(false, Error("Application未初始化"))
            return
        }

        customerManage.addUserInfoApply(params, callback);
    }

    /**
     * 客户资料修改
     * @param params Map<String, Any>
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     */
    fun modifyUserInfoApply(
        params: Map<String, Any>,
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        val app = MyApplication.getInstance().guard {
            callback(false, Error("Application未初始化"))
            return
        }
        val customerManage = app.customerManage.guard {
            callback(false, Error("Application未初始化"))
            return
        }

        customerManage.modifyUserInfoApply(params, callback);
    }

    /**
     * 客户信息审核请求
     * @param auditflag Long 1审核通过2审核拒绝
     * @param userid Long 客户userid
     * @param audituserid Long 审核操作人userid
     * @param remark String 备注
     * @return Packet50
     */
    fun requestUserInfoCheckMange(
        auditflag: Long,
        userid: Long,
        audituserid: Long,
        remark: String = "",
        isSuccess: (isCompleted: Boolean) -> Unit
    ) {
        loadingDialogStatus.value = TaskUiModel.inFlight()
        GlobalScope.launch {
            MyApplication.getInstance()?.customerManage?.requestUserInfoCheckMange(
                auditflag = auditflag,
                userid = userid,
                audituserid = audituserid,
                remark = remark
            ) { isCompleted, err ->
                if (isCompleted) {
                    loadingDialogStatus.postValue(TaskUiModel.success(msg = "请求成功"))
                    isSuccess(true)
                } else {
                    loadingDialogStatus.postValue(
                        TaskUiModel.failed(
                            InteractiveException(
                                errorMessage = err?.message!!
                            )
                        )
                    )
                }
            }
        }
    }

    /**
     * 更新用户状态
     * @param userID Int
     * @param accountStatus Int
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     */
    fun updateUserAccountStatus(
        userID: Int,
        accountStatus: Int,
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        val app = MyApplication.getInstance().guard {
            callback(false, Error("Application未初始化"))
            return
        }
        val customerManage = app.customerManage.guard {
            callback(false, Error("Application未初始化"))
            return
        }

        customerManage.updateUserAccountStatus(userID = userID,accountStatus = accountStatus, callback = callback)
    }
}