package com.duben.microtribe.ui.adapter

import android.content.Context
import android.text.TextUtils
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.duben.microtribe.R
import com.duben.microtribe.mvp.model.OrderRecordBean
import com.duben.microtribe.ui.adapter.base.OnItemClickListener
import com.duben.microtribe.utils.TimeRender
import java.util.*

class OrderRecordAdapter(context: Context, invitedData: MutableList<OrderRecordBean.RecordsBean>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

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

    private var mContext: Context = context
    private var mOrderData: List<OrderRecordBean.RecordsBean> = invitedData

    private var mOnItemClickListener: OnItemClickListener? = null

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

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

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

        val holder = viewHolder as ViewHolder
        val data: OrderRecordBean.RecordsBean = mOrderData[position]
        holder.tv_orderrecord_copy.setOnClickListener {
            mOnItemClickListener?.onItemClick(holder.tv_orderrecord_copy, position)
        }
        holder.tv_orderrecord_name.text = data.title
        holder.tv_orderrecord_date.text = "付款时间:" + data.time
        holder.tv_orderrecord_money.text = "￥" + data.balance
        holder.tv_orderrecord_no.text = "订单编号:" + data.orderid
    }

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

    override fun getItemViewType(position: Int): Int {
        if (mOrderData.isEmpty()) {
            return HOLDER_TYPE_EMPTY
        }
        return HOLDER_TYPE_DRAWCASH
    }

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

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tv_orderrecord_name: TextView = view.findViewById(R.id.tv_orderrecord_name)
        val tv_orderrecord_date: TextView = view.findViewById(R.id.tv_orderrecord_date)
        val tv_orderrecord_money: TextView = view.findViewById(R.id.tv_orderrecord_money)
        val tv_orderrecord_no: TextView = view.findViewById(R.id.tv_orderrecord_no)
        val tv_orderrecord_copy: TextView = view.findViewById(R.id.tv_orderrecord_copy)
    }

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

}