package com.mints.goodmoney.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.mints.goodmoney.R
import com.mints.goodmoney.mvp.model.DrawcashRecordBean
import com.mints.goodmoney.ui.adapter.listener.OnItemClickListener
import com.mints.goodmoney.utils.TimeRender
import java.util.*

class DrawcashRecordAdapter(context: Context, invitedData: MutableList<DrawcashRecordBean.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 mDarwcashData: List<DrawcashRecordBean.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, parent, false)
            return EmptyHolder(emptyView)
        }

        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_list_drawcashrecord, 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: DrawcashRecordBean.RecordsBean = mDarwcashData[position]

        holder.tv_drawcashrecord_applyno.text = "订单号:" + data.cashId
        viewHolder.tv_drawcashrecord_date.text = TimeRender.formatDate(Date(data.createTime), TimeRender.DEFAULT_FORMAT_SHOW)
        viewHolder.tv_drawcashrecord_money.text = "￥" + String.format("%.2f", data.cash) + "元"
        //            viewHolder.tv_drawcashrecord_account.setText("支付宝账户:" + data.getAccount());
        if (TextUtils.equals(data.payChannel, "ALIPAY")) {
            viewHolder.tv_drawcashrecord_account.text = "提现账户：支付宝"
            viewHolder.tv_drawcashrecord_account.setTextColor(ContextCompat.getColor(mContext, R.color.water_color))
        } else {
            viewHolder.tv_drawcashrecord_account.text = "提现账户：微信"
            viewHolder.tv_drawcashrecord_account.setTextColor(ContextCompat.getColor(mContext, R.color.loan_green))
        }
        val errormsg: String = data.errormsg
        if (TextUtils.isEmpty(errormsg)) {
            viewHolder.tv_drawcashrecord_fail.visibility = View.GONE
        } else {
            viewHolder.tv_drawcashrecord_fail.visibility = View.VISIBLE
            viewHolder.tv_drawcashrecord_fail.text = "失败原因:$errormsg"
        }
        when (data.status) {
            0, 1 -> {
                viewHolder.tv_drawcashrecord_status.text = "处理中"
                viewHolder.tv_drawcashrecord_status.setTextColor(ContextCompat.getColor(mContext, R.color.text_font))
            }
            2 -> {
                viewHolder.tv_drawcashrecord_status.text = "已提现"
                viewHolder.tv_drawcashrecord_status.setTextColor(ContextCompat.getColor(mContext, R.color.main_nor_color))
            }
            3 -> {
                viewHolder.tv_drawcashrecord_status.text = "提现失败"
                viewHolder.tv_drawcashrecord_status.setTextColor(ContextCompat.getColor(mContext, R.color.tv_message_recente_money))
            }
            else -> {
                viewHolder.tv_drawcashrecord_status.text = "未知"
                viewHolder.tv_drawcashrecord_status.setTextColor(ContextCompat.getColor(mContext, R.color.text_font))
            }
        }
    }

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

    override fun getItemViewType(position: Int): Int {
        if (mDarwcashData.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_drawcashrecord_applyno: TextView = view.findViewById(R.id.tv_drawcashrecord_applyno)
        val tv_drawcashrecord_date: TextView = view.findViewById(R.id.tv_drawcashrecord_date)
        val tv_drawcashrecord_money: TextView = view.findViewById(R.id.tv_drawcashrecord_money)
        val tv_drawcashrecord_status: TextView = view.findViewById(R.id.tv_drawcashrecord_status)
        val tv_drawcashrecord_account: TextView = view.findViewById(R.id.tv_drawcashrecord_account)
        val tv_drawcashrecord_fail: TextView = view.findViewById(R.id.tv_drawcashrecord_fail)
    }

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

}