package com.hi.music.player.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.hi.music.player.MusicApplication;
import com.hi.music.player.R;
import com.hi.music.player.databinding.ItemResultListAlbumBinding;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.javabean.response.child.ResponseResultListChild;

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

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


        VHolder<ItemResultListAlbumBinding> itemHolder = (VHolder<ItemResultListAlbumBinding>) holder;
        ItemResultListAlbumBinding vb = itemHolder.getVb();
        if(position == data.size()-1){
            vb.view.setVisibility(View.VISIBLE);
        }else {
            vb.view.setVisibility(View.GONE);
        }
        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.header);
        vb.tvTitle.setText(listChild.getSongName());
        vb.tvSubtitle.setText(listChild.getSubTitle());
        vb.header.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);
//                    if(videoId == null||videoId.isEmpty()){
//                        homeItemClickListener.onClickResultAlbum(listChild,absoluteAdapterPosition);
//                    }else if(browserId!= null&&!browserId.isEmpty()){
//                        homeItemClickListener.onClickResultSong(listChild,absoluteAdapterPosition);
//                    }
                }
            }
        });
        CommonUtils.LogMsg("-------Album getBrowserId="+listChild.getBrowserId()+"---getPlayListId= "+listChild.getPlayListId()+"---getVideoId="+listChild.getVideoId());
    }
}
