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

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.view.base.BaseViewModel
import cn.muchinfo.rma.view.base.future.interfaces.OnItemClickListener
import org.jetbrains.anko.backgroundColor

class DealViewModel : BaseViewModel() {

    var list: ArrayList<DealData>? = null
    var onItemClickListener: OnItemClickListener? = null // item点击事件
    var currentDealData: DealData? = null // 当前选择的item对应的数据

    /**
     * 获取配置器。
     */
    fun getAdapter(): RecyclerView.Adapter<HoldAdapter.DealHolder> {
        return HoldAdapter(context, list)
    }
    /**
     * 通用的适配器。
     */
    inner class HoldAdapter(context: Context?, list: ArrayList<DealData>?) : RecyclerView.Adapter<HoldAdapter.DealHolder>() {
        private var list: ArrayList<DealData>? = 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
        private var selectColor: 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)
            selectColor = context.getColor(R.color.p_select_bg_color)!!
        }

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

        inner class DealHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            var idGoodsName = itemView?.findViewById<TextView>(R.id.id_goods_name)
            var idTransType = itemView?.findViewById<TextView>(R.id.id_trans_type)
            var idDealNum = itemView?.findViewById<TextView>(R.id.id_deal_num)
            var idDealPrice = itemView?.findViewById<TextView>(R.id.id_deal_price)
            var idDealTime = itemView?.findViewById<TextView>(R.id.id_deal_time)

            var idChart = itemView?.findViewById<TextView>(R.id.id_chart)
            var idEntrustDetail = itemView?.findViewById<TextView>(R.id.id_deal_detail)
        }

        override fun onBindViewHolder(holder: DealHolder, position: Int) {
            holder.itemView.tag = list?.get(position)

            holder.idGoodsName?.text = list?.get(position)?.goodsName
            holder.idTransType?.text = list?.get(position)?.direction.toString()
            holder.idDealNum?.text = list?.get(position)?.dealNum.toString()
            holder.idDealPrice?.text = list?.get(position)?.dealPrice.toString()
            holder.idDealTime?.text = list?.get(position)?.dealTime

            // 下面三个按钮的点击事件
            holder.idChart?.tag = list?.get(position)
            holder.idEntrustDetail?.tag = list?.get(position)
            holder.idChart?.setOnClickListener {

            }
            holder.idEntrustDetail?.setOnClickListener {

            }

            if (list?.get(position)?.select == true) {
                holder.idChart?.visibility = View.VISIBLE
                holder.idEntrustDetail?.visibility = View.VISIBLE
                holder.itemView.backgroundColor = selectColor
            } else if(list?.get(position)?.select == false) {
                holder.idChart?.visibility = View.GONE
                holder.idEntrustDetail?.visibility = View.GONE
                holder.itemView.backgroundColor = 0
            }

            // 点击item事件
            holder.itemView.setOnClickListener {
                clickItem(it)
                onItemClickListener?.onClick(it)
            }
        }

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

        private fun clickItem(view: View) {
            currentDealData = view.tag as DealData? // 临时存储当前点击item

            // 选择
            list?.forEach { itemValue ->
                if (itemValue.goodsId == currentDealData?.goodsId) {
                    itemValue.select = !itemValue.select
                } else {
                    itemValue.select = false
                }
            }

            // 刷新数据
            notifyDataSetChanged()
        }
    }
}