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

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.business.future.FutureManager
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.FutureDetailsData
import cn.muchinfo.rma.global.data.futureOrders.FutureEntrustData
import cn.muchinfo.rma.global.data.futureOrders.FutureHoldData
import cn.muchinfo.rma.view.base.BaseViewModel
import cn.muchinfo.rma.view.base.app.Constant
import com.blankj.utilcode.util.SPUtils

/**
 * 持仓的ViewModel
 */
class HoldViewModel : BaseViewModel() {

    val holdLiveData: MutableLiveData<List<FutureHoldData>> = MutableLiveData() // 持仓单

    var list: ArrayList<FutureHoldData>? = null
    var marketID: String? = null
    /**
     * 获取配置器。
     */
    fun getAdapter(): RecyclerView.Adapter<HoldAdapter.HoldHolder> {
        return HoldAdapter(context)
    }
    /**
     * 通用的适配器。
     */
    inner class HoldAdapter(context: Context?) : RecyclerView.Adapter<HoldAdapter.HoldHolder>() {
        private var list: ArrayList<FutureHoldData>? = null
        private var inflater: LayoutInflater? = null
        private var context: Context? = null
        private var pink: Drawable? = null
        private var green: Drawable? = null
        private var priceRed: Int = 0
        private var priceGreen: Int = 0
        init {
            this.list = GlobalDataCollection.instance?.futureHoldData
            this.inflater = LayoutInflater.from(context)
            this.context = context

            pink = context?.getDrawable(R.drawable.rma_red)
            green = context?.getDrawable(R.drawable.rma_green)
            priceGreen = context?.getColor(R.color.p_price_green)!!
            priceRed = context.getColor(R.color.p_price_red)
        }

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

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

        inner class HoldHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            var idGoodsName = itemView?.findViewById<TextView>(R.id.id_goods_name) // 商品名称
            var idTransDirection = itemView?.findViewById<TextView>(R.id.id_trans_direction) // 方向
            var idAvailable = itemView?.findViewById<TextView>(R.id.id_available) // 可用
            var idHoldTotal = itemView?.findViewById<TextView>(R.id.id_hold_total) // 持仓
            var idOpenAveragePrice = itemView?.findViewById<TextView>(R.id.id_open_average_price) // 开仓均价
            var idHoldAveragePrice = itemView?.findViewById<TextView>(R.id.id_hold_average_price) // 持仓均价
            var idZPl = itemView?.findViewById<TextView>(R.id.id_zpl) // 盯市浮盈
            var idPlp = itemView?.findViewById<TextView>(R.id.id_plp) // 盈亏比例
            var idMargin = itemView?.findViewById<TextView>(R.id.id_margin) // 保证金
        }

        override fun onBindViewHolder(holder: HoldHolder, position: Int) {
            holder.idGoodsName?.text = list?.get(position)?.goodsname
            holder.idAvailable?.text = list?.get(position)?.enableqty.toString() // 可用（总仓可用）
            holder.idHoldTotal?.text = list?.get(position)?.curpositionqty.toString() // 持仓（总仓数量, 期末头寸）
            if (list?.get(position)?.buyorsell == 0) {
                holder.idTransDirection?.text = context?.resources?.getString(R.string.str_buy)
            } else {
                holder.idTransDirection?.text = context?.resources?.getString(R.string.str_sell)
            }
        }

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

    }

    /**
     * 查询持仓单
     */
    fun queryHold() {
        val params = mutableMapOf<String, String>()
        params["accountID "] = SPUtils.getInstance().getLong(Constant.SELECT_ACCOUNT_ID).toString()
        if (marketID != null) {
            params["marketID"] = marketID!!
        }
        FutureManager().queryErmcpTradePosition(params) { isSuccess, respData, _ ->
            if (isSuccess && !respData!!.isNullOrEmpty()) {
                list = respData as ArrayList<FutureHoldData>
                holdLiveData.value = list
            } else {
                // 没有数据或者接口返回错误的时候，通知界面没有获取到数据

            }
        }
    }
}