package com.daya.live_teaching.im.provider;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import io.rong.imkit.R;

import java.util.List;

import io.rong.imkit.IMCenter;
import io.rong.imkit.conversation.messgelist.provider.ImageMessageItemProvider;
import io.rong.imkit.conversation.messgelist.provider.MessageItemProviderConfig;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imkit.widget.adapter.ViewHolder;
import io.rong.imlib.model.Message;
import io.rong.message.ImageMessage;

import static io.rong.imkit.conversation.messgelist.provider.SightMessageItemProvider.dip2pix;


/**
 * 自定义图片消息样式
 * 修改了背景
 */
public class ClassImageMessageItemProvider extends ImageMessageItemProvider {

    protected MessageItemProviderConfig mConfig = new MessageItemProviderConfig();

    public ClassImageMessageItemProvider() {
        mConfig.showProgress = false;
        mConfig.showReadState = true;
    }

    @Override
    protected void bindMessageContentViewHolder(ViewHolder holder, ViewHolder parentHolder, ImageMessage message, UiMessage uiMessage, int position, List<UiMessage> list, IViewProviderListener<UiMessage> listener) {
        super.bindMessageContentViewHolder(holder, parentHolder, message, uiMessage, position, list, listener);
        ImageView imageView = holder.getView(R.id.rc_img);
        TextView messageTv = holder.getView(R.id.rc_msg);
        RequestOptions options = RequestOptions.bitmapTransform(new RoundedCorners(dip2pix(IMCenter.getInstance().getContext(), 6))).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
        Glide.with(imageView).load(message.getThumUri())
                .apply(options)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        measureLayoutParams(holder.getView(io.rong.imkit.R.id.rl_content), resource);
                        return false;
                    }
                })
                .into(imageView);

        // 设置图片加载
        int progress = uiMessage.getProgress();
        Message.SentStatus status = uiMessage.getSentStatus();

        if (status.equals(Message.SentStatus.SENDING) && progress < 100) {
            messageTv.setText(progress + "%");
            messageTv.setVisibility(View.VISIBLE);
        } else {
            messageTv.setVisibility(View.GONE);
        }
    }


}
