package com.adealink.weparty.im.session.adapter.viewbinder

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.getActivity
import com.adealink.weparty.im.databinding.LayoutSessionMessageBaseBinding
import com.adealink.weparty.im.databinding.LayoutSessionMessageImageBinding
import com.adealink.weparty.util.formatTimeTo
import com.adealink.weparty.util.goImagePreviewActivity
import com.tencent.qcloud.tuikit.timcommon.interfaces.OnItemClickListener
import com.tencent.qcloud.tuikit.tuichat.bean.message.ImageMessageBean

/**
 * 对照: ImageMessageHolder
 */

class ImageMessageViewHolder(
    rootBinding: LayoutSessionMessageBaseBinding,
    binging: LayoutSessionMessageImageBinding
) : MessageViewHolder<ImageMessageBean, LayoutSessionMessageImageBinding>(
    rootBinding,
    binging
) {

    override fun initView(
        binding: LayoutSessionMessageImageBinding,
        msg: ImageMessageBean,
        onItemClickListener: OnItemClickListener?
    ) {
        binding.ivImg.onClick {
            it.context.getActivity()?.let { act ->
                clickImage(act, msg)
            }
        }
        setImage(binding, msg)
        setMessageTime(binding, msg)
    }

    override fun onBindSelfMessage(
        binding: LayoutSessionMessageImageBinding,
        msg: ImageMessageBean
    ) {
    }

    override fun onBindOtherMessage(
        binding: LayoutSessionMessageImageBinding,
        msg: ImageMessageBean
    ) {
    }

    private fun setImage(
        binding: LayoutSessionMessageImageBinding,
        item: ImageMessageBean
    ) {
//        val imagePath =
//            ChatFileDownloadPresenter.getImagePath(item, ImageMessageBean.IMAGE_TYPE_THUMB)
//        binding.ivImg.setImageURI(File(imagePath).toUri())
        val thumbUrl = item.getImageBean(ImageMessageBean.IMAGE_TYPE_THUMB)?.url
        val originUrl = item.getImageBean(ImageMessageBean.IMAGE_TYPE_ORIGIN)?.url
        if (!thumbUrl.isNullOrEmpty()) {
            binding.ivImg.setImageUrl(thumbUrl)
        } else {
            binding.ivImg.setImageUrl(originUrl)
        }
    }

    private fun setMessageTime(
        binding: LayoutSessionMessageImageBinding,
        item: ImageMessageBean
    ) {
        binding.tvTime.text = formatTimeTo((item.messageTime * 1000), "HH:mm")
    }

    private fun clickImage(activity: Activity, msg: ImageMessageBean) {
        val imageUrl = msg.getImageBean(ImageMessageBean.IMAGE_TYPE_ORIGIN)?.url
        if (imageUrl.isNullOrEmpty()) {
            return
        }
        goImagePreviewActivity(
            activity,
            arrayListOf(imageUrl)
        )
    }
}

class ImageMessageViewBinder(
    onItemClickListener: OnItemClickListener
) : BaseMessageViewBinder<ImageMessageBean, LayoutSessionMessageImageBinding, ImageMessageViewHolder>(
    onItemClickListener
) {


    override fun onCreateMessageHolder(
        rootBinder: LayoutSessionMessageBaseBinding,
        inflater: LayoutInflater,
        parent: ViewGroup
    ): ImageMessageViewHolder {
        return ImageMessageViewHolder(
            rootBinder,
            LayoutSessionMessageImageBinding.inflate(
                inflater,
                parent,
                true
            )
        )
    }
}