package cn.muchinfo.rma.view

import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.database.DataBase
import cn.muchinfo.rma.view.base.BaseViewModel
import kotlinx.coroutines.*
import mtp.polymer.com.autowidget.utils.TaskUiModel
import java.lang.Exception

class MainViewModel : BaseViewModel() {

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

    val isInitiaDataSuccess: MutableLiveData<Boolean> = MutableLiveData()

    /**
     * 初始化数据
     */
    // FIXME 这里后期需要加上错误信息和枚举的增量查询
    fun initiaData() {
        loadingDialogStatus.postValue(TaskUiModel.inFlight(msg = "数据初始化中..."))
        var canContinue = false
        // 获取资金账户
        runBlocking {
            val params = mutableMapOf<String, String>().apply {
                put("loginID", GlobalDataCollection.instance?.loginRsp?.loginID.toString())
            }
            MyApplication.getInstance()?.initializeManager?.getTaAccounts(params = params) { isSuccess, respData, error ->
                isInitiaDataSuccess.postValue(isSuccess)
                if (isSuccess) {
                    getUserAccount()
                    GlobalDataCollection.instance?.accountDataList = respData
                } else {
                    loadingDialogStatus.postValue(TaskUiModel.idel())
                }
            }
        }
        try {
            GlobalDataCollection.instance?.errorCodeEntityList = DataBase.getInstance().errorCodeDao().getList()
        }catch (e : Exception){
            e.printStackTrace()
        }

//
//        //如果获取资金账户信息出错，则中断请求
//        if (!canContinue) {
//            loadingDialogStatus.postValue(TaskUiModel.idel())
//            isInitiaDataSuccess.postValue(false)
//            return
//        }
//        //请求增量错误信息，且无论成功不影响后续请求继续
//        runBlocking {
//            val params = mutableMapOf<String, String>().apply {
//                put("rowNumber", getErrorRowNumber())
//            }
//
//            MyApplication.getInstance()?.initializeManager?.queryErrorInfos(params) { isSuccess, respData, error ->
//                //
//                canContinue = true
//                if (isSuccess) {
//                    DataBase.getInstanse().errorCodeDao().insert(respData ?: arrayListOf())
//                }
//            }
//        }
//        isInitiaDataSuccess.postValue(true)


    }

    /**
     * 获取 数据库起始行号前索引
     * @return String
     */
    fun getErrorRowNumber(): String {
        var rowNumber = ""
        val errorList = DataBase.getInstance().errorCodeDao().getList()
        if (errorList.isNotEmpty()) {
            rowNumber = errorList.get(errorList.size.minus(1)).rownumber
        }
        return rowNumber
    }

    /**
     * 获取用户账号信息
     */
    fun getUserAccount(){
        val params = mutableMapOf<String, String>().apply {
            put("userID", GlobalDataCollection.instance?.loginRsp?.userID.toString())
        }

        MyApplication.getInstance()?.initializeManager?.getUserAccount(params = params){isSuccess, respData, error ->
            isInitiaDataSuccess.postValue(isSuccess)
            if (isSuccess) {
                loadingDialogStatus.postValue(TaskUiModel.success(msg = "数据初始化成功"))
                GlobalDataCollection.instance?.userAccountData = respData
            } else {
                loadingDialogStatus.postValue(TaskUiModel.idel())
            }
        }
    }
}