package com.bc_chat.im.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import com.bc_chat.bc_base.utils.gone
import com.bc_chat.bc_base.utils.visible
import com.bc_chat.im.R
import com.bc_chat.im.databinding.ItemChartPicBinding
import com.bc_chat.im.databinding.ItemChartTimeBinding
import com.bc_chat.im.entity.TimeMessage
import com.zhaohaoting.framework.abs.adapter.ListToAdapter
import com.zhaohaoting.framework.utils.DensityUtil
import com.zhaohaoting.framework.utils.glid.GlideUtils
import io.rong.message.ImageMessage
import io.rong.message.SightMessage

/**
 * Created by zhaoting on 2019/3/2.
 * Desc：图片视频
 */
class ChatPictureAdapter(context: Context) : ListToAdapter<TimeMessage, List<TimeMessage>>(context) {

    companion object {
        const val ITEM_TIME: Int = 3
        const val ITEM_DEFAULT: Int = 4
    }

    private var itemSize: Int = 0
    private var margin: Int = 0
    var isSelect: Boolean = false

    init {
        val screenWidth = DensityUtil.getScreenWidth(context)
        itemSize = (screenWidth - DensityUtil.dip2px(context, 40f)) / 4
        margin = DensityUtil.dip2px(context, 5f)
    }

    override fun getItemViewTypeHF(realPosition: Int): Int {
        return if (data[realPosition].type == 1) {
            ITEM_TIME
        } else {
            ITEM_DEFAULT
        }
    }

    override fun getItemLayoutId(type: Int): Int =
            if (type == ITEM_TIME) {
                R.layout.item_chart_time
            } else {
                R.layout.item_chart_pic
            }

    override fun onBindViewHolderHF(baseViewHolder: BaseViewHolder<*>?, realPosition: Int, data: TimeMessage?) {
        if (data?.type == 1) {
            val timeBinding = baseViewHolder?.dataBinding as ItemChartTimeBinding
            timeBinding.tvChatTime.text = data.time
        } else {
            val timeBinding = baseViewHolder?.dataBinding as ItemChartPicBinding
            val layoutParams = timeBinding.root.layoutParams as RecyclerView.LayoutParams
            layoutParams.width = itemSize
            layoutParams.height = itemSize
            layoutParams.setMargins(margin, margin, margin, margin)
            timeBinding.chSelect.visibility = if (isSelect) View.VISIBLE else View.GONE
            timeBinding.chSelect.isSelected = data?.isSelect!!
            timeBinding.chSelect.setOnClickListener {
                it.isSelected = !it.isSelected
                data.isSelect = it.isSelected
                selectListener?.onSelect(it.isSelected, data, realPosition)
            }
            if (data.message?.content is ImageMessage) {
                timeBinding.tvDuration.gone()
                timeBinding.ivPlayCover.gone()
                val imageMessage = data.message?.content as ImageMessage
                GlideUtils.loadCenterCropImage(context,  imageMessage.remoteUri.toString(), timeBinding.ivIcCover)
            } else {
                timeBinding.tvDuration.visible()
                timeBinding.ivPlayCover.visible()
                val sightMessage = data.message?.content as SightMessage
                timeBinding.tvDuration.text = "%02d:%02d".format(sightMessage.duration / 60, sightMessage.duration % 60)
                GlideUtils.loadCenterCropImage(context, sightMessage.thumbUri.toString(), timeBinding.ivIcCover)
            }
        }
    }

    var selectListener: OnSelectListener? = null

    interface OnSelectListener {
        fun onSelect(isChecked: Boolean, data: TimeMessage, realPosition: Int)
    }
}
