package cn.muchinfo.rma.view.base.future.orderDeatails

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.futureOrders.DealOrderData
import cn.muchinfo.rma.global.toShowTime
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.autoWidget.DetailItem
import cn.muchinfo.rma.view.base.BaseViewModel
import cn.muchinfo.rma.view.base.future.costs.OrderDetailData

/**
 * 单据详情
 */
class OrderDetailModel: BaseViewModel() {

    var orderDetailList: ArrayList<OrderDetailData>? = ArrayList() // 信息

    /**
     * 对应的成交单数据
     */
    var tradeDeatils: MutableLiveData<ArrayList<DealOrderData>> = MutableLiveData()

    /**
     * 当前商品ID
     */
    var goodsID: Int = 0

    /**
     * 当前买卖方向
     */
    var buyOrSell: Int = 0

//region cost info 资金信息

    /**
     * 获取配置器。
     */
    fun getCostInfoAdapter(context: Context): RecyclerView.Adapter<OrderDetailAdapter.CostInfoHolder> {
        return OrderDetailAdapter(context, orderDetailList)
    }

    /**
     * 资金信息的适配器。
     */
    class OrderDetailAdapter(context: Context?, list: ArrayList<OrderDetailData>?) : RecyclerView.Adapter<OrderDetailAdapter.CostInfoHolder>() {
        private var list: ArrayList<OrderDetailData>? = null
        private var inflater: LayoutInflater? = null
        private var context: Context? = null
        init {
            this.context = context
            this.list = list
            this.inflater = LayoutInflater.from(context)
        }

        override fun getItemCount(): Int {
            return list?.size ?: 0
        }

        class CostInfoHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            var detailItem = itemView?.findViewById<DetailItem>(R.id.id_item)
        }

        override fun onBindViewHolder(holder: CostInfoHolder, position: Int) {
            holder.detailItem?.setLeftText(list?.get(position)?.getLeftText().toString())
            holder.detailItem?.setRightText(list?.get(position)?.getRightText().toString())
            list?.get(position)?.getRightTextPadding()?.let { holder.detailItem?.setRightValuePadding(it) }
            list?.get(position)?.getLeftTextPadding()?.let { holder.detailItem?.setLeftValuePadding(it) }
            holder.detailItem?.showLine(true)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CostInfoHolder {
            return CostInfoHolder(inflater?.inflate(R.layout.cost_info_item, parent, false))
        }

    }
//endregion

    /**
     * 查询对应的成交单信息
     */
    fun queryHedgeTradeDetails(goodsID: Int, buyOrSell: Int) {
        val params = mutableMapOf<String, String>()
        params["accountID"] = GlobalDataCollection.instance?.accountId.toString()
        params["goodsID"] = goodsID.toString()
        params["buyOrSell"] = buyOrSell.toString()

        MyApplication.getInstance()?.futureManager?.queryErmcpTradeDetails(params) { isSuccess, respData, _ ->
            if (!isSuccess) {
                return@queryErmcpTradeDetails
            }

            tradeDeatils.value = respData as ArrayList<DealOrderData>
        }
    }

    /**
     * 相关成交单列表适合器
     * @property list ArrayList<DealOrderData> 成交单列表
     * @property inflater LayoutInflater? LayoutInflater
     * @constructor
     */
    inner class TradeDetailAdapter(context: Context?): RecyclerView.Adapter<TradeDetailAdapter.TradeDetailHolder>() {
        private var list: ArrayList<DealOrderData> = ArrayList()
        private var inflater: LayoutInflater? = null

        init {
            this.inflater = LayoutInflater.from(context)
        }

        /**
         * 持仓详情对应成交单ViewHolder
         * @property idTradeTime TextView? 成交时间
         * @property idTradeNumber TextView? 成交数量
         * @property idTradePrice TextView? 成交金额
         * @constructor
         */
        inner class TradeDetailHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            var idTradeTime = itemView?.findViewById<TextView>(R.id.id_trade_time)
            var idTradeNumber = itemView?.findViewById<TextView>(R.id.id_trade_number)
            var idTradePrice = itemView?.findViewById<TextView>(R.id.id_trade_price)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TradeDetailHolder {
            return TradeDetailHolder(inflater?.inflate(R.layout.order_detail_relation_item, parent, false))
        }

        override fun onBindViewHolder(holder: TradeDetailHolder, position: Int) {
            holder.itemView.tag = list[position]
            val itemData = list[position]

            holder.idTradeTime?.text = itemData.tradetime?.toShowTime()
            holder.idTradeNumber?.text = itemData.tradeqty
            holder.idTradePrice?.text = itemData.tradeprice
        }

        override fun getItemCount(): Int {
            return list.size
        }

        fun update(list: ArrayList<DealOrderData>) {
            this.list = list
            notifyDataSetChanged()
        }
    }

    /**
     * 获取相关成交单适配器
     * @return RecyclerView.Adapter<TradeDetailAdapter.TradeDetailHolder>
     */
    fun getTradeDetailAdapter(context: Context?): RecyclerView.Adapter<TradeDetailAdapter.TradeDetailHolder> {
        return TradeDetailAdapter(context)
    }
}