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

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

/**
 * 今日委托和今日可撤model
 */
class EntrustChildViewModel : BaseViewModel() {

    var list: ArrayList<EntrustData>? = null // 数据
    var onItemClickListener: OnItemClickListener? = null // item点击事件
    var currentEntrustData: EntrustData? = null // 当前选择的item对应的数据
    /**
     * 获取配置器。
     */
    fun getAdapter(): RecyclerView.Adapter<HoldAdapter.EntrustHolder> {
        return HoldAdapter(context, list)
    }

    /**
     * 通用的适配器。
     */
    inner class HoldAdapter(context: Context?, list: ArrayList<EntrustData>?) : RecyclerView.Adapter<HoldAdapter.EntrustHolder>() {
        private var list: ArrayList<EntrustData>? = null
        private var inflater: LayoutInflater? = null
        private var context: Context? = null
        private var selectColor: Int = 0

        init {
            this.list = list
            this.inflater = LayoutInflater.from(context)
            this.context = context
            selectColor = context?.getColor(R.color.p_select_bg_color)!!
        }

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

        inner class EntrustHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            var idGoodsName = itemView?.findViewById<TextView>(R.id.id_goods_name)
            var idStatus = itemView?.findViewById<TextView>(R.id.id_status)
            var idDate = itemView?.findViewById<TextView>(R.id.id_date)
            var idEntrustType = itemView?.findViewById<TextView>(R.id.id_entrust_type)
            var idEntrustNum = itemView?.findViewById<TextView>(R.id.id_entrust_num)
            var idEntrustPrice = itemView?.findViewById<TextView>(R.id.id_entrust_price)
            var idEntrustPl = itemView?.findViewById<TextView>(R.id.id_entrust_pl)

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

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

            entrust.idGoodsName?.text = list?.get(position)?.goodsName
            entrust.idStatus?.text = list?.get(position)?.status
            entrust.idEntrustType?.text = list?.get(position)?.entrustType
            entrust.idDate?.text = list?.get(position)?.time
            entrust.idEntrustPrice?.text = list?.get(position)?.entrustPrice.toString()
            entrust.idEntrustNum?.text = list?.get(position)?.entrustNum.toString()
            entrust.idEntrustPl?.text = list?.get(position)?.pl.toString()

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

            }
            entrust.idEntrustDetail?.setOnClickListener {

            }
            entrust.idCancel?.setOnClickListener {

            }

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

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

        }

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

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

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

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