package com.zxbw.modulemain.adapter

import android.app.Activity
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zxbw.modulemain.R
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.qy.CouponsEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.RecycleImageView


class HuafeiCouponsAdapter : BaseRecyclerAdapter<CouponsEntity, HuafeiCouponsAdapter.ViewHolder> {
    private var mContext: Activity? = null
    private val listener: OnRecycleItemClickListener<CouponsEntity>
    private var mSelectPos = -1;
    private var originalPrice = "";

    constructor(
        content: Activity?,
        listener: OnRecycleItemClickListener<CouponsEntity>
    ) : super() {
        mContext = content
        this.listener = listener
    }

    fun setSelectPos(pos: Int, originalPrice: String) {
        mSelectPos = pos;
        this.originalPrice = originalPrice;
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val couponsEntity = mList[position]
        if (mSelectPos == position) {
            holder.id_ll_huafei_coupons_item_view?.setBackgroundResource(R.drawable.icon_huafei_select)
            holder.id_tv_huafei_amount?.setTextColor(Color.parseColor("#FFE5C6"))
            holder.id_tv_huafei_desc?.setTextColor(Color.parseColor("#D9D9D9"))
        } else {
            holder.id_ll_huafei_coupons_item_view?.setBackgroundResource(R.drawable.icon_huafei_unselect)
            holder.id_tv_huafei_amount?.setTextColor(Color.parseColor("#894F2A"))
            holder.id_tv_huafei_desc?.setTextColor(Color.parseColor("#999999"))
        }
        holder.id_tv_huafei_amount?.text = "¥ ${couponsEntity.amount}"
        holder.id_tv_huafei_desc?.text = "${couponsEntity.desc}"
        holder.id_ll_huafei_coupons_item_view?.setOnClickListener {
            if (originalPrice == "50") {
                Utils.showToast(mContext, "无法使用此优惠券")
                return@setOnClickListener
            } else if (originalPrice == "100") {
                if (couponsEntity.amount != "5.0") {
                    Utils.showToast(mContext, "无法使用此优惠券")
                    return@setOnClickListener
                }else{
                    if (mSelectPos != position) {
                        mSelectPos = position
                        listener.onItemClick(it, position, couponsEntity)
                    } else {
                        mSelectPos = -1;
                        listener.onItemClick(it, position, null)
                    }
                }
            } else{
                if (mSelectPos != position) {
                    mSelectPos = position
                    listener.onItemClick(it, position, couponsEntity)
                } else {
                    mSelectPos = -1;
                    listener.onItemClick(it, position, null)
                }
            }

            notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(AppContext.get())
                .inflate(R.layout.item_layout_huafei_coupons_view, parent, false)
        )
    }

    class ViewHolder : RecyclerView.ViewHolder {
        var id_ll_huafei_coupons_item_view: LinearLayout?
        var id_tv_huafei_amount: TextView?
        var id_tv_huafei_desc: TextView?

        constructor(
            itemView: View
        ) : super(itemView) {
            id_ll_huafei_coupons_item_view =
                itemView.findViewById<LinearLayout>(R.id.id_ll_huafei_coupons_item_view)
            id_tv_huafei_amount = itemView.findViewById<TextView>(R.id.id_tv_huafei_amount)
            id_tv_huafei_desc = itemView.findViewById<TextView>(R.id.id_tv_huafei_desc)
        }
    }
}