package com.mints.goodmoney.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.mints.goodmoney.R
import com.mints.goodmoney.mvp.model.DrawcashBean
import com.mints.goodmoney.ui.adapter.listener.OnItemClickListener
import java.lang.String

class DrawcashAdapter(val priceList: MutableList<DrawcashBean.CashOutMoneyArrBean>) :
        RecyclerView.Adapter<DrawcashAdapter.ViewHolder>() {

    var myPos: Int = 0
    lateinit var context: Context

    lateinit var mOnItemClickListener: OnItemClickListener

    init {
        if (priceList != null && priceList.size > 0) {
            for (i in 0..priceList.size - 1) {
                if (priceList[i].isCanCashOut) {
                    myPos = i
                    break
                }
            }
        }
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val llDrawItem: LinearLayout = view.findViewById(R.id.llDrawItem)
        val tvDrawItemCash: TextView = view.findViewById(R.id.tvDrawItemCash)
        val tvDrawItemGold: TextView = view.findViewById(R.id.tvDrawItemGold)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        val view = LayoutInflater.from(context).inflate(R.layout.item_rv_drawcash, parent, false)
        val viewHolder = ViewHolder(view)
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val priceBean = priceList[position]
        if (priceBean.money >= 1) {
            holder.tvDrawItemCash.text = "${String.format("%.0f", priceBean.money)}元"
        } else {
            holder.tvDrawItemCash.text = "${priceBean.money}元"
        }
        holder.tvDrawItemGold.text = "约${String.format("%.0f", priceBean.money * 10000)}金币"

        holder.llDrawItem.setOnClickListener {
            if (priceBean.isCanCashOut) {
                holder.llDrawItem.setEnabled(true)
                mOnItemClickListener.onItemClick(holder.llDrawItem, position)
                notifyDataSetChanged()
            } else {
                holder.llDrawItem.setEnabled(false)
            }
        }

        if (!priceBean.isCanCashOut) {
            holder.tvDrawItemCash.setTextColor(ContextCompat.getColor(context, R.color.grayc))
            holder.tvDrawItemGold.setTextColor(ContextCompat.getColor(context, R.color.grayc))
            holder.llDrawItem.setBackgroundResource(R.drawable.shape_vip_adapter)
        } else {
            //当前选中
            if (getPosition() == position) {
                holder.tvDrawItemCash.setTextColor(ContextCompat.getColor(context, R.color.white))
                holder.tvDrawItemGold.setTextColor(ContextCompat.getColor(context, R.color.white))
                holder.llDrawItem.setBackgroundResource(R.drawable.shape_vip_adapter_none)
            } else {
                holder.tvDrawItemCash.setTextColor(ContextCompat.getColor(context, R.color.black))
                holder.tvDrawItemGold.setTextColor(ContextCompat.getColor(context, R.color.gray))
                holder.llDrawItem.setBackgroundResource(R.drawable.shape_vip_adapter)
            }
        }


        holder.itemView.setTag(position)
    }

    override fun getItemCount() = priceList.size

    fun setOnItemClickListener(listener: OnItemClickListener) {
        mOnItemClickListener = listener
    }

    fun getPosition() = myPos

    fun setPosition(myPos: Int) {
        this.myPos = myPos
    }
}