package com.offline.music.playermp3.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.offline.music.playermp3.MusicApplication;
import com.offline.music.playermp3.R;
import com.offline.music.playermp3.databinding.ItemSearchBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.response.ResponseSearch;
import com.offline.music.playermp3.javabean.response.child.ResponseSearchChild;

import java.util.List;

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

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

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

        vb.tvHeaderTitle.setText(responseSearch.getHeaderTitle());

        String beastSongTCovert = responseSearch.getBeastSongTCovert();
        if (beastSongTCovert != null && !beastSongTCovert.isEmpty()) {
            vb.layoutBest.setVisibility(View.VISIBLE);
            Glide.with(MusicApplication.myApplication)
                    .load(beastSongTCovert)
                    .transform(new CircleCrop())
                    .into(vb.imBestCovert);
            vb.tvBestTitle.setText(responseSearch.getBeastSongTitle());
            vb.tvBestSubtitle.setText(responseSearch.getBeastSongDescribe());
            vb.layout.setBackground(ContextCompat.getDrawable(MusicApplication.myApplication, R.drawable.bg_best_bg));
            vb.layout.setPadding(CommonUtils.dpToPx(6), CommonUtils.dpToPx(10), 0, 0);
            vb.tvPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (homeItemClickListener != null) {
                        homeItemClickListener.onClickSearchResultBest(responseSearch);

                    }
                }
            });
        } else {
            vb.layoutBest.setVisibility(View.GONE);
            vb.layout.setBackground(null);
            vb.layout.setPadding(0, 0, 0, 0);
        }

        List<ResponseSearchChild> list = responseSearch.getList();

        if(list!= null&&list.size()>0){
            AdapterSearchChild adapterSearchChild = new AdapterSearchChild();
            adapterSearchChild.setData(list);
            vb.list.setAdapter(adapterSearchChild);
            if (homeItemClickListener != null)
                adapterSearchChild.setHomeItemClickListener(homeItemClickListener);
            vb.list.setLayoutManager(new LinearLayoutManager(MusicApplication.myApplication));
        }

        if(position == data.size() - 1){
            vb.place.setVisibility(View.VISIBLE);
        }else {
            vb.place.setVisibility(View.GONE);
        }

    }
}
