package com.example.cloudmusic.adapter;

import static androidx.core.content.ContextCompat.startActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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.example.cloudmusic.activity.PlayerActivity;
import com.example.cloudmusic.R;
import com.example.cloudmusic.been.ListBeen;

import java.util.List;

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

    private final List<ListBeen.Song> mList;
    private final Context mContext;

    public SongAdapter(List<ListBeen.Song> list, Context context) {
        mList = list;
        mContext = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull SongAdapter.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 implements View.OnClickListener {

        private final TextView number;
        private final TextView song_name;
        private final TextView song_author;
        private final ImageView mv;
        private final TextView vip;
        private ListBeen.Song song1;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            number = itemView.findViewById(R.id.bianh);
            song_name = itemView.findViewById(R.id.song_name);
            song_author = itemView.findViewById(R.id.song_author);
            mv = itemView.findViewById(R.id.song_item_mv);
            vip = itemView.findViewById(R.id.song_item_vip);

            itemView.setOnClickListener(this);
        }

        public void setData(ListBeen.Song song) {
            song1 = song;
            number.setText(song.getSongNumber());
            song_name.setText(song.getSongName());
            song_author.setText(song.getAuthorName()+"-"+song.getListName());
            if (song.isMv()){
                mv.setVisibility(View.VISIBLE);
            }
            if (song.isVip()){
                vip.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(mContext, PlayerActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("id",song1.getId());
            bundle.putString("songListName",song1.getSongListName());
            intent.putExtras(bundle);
            startActivity(mContext,intent,bundle);
        }
    }
}
