package xn.xxp.main.rule;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

import http.vo.response.SafeBook;
import xn.xxp.databinding.ItemRuleBinding;

public class RuleAdapter extends BaseAdapter {
    private List<SafeBook> safeBookList;

    public RuleAdapter(List<SafeBook> safeBookList) {
        this.safeBookList = safeBookList;
    }

    @Override
    public int getCount() {
        return null == safeBookList || safeBookList.isEmpty() ? 0 : safeBookList.size();
    }

    @Override
    public SafeBook getItem(int position) {
        return null != safeBookList && !safeBookList.isEmpty() ? safeBookList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            ItemRuleBinding binding = ItemRuleBinding.inflate(LayoutInflater.from(parent.getContext()));
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SafeBook safeBook = getItem(position);
        if (null != safeBook) {
            viewHolder.binding.text.setText(TextUtils.isEmpty(safeBook.name) ? "" : safeBook.name);
        }
        return convertView;
    }

    static class ViewHolder {
        private final ItemRuleBinding binding;

        public ViewHolder(ItemRuleBinding binding) {
            this.binding = binding;
        }
    }
}
