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.ItemResultListSongBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.response.child.ResponseResultListChild;

public class AdapterResultListSong extends BaseAdapter<ResponseResultListChild, ItemResultListSongBinding> {
    @Override
    protected ItemResultListSongBinding getViewBinding(ViewGroup parent) {
        return ItemResultListSongBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ResponseResultListChild listChild = data.get(position);

        VHolder<ItemResultListSongBinding> itemHolder = (VHolder<ItemResultListSongBinding>) holder;
        ItemResultListSongBinding vb = itemHolder.getVb();

        Glide.with(MusicApplication.myApplication)
                .asDrawable()
                .load(listChild.getThumbnail())
                .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.imCovert);
        vb.tvSongName.setText(listChild.getSongName());
        String playCount = listChild.getPlayCount();
        String subTitle = listChild.getSubTitle();
        if (playCount == null){
            vb.tvSubtitle.setText(subTitle);
        }else {
            String s = subTitle + " • " + playCount;
            vb.tvSubtitle.setText(s);
        }


        vb.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (homeItemClickListener != null) {
                    int absoluteAdapterPosition = itemHolder.getAbsoluteAdapterPosition();
                    String videoId = listChild.getVideoId();
                    String browserId = listChild.getBrowserId();
                    homeItemClickListener.onClickResultSong(listChild, absoluteAdapterPosition);

                }
            }
        });

//        CommonUtils.LogMsg("-------Song getBrowserId=" + listChild.getBrowserId() + "---getPlayListId= " + listChild.getPlayListId() + "---getVideoId=" + listChild.getVideoId());

    }
}
