package com.ez.ertc.meeting.base;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class YsAdapterHelper {

    private List<YsDtoStyle> styles;

    public YsAdapterHelper(List<YsDtoStyle> styles) {
        this.styles = styles;
    }

    public int getItemViewType(Class clazz){
        for (int i = 0;i < styles.size();i++){
            YsDtoStyle style = styles.get(i);
            if (clazz == style.dtoClazz){
                return i;
            }
        }
        return 0;
    }

    public int getLayoutId(int viewType){
        return styles.get(viewType).layoutId;
    }

    public YsRvBaseHolder newInstance(int viewType, View view, Context context, RecyclerView.Adapter adapter){
        Class holderClazz = styles.get(viewType).holderClazz;
        try {
            Constructor c = holderClazz.getConstructor(View.class,Context.class);
            YsRvBaseHolder holder = (YsRvBaseHolder) c.newInstance(view,context);
            holder.setAdapter(adapter);
            return holder;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

}
