package com.bc_chat.im.adapter

import android.content.Context
import android.net.Uri
import android.os.AsyncTask
import android.view.View
import android.widget.ImageView
import com.bc_chat.bc_base.adapter.ModeListAdapter
import com.bc_chat.bc_base.entity.Collect
import com.bc_chat.bc_base.entity.CollectMessage
import com.bc_chat.bc_base.entity.ListModel
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.im.R
import com.bc_chat.im.databinding.ItemCollectImageBinding
import com.bc_chat.im.databinding.ItemCollectSightBinding
import com.bc_chat.im.databinding.ItemCollectTextBinding
import com.bc_chat.im.databinding.ItemCollectVoiceBinding
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter
import com.zhaohaoting.framework.utils.glid.GlideUtils
import io.rong.message.ImageMessage
import io.rong.message.SightMessage
import io.rong.message.TextMessage
import io.rong.message.VoiceMessage
import java.lang.ref.WeakReference
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by zhaoting on 2019/3/2.
 * Desc：收藏
 */
class CollectAdapter(context: Context) : ModeListAdapter<CollectMessage, ListModel<CollectMessage>>(context, ListModel()) {

    companion object {
        const val ITEM_TEXT: Int = 3
        const val ITEM_IMAGE: Int = 4
        const val ITEM_VOICE: Int = 5
        const val ITEM_SIGHT: Int = 6
    }

    val format = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)

    override fun getItemViewTypeHF(realPosition: Int): Int = when (mDataList[realPosition].type) {
        Const.CollectionType.TYPE_TEXT_MESSAGE -> ITEM_TEXT
        Const.CollectionType.TYPE_IMAGE_MESSAGE -> ITEM_IMAGE
        Const.CollectionType.TYPE_VOICE_MESSAGE -> ITEM_VOICE
        Const.CollectionType.TYPE_SIGHT_MESSAGE -> ITEM_SIGHT
        else -> ITEM_TEXT
    }

    override fun getItemLayoutId(type: Int): Int = when (type) {
        ITEM_TEXT -> R.layout.item_collect_text
        ITEM_IMAGE -> R.layout.item_collect_image
        ITEM_VOICE -> R.layout.item_collect_voice
        ITEM_SIGHT -> R.layout.item_collect_sight
        else -> R.layout.item_collect_text
    }

    override fun onBindViewHolderHF(baseViewHolder: AbsRecyclerAdapter.BaseViewHolder<*>?, realPosition: Int, data: CollectMessage?) {
        when (data?.type) {
            Const.CollectionType.TYPE_TEXT_MESSAGE -> {
                val textBinding = baseViewHolder?.dataBinding as ItemCollectTextBinding
                val content = data.content as TextMessage
                textBinding.cetName.text = content.userInfo.name
                textBinding.tvContent.text = content.content
                textBinding.cetTime.text = format.format(data.c_time?.toLong()?.times(1000))
            }
            Const.CollectionType.TYPE_IMAGE_MESSAGE -> {
                val imageBinding = baseViewHolder?.dataBinding as ItemCollectImageBinding
                val content = data.content as ImageMessage
                imageBinding.cetName.text = content.userInfo.name
                imageBinding.cetTime.text = format.format(data.c_time?.toLong()?.times(1000))
                GlideUtils.loadCenterCropImage(context, content.remoteUri, imageBinding.ivCover)
                imageBinding.ivCover.setOnClickListener {
                    collectClickListener?.onClick(imageBinding.ivCover, imageBinding.ivCover, data)
                }
            }
            Const.CollectionType.TYPE_VOICE_MESSAGE -> {
                val voiceBinding = baseViewHolder?.dataBinding as ItemCollectVoiceBinding
                val content = data.content as VoiceMessage
                voiceBinding.cetName.text = content.userInfo.name
                voiceBinding.cetTime.text = format.format(data.c_time?.toLong()?.times(1000))
                voiceBinding.tvVoiceDuration.text = "%02d:%02d".format(content.duration / 60, content.duration % 60)
                voiceBinding.progress.max = content.duration
                voiceBinding.ivSwitch.setOnClickListener {
                    collectClickListener?.onClick(voiceBinding.ivSwitch, voiceBinding.ivSwitch, data)
                }
            }
            Const.CollectionType.TYPE_SIGHT_MESSAGE -> {
                val sightBinding = baseViewHolder?.dataBinding as ItemCollectSightBinding
                val content = data.content as SightMessage
                sightBinding.cetName.text = content.userInfo.name
                sightBinding.cetTime.text = format.format(data.c_time?.toLong()?.times(1000))
                sightBinding.tvSightDuration.text = "%02d:%02d".format(content.duration / 60, content.duration % 60)
                sightBinding.tvSightLength.text = "%.2fM".format(content.size.div(1024 * 1024).toFloat())
                GlideUtils.loadCenterCropImage(context, content.thumbUri, sightBinding.ivCover)
                sightBinding.ivSwitch.setOnClickListener {
                    collectClickListener?.onClick(sightBinding.ivSwitch, sightBinding.ivSwitch, data)
                }
            }
        }
    }

    /**
     * 异步解析语音和视频缩略图
     */
    private class ParseTask(imageView: ImageView, context: Context) : AsyncTask<Collect, Int, Uri>() {

        var imageView: WeakReference<ImageView> = WeakReference(imageView)
        var context: WeakReference<Context> = WeakReference(context)

        override fun doInBackground(vararg params: Collect?): Uri? {
            val collection = params[0]
            if (collection?.type == Const.CollectionType.TYPE_SIGHT_MESSAGE) {  //视频解析 需要返回uri
                return collection.parseSight(context.get()!!)
            } else if (collection?.type == Const.CollectionType.TYPE_VOICE_MESSAGE) {  //语音解析无需返回值
                collection.parseVoice(context.get()!!)
            }
            return null
        }

        override fun onPostExecute(result: Uri?) {
            if (result != null) {
                GlideUtils.loadCenterCropImage(context.get(), result, imageView.get())
            }
        }
    }

    private var collectClickListener: OnCollectClickListener? = null

    fun setOnCollectClickListener(collectClickListener: OnCollectClickListener) {
        this.collectClickListener = collectClickListener;
    }

    interface OnCollectClickListener {
        fun onClick(view: View, updateView: View, data: CollectMessage?)
    }
}
