package com.bc_chat.circle.adapetr

import android.content.Context
import com.bc_chat.bc_base.entity.contact.Comment
import com.bc_chat.bc_base.entity.contact.CommentModel
import com.bc_chat.contacts.R
import com.bc_chat.contacts.databinding.ItemCommentLayoutBinding
import com.bc_chat.contacts.view.CommentView
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter

/**
 * author：Created by zhaohaoting on 2019/8/28
 * email：526309416@qq.com
 * desc：
 */
class CommentAdapter(context: Context, var commentModel: CommentModel = CommentModel()) : AbsRecyclerAdapter<Comment, CommentModel>(context, commentModel.commentsList) {

    override fun getItemLayoutId(type: Int): Int = R.layout.item_comment_layout

    override fun getData(): CommentModel {
        return commentModel
    }

    override fun onBindViewHolderHF(baseViewHolder: AbsRecyclerAdapter.BaseViewHolder<*>?, realPosition: Int, data: Comment?) {
        val binding = baseViewHolder?.dataBinding as ItemCommentLayoutBinding
        binding.commentView.updateView(data!!)
        binding.commentView.setChildRefreshListener(object : CommentView.ChildRefreshListener {
            override fun deleteItemClik() {
                childItemClickListener?.deleteItemClik(realPosition)
            }

            override fun deleteChildItemClik(position: Int) {
                childItemClickListener?.deleteChildItemClik(realPosition, position)
            }

            override fun childItemClik(position: Int) {
                childItemClickListener?.childItemClick(realPosition, position)
            }

            override fun childRefresh() {
                notifyDataSetChanged()
            }
        })
        onItemViewClickListener?.let { itemClick ->
            binding.commentView.setOnItemViewClickListener {
                itemClick.onItemClick(baseViewHolder, it, realPosition)
            }
        }
    }

    override fun notifyDataChanged(data: CommentModel?, isRefresh: Boolean) {
        data?.let {
            if (isRefresh) {
                commentModel.commentsList?.clear()
            }
            commentModel.commentsList?.addAll(data.commentsList!!)
            notifyDataSetChanged()
        }
    }

    fun setChildItemClickListener(childItemClickListener: OnChildItemClickListener) {
        this.childItemClickListener = childItemClickListener
    }

    private var childItemClickListener: OnChildItemClickListener? = null

    public interface OnChildItemClickListener {
        fun childItemClick(position: Int, childPosition: Int)
        fun deleteItemClik(position: Int)
        fun deleteChildItemClik(position: Int, childPosition: Int)
    }

}