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.media3.exoplayer.offline.Download;
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.ItemLikeSongBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.BoxDownloadSong;

public class AdapterDownloadSong extends BaseAdapter<Download, ItemLikeSongBinding> {

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VHolder<ItemLikeSongBinding> itemHolder = (VHolder<ItemLikeSongBinding>) holder;
        ItemLikeSongBinding vb = itemHolder.getVb();
        Download download = data.get(position);
        BoxDownloadSong boxDownloadSong = CommonUtils.downloadToBean(download);

        if (position == data.size()-1){
            vb.place.setVisibility(View.VISIBLE);
        }else {
            vb.place.setVisibility(View.GONE);
        }
        vb.layoutDownload.setVisibility(View.GONE);
        Glide.with(MusicApplication.myApplication)
                .asDrawable()
                .load(boxDownloadSong.getCovert())
                .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.tvTitle.setText(boxDownloadSong.getSongName());
        vb.tvSingerName.setText(boxDownloadSong.getSingerName());

        vb.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(homeItemClickListener!= null){
                    int absoluteAdapterPosition = itemHolder.getAbsoluteAdapterPosition();
                    homeItemClickListener.onClickDownloadSong(download,absoluteAdapterPosition);
                }
            }
        });

    }


}
