package com.offline.music.playermp3.adapter;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

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 com.offline.music.playermp3.MusicApplication;
import com.offline.music.playermp3.R;
import com.offline.music.playermp3.databinding.ItemSingerBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.response.child.ResponseSingle;

public class AdapterSinger extends BaseAdapter<ResponseSingle, ItemSingerBinding> {

    @Override
    protected ItemSingerBinding getViewBinding(ViewGroup parent) {
        return ItemSingerBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == TYPE_ITEM) {
            VHolder<ItemSingerBinding> itemHolder = (VHolder<ItemSingerBinding>) holder;
            ItemSingerBinding vb = itemHolder.getVb();
            ResponseSingle responseSingle = data.get(position);

            Glide.with(MusicApplication.myApplication)
                    .asDrawable()
                    .load(responseSingle.getSingerHead())
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(CommonUtils.dpToPx(4))))
                    .placeholder(R.mipmap.im_placeholder)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                            CommonUtils.LogMsg(e.getMessage());
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    })
                    .into(vb.header);
            vb.tvSingerName.setText(responseSingle.getSingerName());
            vb.tvSongName.setText(responseSingle.getSongTitle());
//        vb.tvDescribe.setText(responseSingle.getDescription());
            vb.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (homeItemClickListener != null) {
                        homeItemClickListener.onClickItemSinger(responseSingle);
                    }

                }
            });

        }


    }


}
