package com.example.cloudmusic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;
import com.example.cloudmusic.been.MineMusicListBeen;
import com.example.cloudmusic.R;

import java.util.List;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.InnerHolder> {

    private final Context mContext;
    private final List<MineMusicListBeen> mList;

    public MusicAdapter(List<MineMusicListBeen> list, Context context) {
        mList = list;
        mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public MusicAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MusicAdapter.InnerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.music_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MusicAdapter.InnerHolder holder, int position) {
        holder.setData(mList.get(position));
    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        private final ImageView img;
        private final TextView title;
        private final TextView content;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.music_img);
            title = itemView.findViewById(R.id.music_title);
            content = itemView.findViewById(R.id.music_content);
        }

        public void setData(MineMusicListBeen been) {
            Glide.with(mContext)
                    .load(been.getCoverImgUrl())
                    .placeholder(R.drawable.cloudmusic_white)
                    .into(img);
            title.setText(been.getName());
            content.setText("歌单-"+been.getCount()+"首-"+been.getAuthor());
        }
    }
}
