package com.zxhl.main.page.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
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.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.chat.ChatRoomEntity
import com.zxhl.cms.utils.*
import com.zxhl.main.R

class RoomAdapter : BaseRecyclerAdapter<ChatRoomEntity, RecyclerView.ViewHolder> {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<ChatRoomEntity>
    private val HEAD = 1009
    private val CONTENT = 1000
    val LOADMORE = 1001
    private var mListImgView: MutableList<ImageView> = ArrayList()
    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<ChatRoomEntity>
    ) : super() {
        this.listener = listener
        mContext = content
    }


    private var isLoadMore = false
    fun setLoadMore(isBoolean: Boolean) {
        isLoadMore = isBoolean
    }

    fun getLoadMore(): Boolean = isLoadMore
    override fun getItemCount(): Int {
        val size = mList.size + 1
        if (size > 0) {
            return if (isLoadMore) {
                mList.size + 2
            } else {
                mList.size + 1
            }
        } else {
            return size
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (isLoadMore) {
            if (position == mList.size + 1) {
                return LOADMORE
            } else {
                if (position == 0) {

                    return HEAD
                }

                return CONTENT
            }
        } else {
            if (position == 0) {

                return HEAD
            }

            return CONTENT
        }
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun onCreateViewHolder(holder: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (isLoadMore) {
            if (viewType == LOADMORE) {
                return LoadMoreHolder(
                    LayoutInflater.from(mContext).inflate(R.layout.item_load_more, holder, false)
                )
            } else {
                if (viewType == HEAD) {

                    return HomeHeadHolder(
                        LayoutInflater.from(mContext)
                            .inflate(R.layout.item_layout_room_head, holder, false),
                    )
                } else {
                    return ViewHolder(
                        LayoutInflater.from(holder.context)
                            .inflate(R.layout.item_layout_room, holder, false)
                    )
                }
            }
        } else {
            if (viewType == HEAD) {
                return HomeHeadHolder(
                    LayoutInflater.from(mContext)
                        .inflate(R.layout.item_layout_room_head, holder, false),
                )
            } else {
                return ViewHolder(
                    LayoutInflater.from(holder.context)
                        .inflate(R.layout.item_layout_room, holder, false)
                )
            }

        }


    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is HomeHeadHolder) {

        } else if (holder is ViewHolder) {
            val data = mList[position - 1]
            if (position % 6 == 0) {
                holder.id_item.setBackgroundResource(R.drawable.room_bg_six)
            } else if (position % 6 == 1) {
                holder.id_item.setBackgroundResource(R.drawable.room_bg_one)
            } else if (position % 6 == 2) {
                holder.id_item.setBackgroundResource(R.drawable.room_bg_two)
            } else if (position % 6 == 3) {
                holder.id_item.setBackgroundResource(R.drawable.room_bg_three)
            } else if (position % 6 == 4) {
                holder.id_item.setBackgroundResource(R.drawable.room_bg_four)
            } else if (position % 6 == 5) {
                holder.id_item.setBackgroundResource(R.drawable.room_bg_five)
            }
            mListImgView?.add(holder.id_img_one)
            mListImgView?.add(holder.id_img_two)
            mListImgView?.add(holder.id_img_three)
            for (i in 0 until (data.roomUsersImages?.size ?: 0)) {
                if (i < mListImgView.size) {
                    Glide.with(AppContext.get()).load(data?.roomUsersImages!![i])
                        .apply(RequestOptions.bitmapTransform(CircleCrop()))
                        .into(mListImgView[i])
                }

            }

            holder.id_tv_type.text = data.category
            holder.id_tv_topic.text = data.title
            holder.id_tv_guanzhong_nums.text = data.roomUsersNum.toString()
        } else if (holder is LoadMoreHolder) {
            Glide.with(AppContext.get()).asGif().load(R.drawable.load_more).into(holder.mImg)
        }
    }


    inner class ViewHolder : RecyclerView.ViewHolder {

        var id_tv_type: TextView
        var id_tv_topic: TextView
        var id_tv_guanzhong_nums: TextView
        var id_item: LinearLayout
        var id_img_one: ImageView
        var id_img_two: ImageView
        var id_img_three: ImageView

        constructor(itemView: View) : super(itemView) {

            id_tv_type = itemView.findViewById(R.id.id_tv_type)
            id_tv_topic = itemView.findViewById(R.id.id_tv_topic)
            id_tv_guanzhong_nums = itemView.findViewById(R.id.id_tv_guanzhong_nums)
            id_img_one = itemView.findViewById(R.id.id_img_one)
            id_img_two = itemView.findViewById(R.id.id_img_two)
            id_img_three = itemView.findViewById(R.id.id_img_three)
            id_item = itemView.findViewById(R.id.id_item)
            id_item.setOnClickListener(object : NoDoubleClickListener() {
                override fun onNoDoubleClick(v: View?) {
                    val pos = adapterPosition
                    if (pos >= 0 && pos <= mList.size) {
                        listener.onItemClick(v, pos - 1, mList[pos - 1])
                    }
                }
            })

        }
    }

    class HomeHeadHolder : RecyclerView.ViewHolder {


        constructor(
            itemView: View
        ) : super(itemView) {

        }

    }

    inner class LoadMoreHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mImg: ImageView

        init {
            mImg = itemView.findViewById(R.id.id_item_more_img)
        }
    }


}