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

import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.ExposureReportData
import cn.muchinfo.rma.global.data.FinancialReportData
import cn.muchinfo.rma.global.data.SpotReportData
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.base.BaseViewModel
import mtp.polymer.com.autowidget.utils.TaskUiModel

class ReportQueryViewModel : BaseViewModel(){

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


    /**
     * 财务报表头部数据
     */
    val financialReportTitleData : MutableLiveData<FinancialReportData> = MutableLiveData()

    /**
     * 财务报表选择的日报表还是月报表 1为日报表 2为月报表
     */
    val isselectFinancialMonthOrDay : MutableLiveData<String> = MutableLiveData<String>().apply { value = "1" }

    /**
     * 财务报表-日报表-款项数据
     */
    val financialReportDayPaymentDataList : MutableLiveData<List<FinancialReportData>> = MutableLiveData()

    /**
     * 财务报表-日报表-发票数据
     */
    val financialReportDayInvoiceDataList : MutableLiveData<List<FinancialReportData>> = MutableLiveData()

    /**
     * 财务报表-月报表-列表数据
     */
    val financialReportMonthDataList : MutableLiveData<List<FinancialReportData>> = MutableLiveData()

    /**
     * 敞口日报表数据
     */
    val exposureReportDataList : MutableLiveData<List<ExposureReportData>> = MutableLiveData()

    /**
     * 现货报表选择的日报表还是月报表 1为日报表 2为月报表
     */
    val isselectSpotMonthOrDay : MutableLiveData<String> = MutableLiveData<String>().apply { value = "1" }

    /**
     * 现货报表列表数据（日/月）
     */
    val spotReportDataList : MutableLiveData<List<SpotReportData>> = MutableLiveData()

    /**
     * 现货报表详情列表
     */
    val spotReportDetailsDataList : MutableLiveData<List<SpotReportData>> = MutableLiveData()

    /**
     * 查询财务日报表(菜单:报表查询/财务报表/日报表)
     * @param tradedate String
     */
    fun qryReportDayFinance(tradedate : String,isFirstLoading : Boolean = true){

        if (isFirstLoading){
            loadingDialogStatus.postValue(TaskUiModel.inFlight())
        }

        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("tradedate", tradedate)
        }

        MyApplication.getInstance()?.reportManager?.qryReportDayFinance(params = params){isSuccess, respData, error ->
            if (isSuccess){
                if (respData?.size ?: 0 > 0){
                    financialReportTitleData.postValue(respData?.get(0))
                }else{
                    financialReportTitleData.postValue(FinancialReportData())
                }
            }
        }
    }

    /**
     * 查询财务日报表款项(菜单:报表查询/财务报表/日报表/款项)
     * @param tradedate String
     */
    fun qryReportDayFinanceKx(tradedate : String){
        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("tradedate", tradedate)
        }

        MyApplication.getInstance()?.reportManager?.qryReportDayFinanceKx(params = params){isSuccess, respData, error ->
            if (isSuccess){
                financialReportDayPaymentDataList.postValue(respData)
            }
        }
    }

    /**
     * 查询财务日报表发票(菜单:报表查询/财务报表/日报表/发票)
     * @param tradedate String
     */
    fun qryReportDayFinanceFp(tradedate : String){
        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("tradedate", tradedate)
        }

        MyApplication.getInstance()?.reportManager?.qryReportDayFinanceFp(params = params){isSuccess, respData, error ->
            if (isSuccess){
                financialReportDayInvoiceDataList.postValue(respData)
            }
        }
    }

    /**
     * 查询财务月报表(菜单:报表查询/财务报表/月报表)
     * @param cycletime String
     */
    fun qryReportMonthFinance(cycletime : String){
        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("cycletime", cycletime)
        }

        MyApplication.getInstance()?.reportManager?.qryReportMonthFinance(params = params){isSuccess, respData, error ->
            if (isSuccess){
                if (respData?.size ?: 0 > 0){
                    financialReportMonthDataList.postValue(respData?.get(0)?.dayFr)
                    financialReportTitleData.postValue(respData?.get(0))
                }else{
                    financialReportMonthDataList.postValue(arrayListOf())
                    financialReportTitleData.postValue(FinancialReportData())
                }

            }
        }
    }

    /**
     * 查询敞口日报表(菜单:报表查询/敞口报表/敞口日报表)
     * @param tradedate String
     */
    fun qryReportDayExposure(tradedate: String){
        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("tradedate", tradedate)
        }

        MyApplication.getInstance()?.reportManager?.qryReportDayExposure(params = params){isSuccess, respData, error ->
            if (isSuccess){
                exposureReportDataList.postValue(respData)
            }
        }
    }

    /**
     * 查询现货日报表(菜单:报表查询/现货报表/现货日报表)
     * @param tradedate String
     */
    fun qryReportDaySpot(tradedate : String){
        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("tradedate", tradedate)
        }
        MyApplication.getInstance()?.reportManager?.qryReportDaySpot(params = params){isSuccess, respData, error ->
            if (isSuccess){
                spotReportDataList.postValue(respData)
            }
        }
    }

    /**
     * 查询现货月报表(菜单:报表查询/现货报表/现货月报表)
     * @param tradedate String
     */
    fun qryReportMonthSpot(tradedate : String){
        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("tradedate", tradedate)
        }
        MyApplication.getInstance()?.reportManager?.qryReportMonthSpot(params = params){isSuccess, respData, error ->
            if (isSuccess){
                spotReportDataList.postValue(respData)
            }
        }
    }

    /**
     * 对于列表点击展开后的数据处理
     * @param index Int
     */
    fun resetDataList(index: Int) {
        val datanewlist = arrayListOf<SpotReportData>()
        spotReportDataList.value?.forEach {
            if (index == spotReportDataList.value?.indexOf(it)) {
                if (it.indexSelect == 0) {
                    datanewlist.add(it.copy(indexSelect = 1))
                } else {
                    datanewlist.add(it.copy(indexSelect = 0))
                }
            } else {
                datanewlist.add(it)
            }
        }
        spotReportDataList.postValue(datanewlist)
    }

    /**
     * 查询现货日报表详情(菜单:报表查询/现货报表/现货日报表详情)
     * @param wrstandardid String
     * @param tradedate String
     */
    fun qryReportDaySpotDetail(wrstandardid : String,tradedate : String){
        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("wrstandardid", wrstandardid)
            put("tradedate", tradedate)
        }

        MyApplication.getInstance()?.reportManager?.qryReportDaySpotDetail(params = params){isSuccess, respData, error ->
            if (isSuccess){
                spotReportDetailsDataList.postValue(respData)
            }
        }
    }

    /**
     * 查询现货月报表详情(菜单:报表查询/现货月报表/现货月报表详情)
     * @param wrstandardid String
     * @param cycletime String 周期时间:月(格式:yyyyMM)
     */
    fun qryReportMonthSpotDetail(wrstandardid : String,cycletime : String){
        val params = mutableMapOf<String, String>().apply {
            put("userid", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("wrstandardid", wrstandardid)
            put("cycletime", cycletime)
        }

        MyApplication.getInstance()?.reportManager?.qryReportMonthSpotDetail(params = params){isSuccess, respData, error ->
            if (isSuccess){
                spotReportDetailsDataList.postValue(respData)
            }
        }
    }
}