package com.ym.chat.adapter

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.ym.chat.R
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.chat.VisitorEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.BlurTransformation
import com.zxhl.cms.widget.CircleImageView

class VisitorApdater : BaseRecyclerAdapter<VisitorEntity, VisitorApdater.ViewHolder> {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<VisitorEntity>
    private var free: Int = 0

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<VisitorEntity>,
        free: Int
    ) : super() {
        this.listener = listener
        mContext = content
        this.free = free
    }

    override fun onCreateViewHolder(holder: ViewGroup, p1: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(holder.context).inflate(R.layout.item_layout_visitor, holder, false)
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mList[position]
        holder.id_tv_name.text = data.nickname
        holder.id_tv_look_time.text = data.dateStr + " 看过你"
        if (TextUtils.equals(data.chatStatus, "0")) {
            holder.id_tv_chat.text = "打招呼"
            holder.id_tv_chat.setTextColor(AppContext.get().resources.getColor(R.color.white))
            holder.id_tv_chat.setBackgroundResource(R.drawable.shape_aa60ff_r3)
        } else {
            holder.id_tv_chat.text = "继续聊天"
            holder.id_tv_chat.setBackgroundResource(R.drawable.shape_aa60ff_storke_r5)
            holder.id_tv_chat.setTextColor(AppContext.get().resources.getColor(R.color.color_aa60ff))
        }
        holder.id_tv_chat?.setOnClickListener {
            listener.onItemClick(it,position,data)
        }
        holder.id_rl_item?.setOnClickListener {
            listener.onItemClick(it,position,data)
        }
        if (free == 2) {
            holder.id_tv_name.visibility = View.INVISIBLE
            holder.id_tv_chat.visibility = View.INVISIBLE
            holder.id_img_mohu_tv.visibility = View.VISIBLE
            if (!TextUtils.isEmpty(data.headImage)) {
                Glide.with(AppContext.get()).load(data.headImage)
//                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                    .apply(RequestOptions.bitmapTransform(BlurTransformation(25)))
                    .into(holder.id_img_head)
            } else {
                holder.id_img_head.setImageResource(R.drawable.icon_user_photo_default)
            }
            if (data.nickname.isNotEmpty()) {
                when (data.nickname.length) {
                    1 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_1)
                    }
                    2 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_2)
                    }
                    3 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_3)
                    }
                    4 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_4)
                    }
                    5 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_5)
                    }
                    6 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_6)
                    }
                    7 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_7)
                    }
                    8 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_8)
                    }
                    9 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_9)
                    }
                    10 -> {
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_10)
                    }
                    else ->
                        holder.id_img_mohu_tv.setImageResource(R.drawable.font_mohu_10)

                }
            }
        } else {
            holder.id_tv_name.visibility = View.VISIBLE
            holder.id_tv_chat.visibility = View.VISIBLE
            holder.id_img_mohu_tv.visibility = View.GONE
            if (!TextUtils.isEmpty(data.headImage)) {
                Glide.with(AppContext.get()).load(data.headImage)
                    .apply(RequestOptions.bitmapTransform(CircleCrop()))
                    //        .apply(RequestOptions.bitmapTransform(BlurTransformation(25)))
                    .into(holder.id_img_head)
            } else {
                holder.id_img_head.setImageResource(R.drawable.icon_user_photo_default)
            }
        }

    }

    inner class ViewHolder : RecyclerView.ViewHolder {


        var id_tv_name: TextView
        var id_tv_look_time: TextView
        var id_tv_chat: TextView
        var id_img_mohu_tv: ImageView
        var id_img_head: CircleImageView
        var id_rl_item:RelativeLayout
        constructor(itemView: View) : super(itemView) {
            id_img_head = itemView.findViewById(R.id.id_img_head)
            id_img_mohu_tv = itemView.findViewById(R.id.id_img_mohu)
            id_tv_name = itemView.findViewById(R.id.id_tv_name)
            id_tv_look_time = itemView.findViewById(R.id.id_tv_look_time)
            id_tv_chat = itemView.findViewById(R.id.id_tv_chat)
            id_rl_item = itemView.findViewById(R.id.id_rl_item)

        }
    }

}