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.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.mints.goodmoney.R
import com.mints.goodmoney.mvp.model.GoldRecordBean
import com.mints.goodmoney.ui.adapter.listener.OnItemClickListener

class CoinRecordAdapter(context: Context, coinData: MutableList<GoldRecordBean.ListBean>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        const val HOLDER_TYPE_EMPTY = 0
        const val HOLDER_TYPE_COIN = 1
    }

    private var mContext: Context = context
    private var mCoinData: List<GoldRecordBean.ListBean> = coinData

    private var mOnItemClickListener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == HOLDER_TYPE_EMPTY) {
            val emptyView = LayoutInflater.from(mContext).inflate(R.layout.item_empty, parent, false)
            return EmptyHolder(emptyView)
        }

        val view = LayoutInflater.from(mContext).inflate(R.layout.item_list_goldrecord, parent, false)
        return ViewHolder(view)
    }

    override fun getItemViewType(position: Int): Int {
        if (mCoinData.isEmpty()) {
            return HOLDER_TYPE_EMPTY
        }
        return HOLDER_TYPE_COIN
    }

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        if (viewHolder is EmptyHolder) {
            return
        }

        val holder = viewHolder as ViewHolder

        val coinData = mCoinData[position]
        if (coinData.isShowDay) {
            holder.tv_goldrecord_date.visibility = View.VISIBLE
            holder.tv_goldrecord_date.text = coinData.day

            if (coinData.day.contains("今")) {
                holder.tv_goldrecord_date.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
            } else {
                holder.tv_goldrecord_date.setCompoundDrawablesWithIntrinsicBounds(mContext.resources.getDrawable(R.mipmap.ic_goldrecord_time), null, null, null)
            }
        } else {
            holder.tv_goldrecord_date.visibility = View.GONE
        }

        holder.tv_goldrecord_time.text = coinData.time
        holder.tv_goldrecord_content.text = coinData.goldText

        when (coinData.type) {
            0 -> {
                if (coinData.rewardType == 0) {
                    // 0-金币
                    holder.tv_goldrecord_cash_type.text = "金币"
                    holder.tv_goldrecord_cash.setTextColor(ContextCompat.getColor(mContext, R.color.main_mints))
                } else {
                    holder.tv_goldrecord_cash_type.text = "钻石"
                    holder.tv_goldrecord_cash.setTextColor(ContextCompat.getColor(mContext, R.color.versus_task_play_text))
                }
                holder.tv_goldrecord_cash.text = "+" + coinData.unitCoin
            }
            else -> {
                if (coinData.rewardType == 0) {
                    // 0-金币
                    holder.tv_goldrecord_cash_type.text = "金币"
                } else {
                    holder.tv_goldrecord_cash_type.text = "钻石"
                }
                holder.tv_goldrecord_cash.text = "-" + coinData.unitCoin
                holder.tv_goldrecord_cash.setTextColor(ContextCompat.getColor(mContext, R.color.loan_green))
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mCoinData.isEmpty()) {
            1
        } else {
            mCoinData.size
        }
    }

    inner class EmptyHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tv_goldrecord_date: TextView = view.findViewById(R.id.tv_goldrecord_date)
        val tv_goldrecord_time: TextView = view.findViewById(R.id.tv_goldrecord_time)
        val tv_goldrecord_content: TextView = view.findViewById(R.id.tv_goldrecord_content)
        val tv_goldrecord_cash: TextView = view.findViewById(R.id.tv_goldrecord_cash)
        val tv_goldrecord_cash_type: TextView = view.findViewById(R.id.tv_goldrecord_cash_type)
    }

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

}