package com.xty.base.adapter

import android.widget.ImageView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.tamsiree.rxkit.RxTimeTool
import com.xty.base.R
import com.xty.common.*
import com.xty.common.picture.PictureUtils
import com.xty.network.model.ChatBean
import de.hdodenhof.circleimageview.CircleImageView

class ChatMSgAdapter(var chartImage:String="") : BaseMultiItemQuickAdapter<ChatBean, BaseViewHolder>() {
    val yestory by lazy{RxTimeTool.getYestoryDate("yyyy-MM-dd")}  //昨天的日期

    init {
        //1自己  2 别人
        addItemType(1, R.layout.item_right_msg)
        addItemType(2, R.layout.item_left_msg)


    }

    override fun convert(holder: BaseViewHolder, item: ChatBean) {
     var previousTime= if(  holder.layoutPosition!=0) data[holder.layoutPosition-1].create_time else ""
        var timeStatus=showDayTime(previousTime,item)
        when(item.itemType){
            1->{
                holder.setGone(R.id.mCard,timeStatus.isEmpty()).setText(R.id.mTime,timeStatus)
                holder.getView<CircleImageView>(R.id.mRightImage).setImageUser(context,Const.CHAT_IMAGE)
                when(item.process_type){
                    0->holder.setText(R.id.mRightMsg,item.process_detail).setVisible(R.id.mRightMsg,true).setGone(R.id.mAudioRightBg,true).setGone(R.id.mVideoRightBg,true)
                    1->{
                        if(item.isLocalFIle){
                            holder.getView<ImageView>(R.id.mRightPhoto).setImage(context,item.file_path)
                        }else{
                            holder.getView<ImageView>(R.id.mRightPhoto)
                                .apply {
                                    setImage(context,getImageUrl(item.file_path))
                                }
                        }
                        holder.setVisible(R.id.mRightMsg,false).setGone(R.id.mVideoRightBg,false).setGone(R.id.mAudioRightBg,true).setGone(R.id.mPlay,true)
                    }
                    2->{
                        holder.setText(R.id.mAudioTime, "${item.fileLength}''")
                        holder.setVisible(R.id.mRightMsg,false).setGone(R.id.mVideoRightBg,true).setGone(R.id.mAudioRightBg,false).setGone(R.id.mPlay,false)
                    }
                    3->{
                        var view=holder.getView<ImageView>(R.id.mRightPhoto)

                        if(item.isLocalFIle){
                            PictureUtils.getVideoThumb(item.file_path,view)
                        }else{
                            view.getThumbVideo(context, getImageUrl(item.file_path))
                        }
                        holder.setVisible(R.id.mRightMsg,false).setGone(R.id.mVideoRightBg,false).setGone(R.id.mAudioRightBg,true).setGone(R.id.mPlay,false)
                    }
                }
            }
            2->{
                holder.setGone(R.id.mCard,timeStatus.isEmpty()).setText(R.id.mTime,timeStatus)
                holder.getView<CircleImageView>(R.id.mLeftImage).setImageUser(context,chartImage)
                when(item.process_type){
                    0->holder.setText(R.id.mLeftMsg,item.process_detail).setVisible(R.id.mLeftMsg,true).setGone(R.id.mAudioLeftBg,true).setGone(R.id.mVideoLeftBg,true)
                    1->{
                        if(item.isLocalFIle){
                            holder.getView<ImageView>(R.id.mLeftPhoto).setImage(context,item.file_path)
                        }else{
                            holder.getView<ImageView>(R.id.mLeftPhoto).apply {
                                setImage(context, getImageUrl(item.file_path))
                            }
                        }
                        holder.setVisible(R.id.mLeftMsg,false).setGone(R.id.mVideoLeftBg,false).setGone(R.id.mAudioLeftBg,true).setGone(R.id.mPlay,true)
                    }
                    2->{
                        holder.setText(R.id.mAudioTime, "${item.fileLength}''")
                        holder.setVisible(R.id.mLeftMsg,false).setGone(R.id.mAudioLeftBg,false).setGone(R.id.mAudioLeftBg,false).setGone(R.id.mPlay,false)
                    }
                    3->{
                        var view=holder.getView<ImageView>(R.id.mLeftPhoto)
                        if(item.isLocalFIle){
                            PictureUtils.getVideoThumb(item.file_path,view)
                        }else{
                            view.getThumbVideo(context, getImageUrl(item.file_path))
                        }
                        holder.setVisible(R.id.mLeftMsg,false).setGone(R.id.mVideoLeftBg,false).setGone(R.id.mAudioLeftBg,true).setGone(R.id.mPlay,false)
                    }

                }
            }

        }
    }

    private fun showDayTime(previousTime:String,item: ChatBean) :String{
        if(item.create_time.isNullOrEmpty()){
            return ""
        }else {
            var time=item.create_time.substring(0,10)
            if(previousTime.isNullOrEmpty()){
                return time
            }
            var oldTime=previousTime.substring(0,10)
            if(oldTime==time){
                return ""
            }else {
                if(time==yestory){
                    return  "昨天"
                }else {
                    return  time
                }

            }

        }

    }
}