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.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.view.base.BaseViewModel

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

    var list: ArrayList<HoldData>? = null

    /**
     * 获取配置器。
     */
    fun getAdapter(): RecyclerView.Adapter<HoldAdapter.HoldHolder> {
        return HoldAdapter(context, list)
    }
    /**
     * 通用的适配器。
     */
    class HoldAdapter(context: Context?, list: ArrayList<HoldData>?) : RecyclerView.Adapter<HoldAdapter.HoldHolder>() {
        private var list: ArrayList<HoldData>? = 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 = list
            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
        }

        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 idLastPrice = itemView?.findViewById<TextView>(R.id.id_last_price)
            var idCost = itemView?.findViewById<TextView>(R.id.id_cost)
            var idPl = itemView?.findViewById<TextView>(R.id.id_pl)
            var idPlp = itemView?.findViewById<TextView>(R.id.id_plp)

        }

        override fun onBindViewHolder(holder: HoldHolder, position: Int) {
            holder.idGoodsName?.text = list?.get(position)?.goodsName
            holder.idAvailable?.text = list?.get(position)?.available.toString()
            holder.idHoldTotal?.text = list?.get(position)?.totals.toString()
            holder.idLastPrice?.text = list?.get(position)?.lastPrice.toString()
            holder.idCost?.text = list?.get(position)?.cost.toString()
            holder.idPl?.text = list?.get(position)?.pl.toString()
            holder.idPlp?.text = list?.get(position)?.plp.toString()
            if (list?.get(position)?.direction == 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))
        }

    }
}