package com.bc_chat.contacts.view

import android.annotation.SuppressLint
import android.content.Context
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.bc_chat.bc_base.entity.contact.Dynamic
import com.bc_chat.bc_base.utils.gone
import com.bc_chat.bc_base.utils.visible
import com.bc_chat.circle.adapetr.DynamicAdapter
import com.bc_chat.contacts.R
import com.bc_chat.contacts.adapter.DynamicImageAdapter
import com.zhaohaoting.framework.imagepicker.ImagePicker
import com.zhaohaoting.framework.imagepicker.bean.MediaItem
import com.zhaohaoting.framework.utils.Check
import com.zhaohaoting.framework.utils.format.MsTransformation
import com.zhaohaoting.framework.utils.glid.GlideUtils

/**
 * author：Created by zhaohaoting on 2019/8/27
 * email：526309416@qq.com
 * desc：
 */
class DynamicItemView @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 tvTitle: TextView? = null
    private var tvDesc: TextView? = null
    private var rvImage: RecyclerView? = null
    private var tvCommentCount: TextView? = null
    private var dynamicImageAdapter: DynamicImageAdapter? = null
    private var realPosition: Int? = 0
    private var setTop: View? = null
    private var userTag: TextView? = null

    init {
        val inflater = LayoutInflater.from(context)
        val view = inflater.inflate(R.layout.dynamic_view_layout, this, true)
        ivPhoto = view.findViewById(R.id.iv_photo)
        tvNick = view.findViewById(R.id.tv_nick)
        tvTime = view.findViewById(R.id.tv_time)
        tvTitle = view.findViewById(R.id.tv_title)
        tvDesc = view.findViewById(R.id.tv_desc)
        rvImage = view.findViewById(R.id.rv_image)
        setTop = view.findViewById(R.id.setTop)
        userTag = view.findViewById(R.id.user_tag)

        tvCommentCount = view.findViewById(R.id.tv_comment_count)
        rvImage?.setHasFixedSize(true)
        rvImage?.isNestedScrollingEnabled = false
        rvImage?.layoutManager = GridLayoutManager(context, 3)
        dynamicImageAdapter = DynamicImageAdapter(context)
        rvImage?.adapter = dynamicImageAdapter
    }

    @SuppressLint("ClickableViewAccessibility")
    fun updateView(dynamic: Dynamic, realPosition: Int) {
        this.realPosition = realPosition
        GlideUtils.loadCircleImage(context, dynamic.photo, ivPhoto)
        tvNick?.text = dynamic.nickname
        tvTime?.text = MsTransformation.formatTimesBefore1(dynamic.c_time * 1000, System.currentTimeMillis())
        tvTitle?.text = dynamic.title
        tvDesc?.text = dynamic.content
        tvCommentCount?.text = dynamic.comments_count
        if (dynamic.is_top.equals("1")) {
            setTop.visible()
        } else {
            setTop.gone()
        }
        if (Check.isEmpty(dynamic.label)) {
            userTag.gone()
        } else {
            userTag.visible()
            userTag?.text = dynamic.label
        }

        if (!dynamic.imgs.isNullOrEmpty()) {
            val split = dynamic.imgs?.split(",")
            val mediaList = arrayListOf<MediaItem>()
            split?.map {
                mediaList.add(MediaItem(it, ImagePicker.MEDIA_TYPE_IMAGE))
            }
            dynamicImageAdapter?.notifyDataChanged(mediaList, true)
        } else {
            dynamicImageAdapter?.notifyDataChanged(null, true)
        }
        onItemClickListener?.let { dynamicClick ->
            dynamicImageAdapter?.addOnItemClickListener { vh, position ->
                dynamicClick.itemImageClick(dynamicImageAdapter!!.data, position, realPosition)
            }
            ivPhoto?.setOnClickListener {
                dynamicClick.itemPhotoClick(dynamic.uid!!)
            }
            tvNick?.setOnClickListener {
                dynamicClick.itemPhotoClick(dynamic.uid!!)
            }
            rvImage?.setOnTouchListener { v, event ->
                if (event.action == MotionEvent.ACTION_UP) {
                    //模拟父控件的点击
                    dynamicClick.itemImageClick(dynamicImageAdapter!!.data, -1, realPosition)
                }
                return@setOnTouchListener false
            }
        }
    }

    private var onItemClickListener: DynamicAdapter.OnDynamicClickListener? = null

    fun setDynamicClickListener(onItemClickListener: DynamicAdapter.OnDynamicClickListener) {
        this.onItemClickListener = onItemClickListener
    }
}