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

import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.AmountLogData
import cn.muchinfo.rma.global.data.FinancialManageData
import cn.muchinfo.rma.global.data.futureOrders.DealOrderData
import cn.muchinfo.rma.global.data.futureOrders.FutureEntrustData
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.base.BaseViewModel

class TradingQueryViewModel : BaseViewModel(){

    /**
     * 历史资金流水
     */
    val hisAmountLogDataList : MutableLiveData<List<AmountLogData>> = MutableLiveData()

    /**
     * 历史成交单数据
     */
    val dealOrderHistoryList : MutableLiveData<List<DealOrderData>> = MutableLiveData()

    /**
     * 历史委托数据
     */
    val entrustOrderHistoryList : MutableLiveData<List<FutureEntrustData>> = MutableLiveData()

    /**
     * 历史成交单的点击展开事件
     * @param index Int
     */
    fun clickItemDeal(index: Int){
        val datanewlist = arrayListOf<DealOrderData>()
        dealOrderHistoryList.value?.forEach {
            if (index == dealOrderHistoryList.value?.indexOf(it)) {
                datanewlist.add(it.copy(select = it.select.not()))
            } else {
                datanewlist.add(it)
            }
        }
        dealOrderHistoryList.postValue(datanewlist)
    }

    /**
     * 历史成交单的点击展开事件
     * @param index Int
     */
    fun clickItemEntrust(index: Int){
        val datanewlist = arrayListOf<FutureEntrustData>()
        entrustOrderHistoryList.value?.forEach {
            if (index == entrustOrderHistoryList.value?.indexOf(it)) {
                datanewlist.add(it.copy(selected = it.selected.not()))
            } else {
                datanewlist.add(it)
            }
        }
        entrustOrderHistoryList.postValue(datanewlist)
    }

    /**
     * 查询资金流水
     * @param startDate String 开始时间 闭区间，格式：yyyy-MM-dd
     * @param endDate String 结束时间 闭区间，格式：yyyy-MM-dd
     */
    fun queryHisAmountLog(startDate : String = "",endDate : String = ""){
        val params = mutableMapOf<String, String>().apply {
            put("accountID", GlobalDataCollection.instance?.loginRsp?.memberUserID.toString())
            if (startDate.isNotEmpty()){
                put("startDate", startDate)
            }
            if (endDate.isNotEmpty()){
                put("endDate",endDate)
            }
        }

        MyApplication.getInstance()?.tradingQueryManager?.queryHisAmountLog(params = params){isSuccess, respData, error ->
            if (isSuccess){
                hisAmountLogDataList.postValue(respData)
            }
        }

    }

    /**
     * 查询历史成交
     * @param startDate String 开始时间 闭区间，格式：yyyy-MM-dd
     * @param endDate String 结束时间 闭区间，格式：yyyy-MM-dd
     */
    fun queryErmcpHisTradeDetails(startDate : String = "",endDate : String = ""){
        val params = mutableMapOf<String, String>().apply {
            put("accountID", GlobalDataCollection.instance?.accountId.toString())
            if (startDate.isNotEmpty()){
                put("startDate", startDate)
            }
            if (endDate.isNotEmpty()){
                put("endDate",endDate)
            }
        }

        MyApplication.getInstance()?.futureManager?.queryErmcpHisTradeDetails(params = params){isSuccess, respData, error ->
            if (isSuccess){
                dealOrderHistoryList.postValue(respData)
            }
        }

    }

    /**
     * 查询历史委托
     * @param startDate String 开始时间 闭区间，格式：yyyy-MM-dd
     * @param endDate String 结束时间 闭区间，格式：yyyy-MM-dd
     */
    fun queryErmcpHisEntrustDetails(startDate : String = "",endDate : String = ""){
        val params = mutableMapOf<String, String>().apply {
            put("accountID", GlobalDataCollection.instance?.accountId.toString())
            if (startDate.isNotEmpty()){
                put("startDate", startDate)
            }
            if (endDate.isNotEmpty()){
                put("endDate",endDate)
            }
        }

        MyApplication.getInstance()?.futureManager?.queryErmcpHisEntrustDetails(params = params){isSuccess, respData, error ->
            if (isSuccess){
                entrustOrderHistoryList.postValue(respData)
            }
        }

    }



}