package com.bc_chat.contacts.view

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.bc_chat.bc_base.entity.contact.Comment
import com.bc_chat.bc_base.utils.AppConfig
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.bc_base.utils.gone
import com.bc_chat.bc_base.utils.visible
import com.bc_chat.circle.adapetr.ChildeCommentAdapter
import com.bc_chat.contacts.R
import com.zhaohaoting.framework.utils.Check
import com.zhaohaoting.framework.utils.format.MsTransformation
import com.zhaohaoting.framework.utils.glid.GlideUtils
import java.text.FieldPosition

/**
 * author：Created by zhaohaoting on 2019/8/27
 * email：526309416@qq.com
 * desc：
 */
class CommentView @JvmOverloads constructor(context: Context, attributes: AttributeSet? = null, defStyleAttr: Int = 0) : RelativeLayout(context, attributes, defStyleAttr) {

    private var ivPhoto: ImageView? = null
    private var tvNick: TextView? = null
    private var tvTime: TextView? = null
    private var tvContent: TextView? = null
    private var replyCount: TextView? = null
    private var rvComment: RecyclerView? = null
    private var ivComment: ImageView? = null
    private var commentLayout: LinearLayout? = null
    private var dynamicImageAdapter: ChildeCommentAdapter? = null
    private var clickListener: OnClickListener? = null
    private var subList: List<Comment>? = null
    private val MINE_ITEM = 2
    private var comment: Comment? = null
    private var userTag: TextView? = null
    private var tvDelete: TextView? = null


    init {
        val inflater = LayoutInflater.from(context)
        val view = inflater.inflate(R.layout.comment_view, this, true)
        ivPhoto = view.findViewById(R.id.iv_photo)
        tvNick = view.findViewById(R.id.tv_nick)
        tvTime = view.findViewById(R.id.tv_time)
        ivComment = view.findViewById(R.id.iv_comment)
        tvContent = view.findViewById(R.id.tv_content)
        commentLayout = view.findViewById(R.id.comment_layout)
        rvComment = view.findViewById(R.id.rv_comment)
        replyCount = view.findViewById(R.id.reply_count)
        userTag = view.findViewById(R.id.user_tag)
        tvDelete = view.findViewById(R.id.tv_delete)
        rvComment?.setHasFixedSize(true)
        rvComment?.isNestedScrollingEnabled = false
        rvComment?.layoutManager = LinearLayoutManager(context)
        dynamicImageAdapter = ChildeCommentAdapter(context)
        rvComment?.adapter = dynamicImageAdapter
        replyCount?.setOnClickListener { v ->
            comment?.let {
                val expand = !it.isExpand
                it.isExpand = expand
                val data = dynamicImageAdapter?.data as MutableList
                if (expand) {
                    replyCount.gone()
                    data.addAll(subList!!)
                    dynamicImageAdapter?.notifyItemRangeInserted(MINE_ITEM, subList!!.size)
                } else {
                    data.removeAll(subList!!)
                    replyCount?.text = "共%d条回复，点此查看>".format(it.list?.size)
                    dynamicImageAdapter?.notifyItemRangeRemoved(MINE_ITEM, subList!!.size)
                }
                dynamicImageAdapter?.notifyDataSetChanged()
                childRefreshListener?.childRefresh()
            }
        }
        dynamicImageAdapter?.addOnItemClickListener { vh, position ->
            childRefreshListener?.childItemClik(position)
        }
        ivComment?.setOnClickListener {
            clickListener?.onClick(it)
        }
        ivPhoto?.setOnClickListener {
            clickListener?.onClick(it)
        }
        tvNick?.setOnClickListener {
            clickListener?.onClick(it)
        }
        tvContent?.setOnLongClickListener {
            if (comment?.uid.equals(AppConfig.instance.getUser().userId) || AppConfig.instance.getUser().userId.equals(Const.ADMIN_ID)) {
                childRefreshListener?.deleteItemClik()
            }
            return@setOnLongClickListener false
        }
        tvDelete?.setOnClickListener {
            childRefreshListener?.deleteItemClik()
        }
    }

    fun setChildRefreshListener(childRefreshListener: ChildRefreshListener) {
        dynamicImageAdapter?.setChildRefreshListener(childRefreshListener)
        this.childRefreshListener = childRefreshListener
    }

    private var childRefreshListener: ChildRefreshListener? = null

    interface ChildRefreshListener {
        fun childRefresh()

        fun childItemClik(position: Int)

        fun deleteItemClik()

        fun deleteChildItemClik(position: Int)

    }

    fun updateView(comment: Comment) {
        this.comment = comment;
        GlideUtils.loadCircleImage(context, comment.photo, ivPhoto)
        tvNick?.text = comment.nickname
        tvTime?.text = MsTransformation.formatTimesBefore1(comment.c_time * 1000, System.currentTimeMillis())
        tvContent?.text = comment.content
        if (Check.isEmpty(comment?.label)) {
            userTag.gone()
        } else {
            userTag.visible()
            userTag?.text = comment?.label
        }
        val secondaryList = comment.list
//        if (comment?.uid.equals(AppConfig.instance.getUser().userId) || AppConfig.instance.getUser().userId.equals(Const.ADMIN_ID)) {
//            tvDelete.visible()
//        } else {
//            tvDelete.gone()
//        }
        if (comment.isExpand) {
            rvComment.visible()
            replyCount.gone()
            dynamicImageAdapter?.notifyDataChanged(secondaryList, true)
        } else {
            if (secondaryList!!.size > MINE_ITEM) {
                rvComment.visible()
                replyCount.visible()
                replyCount?.text = "共%d条回复，点此查看>".format(secondaryList.size)
                subList = secondaryList.subList(MINE_ITEM, secondaryList.size)
                dynamicImageAdapter?.notifyDataChanged(secondaryList.subList(0, MINE_ITEM), true)
            } else if (secondaryList!!.isNotEmpty()) {
                rvComment.visible()
                replyCount.gone()
                dynamicImageAdapter?.notifyDataChanged(secondaryList, true)
            } else {
                rvComment.gone()
                replyCount.gone()
                dynamicImageAdapter?.notifyDataChanged(secondaryList, true)
            }
        }
    }


    fun setOnItemViewClickListener(listener: (view: View) -> Unit) {
        clickListener = OnClickListener {
            listener(it)
        }
    }
}