package com.ez.ertc.meeting.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import java.util.List;


public class YsBaseAdapter extends RecyclerView.Adapter<YsRvBaseHolder<YsBaseDto>> {

    private YsAdapterHelper ysAdapterHelper;
    private Context context;
    private LayoutInflater layoutInflater;
    private List<YsBaseDto> mData;

    public List<YsBaseDto> getmData() {
        return mData;
    }

    public void setmData(List<YsBaseDto> mData) {
        this.mData = mData;
    }

    public YsBaseAdapter(Context context, List<YsDtoStyle> styles) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.ysAdapterHelper = new YsAdapterHelper(styles);
    }

    public void update(List<YsBaseDto> data){
        mData = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public YsRvBaseHolder<YsBaseDto> onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        return ysAdapterHelper.newInstance(
                viewType,
                layoutInflater.inflate(ysAdapterHelper.getLayoutId(viewType),viewGroup,false),
                context,
                YsBaseAdapter.this
        );
    }

    @Override
    public void onBindViewHolder(@NonNull YsRvBaseHolder<YsBaseDto> viewHolder, int position) {
        viewHolder.setData(mData.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        return ysAdapterHelper.getItemViewType(mData.get(position).getClass());
    }


    @Override
    public int getItemCount() {
        return mData == null ? 0 :mData.size();
    }
}
