package cn.muchinfo.rma.view.base.exposure

import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.*
import cn.muchinfo.rma.global.toPercentage
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.autoWidget.toArrayList
import cn.muchinfo.rma.view.autoWidget.utils.NumberUtils
import cn.muchinfo.rma.view.base.BaseViewModel

class ExposureViewModel : BaseViewModel(){

    /**
     * 实时敞口头部数据
     */
    val realTimeExposureTitle: MutableLiveData<List<ColumnsData>> = MutableLiveData()

    /**
     * 实时敞口列表数据
     */
    val realTimeExposureDataList : MutableLiveData<List<RealExposureData>> = MutableLiveData()

    /**
     * 现货数量列表数据
     */
    val exposureSpotDataList : MutableLiveData<List<NumberSpotData>> = MutableLiveData()

    /**
     * 期货数量列表数据
     */
    val exposurePositionDataList : MutableLiveData<List<NumberSpotData>> = MutableLiveData()

    /**
     * 历史敞口列表数据
     */
    val exposureHistoryDataList : MutableLiveData<List<ExposureHistoryData>> = MutableLiveData()

    /**
     * 实时敞口明细头部数据
     */
    val exposureRealTimeDetailsTitle: MutableLiveData<List<ColumnsData>> = MutableLiveData()

    /**
     * 实时敞口明细列表
     */
    val exposureRealTimeDetailsDataList : MutableLiveData<List<ExposureRealTimeDetailsData>> = MutableLiveData()

    /**
     * 现货头寸-现货明细列表数据/期货头寸-期货明细列表数据
     */
    val exposureSpotDetailsDataList : MutableLiveData<List<NumberSpotDetailsData>> = MutableLiveData()

    /**
     * 查询实时敞口明细列表头部数据
     * @param tableKey String
     * type 1 是现货 2是期货
     */
    fun queryRealDetailsTitle(tableKey: String,type : String,middleGoodsId : String) {
        val params = mutableMapOf<String, String>().apply {
            put("tableKey", tableKey)
            put("tableType", "3")
        }
        MyApplication.getInstance()?.commonManager?.queryTableDefine(params = params) { isSuccess, respData, error ->
            if (isSuccess) {
                val newlist = respData?.get(0)?.columns?.toArrayList()
                newlist?.sortWith(Comparator { t1, t2 ->
                    t1.orderindex?.toInt()?.minus(t2.orderindex?.toInt() ?: 0) ?: 0
                })
                exposureRealTimeDetailsTitle.postValue(newlist)
                if (type == "1"){
                    queryExposureDetail(middleGoodsId)
                }else{
                    queryRealtimeExposurePosition(middleGoodsId)
                }
            }
        }
    }

    /**
     * 查询实时敞口的现货明细
     */
    fun queryExposureDetail(middleGoodsId : String){
        val params = mutableMapOf<String, String>().apply {
            put("areaUserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("middleGoodsId", middleGoodsId)
        }

        MyApplication.getInstance()?.exposureManager?.queryExposureDetail(params = params){isSuccess, respData, error ->
            if (isSuccess){
                exposureRealTimeDetailsDataList.postValue(getRealTimeExposureDatailsSpotRightData(respData ?: arrayListOf()))
            }
        }
    }

    /**
     * 查询实时敞口的期货明细
     */
    fun queryRealtimeExposurePosition(middleGoodsId : String){
        val params = mutableMapOf<String, String>().apply {
            put("areaUserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("middleGoodsId", middleGoodsId)
        }

        MyApplication.getInstance()?.exposureManager?.queryRealtimeExposurePosition(params = params){isSuccess, respData, error ->
            if (isSuccess){
                exposureRealTimeDetailsDataList.postValue(getRealTimeExposureDatailsFutureRightData(respData ?: arrayListOf()))
            }
        }
    }

    /**
     * 获取实时敞口现货品种右侧数据
     * @param respData List<ExposureRealTimeDetailsData>
     * @return List<ExposureRealTimeDetailsData>
     */
    private fun getRealTimeExposureDatailsSpotRightData(respData: List<ExposureRealTimeDetailsData>) : List<ExposureRealTimeDetailsData>{
        val newList = arrayListOf<ExposureRealTimeDetailsData>()
        respData.forEach {
            val newRightData = arrayListOf<String>()
            newRightData.add(it.enumdicname + "\n" + it.qty)
            newRightData.add(NumberUtils.roundNum(it.changeQty,2))
            newRightData.add(it.relateNo ?: "")
            newRightData.add(NumberUtils.roundNum(it.convertfactor,2) + "\n" + NumberUtils.roundNum(it.convertratio,2))
            val newData = it.copy(rightData = newRightData)
            newList.add(newData)
        }
        return newList
    }

    /**
     * 获取实时敞口期货品种右侧数据
     * @param respData List<ExposureRealTimeDetailsData>
     * @return List<ExposureRealTimeDetailsData>
     */
    private fun getRealTimeExposureDatailsFutureRightData(respData: List<ExposureRealTimeDetailsData>) : List<ExposureRealTimeDetailsData>{
        val newList = arrayListOf<ExposureRealTimeDetailsData>()
        respData.forEach {
            val newRightData = arrayListOf<String>()
            newRightData.add(it.ydqty + "\n" + it.curqty)
            newRightData.add(NumberUtils.roundNum(it.diffqty,2))
            newRightData.add(it.diffhedgeqty ?: "")
            newRightData.add(NumberUtils.roundNum(it.agreeunit,2) + "\n" + NumberUtils.roundNum(it.convertratio,2))
            val newData = it.copy(rightData = newRightData)
            newList.add(newData)
        }
        return newList
    }



    /**
     * 查询实时敞口列表头部数据
     * @param tableKey String
     */
    fun queryTitle(tableKey: String) {
        val params = mutableMapOf<String, String>().apply {
            put("tableKey", tableKey)
            put("tableType", "3")
        }
        MyApplication.getInstance()?.commonManager?.queryTableDefine(params = params) { isSuccess, respData, error ->
            if (isSuccess) {
                val newlist = respData?.get(0)?.columns?.toArrayList()
                newlist?.sortWith(Comparator { t1, t2 ->
                    t1.orderindex?.toInt()?.minus(t2.orderindex?.toInt() ?: 0) ?: 0
                })
                realTimeExposureTitle.postValue(newlist)
                queryRealtimeExposure()
            }
        }
    }

    /**
     * 查询实时敞口
     */
    fun queryRealtimeExposure(){
        val params = mutableMapOf<String, String>().apply {
            put("AreaUserID", GlobalDataCollection.instance?.loginRsp?.userID.toString())
        }

        MyApplication.getInstance()?.exposureManager?.queryRealtimeExposure(params = params){isSuccess, respData, error ->
            if (isSuccess){
                realTimeExposureDataList.postValue(getRealTimeExposureRightData(respData ?: arrayListOf()))
            }
        }
    }

    /**
     * 实时敞口组装右部数据
     * @param respData List<RealExposureData>
     * @return List<RealExposureData>
     */
    private fun getRealTimeExposureRightData(respData: List<RealExposureData>) : List<RealExposureData>{
        val newList = arrayListOf<RealExposureData>()
        respData.forEach {
            val newRightData = arrayListOf<String>()
            newRightData.add(NumberUtils.roundNum(it.TotalSpotQty,2) + "\n" + it.MiddleGoodsHedgeRatio?.toPercentage())
            newRightData.add(NumberUtils.roundNum(it.TotalNeedHedgeQty,2) + "\n" + NumberUtils.roundNum(it.TotalFutureQty,2))
            newRightData.add(NumberUtils.roundNum(it.NeedHedgeExposoure,2) + "\n" + it.NeedHedgeRatio?.toPercentage())
            newRightData.add(NumberUtils.roundNum(it.TotalExposure,2) + "\n" +it.TotalHedgeRatio?.toPercentage())
            newRightData.add(NumberUtils.roundNum(it.OriTotalSpotQty,2) + "\n" + NumberUtils.roundNum(it.DiffSpotQty,2))
            newRightData.add(NumberUtils.roundNum(it.OriTotalFutuQty,2) + "\n" + NumberUtils.roundNum(it.DiffFutuQty,2))
            val newData = it.copy(rightData = newRightData)
            newList.add(newData)
        }
        return newList
    }

    /**
     * 查询现货数量
     */
    fun queryExposureSpot(){
        val params = mutableMapOf<String, String>().apply {
            put("areaUserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
        }

        MyApplication.getInstance()?.exposureManager?.queryExposureSpot(params = params){isSuccess, respData, error ->
            if (isSuccess){
                exposureSpotDataList.postValue(respData)
            }
        }
    }

    /**
     * 查询期货数量
     */
    fun queryExposureHedgePosition(){
        val params = mutableMapOf<String, String>().apply {
            put("areaUserID", GlobalDataCollection.instance?.loginRsp?.userID.toString())
        }

        MyApplication.getInstance()?.exposureManager?.queryExposureHedgePosition(params = params){isSuccess, respData, error ->
            if (isSuccess){
                exposurePositionDataList.postValue(respData)
            }
        }
    }

    /**
     * 历史敞口查询
     */
    fun queryHisExposure(){
        val params = mutableMapOf<String, String>().apply {
            put("areaUserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("lastNum", "30")
        }
        MyApplication.getInstance()?.exposureManager?.queryHisExposure(params = params){isSuccess, respData, error ->
            if (isSuccess){
                exposureHistoryDataList.postValue(respData)
            }
        }
    }

    /**
     * 查询敞口现货头寸明细(敞口/现货头寸/现货明细)
     * @param wrstandardId String 现货品种ID
     */
    fun queryExposureSpotDetail(wrstandardId : String){
        val params = mutableMapOf<String, String>().apply {
            put("areaUserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("wrstandardId", wrstandardId)
        }
        MyApplication.getInstance()?.exposureManager?.queryExposureSpotDetail(params = params){isSuccess, respData, error ->
            if (isSuccess){
                exposureSpotDetailsDataList.postValue(respData)
            }
        }
    }

    /**
     * 查询敞口期货头寸期货明细(菜单:敞口/期货头寸/期货明细)
     * @param goodsId String 商品id
     */
    fun queryExposureHedgePositionDetail(goodsId : String){
        val params = mutableMapOf<String, String>().apply {
            put("areaUserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("goodsId", goodsId)
        }
        MyApplication.getInstance()?.exposureManager?.queryExposureHedgePositionDetail(params = params){isSuccess, respData, error ->
            if (isSuccess){
                exposureSpotDetailsDataList.postValue(respData)
            }
        }
    }


}