package net.testing.databasemanipulation.feature;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class DBAdapter extends ArrayAdapter<Object>{


    List list = new ArrayList();

    public DBAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }

    //@Override
    public void add(@Nullable DBGetSet object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        // return super.getCount();
        return list.size();
    }

    @Nullable
    @Override
    public Object getItem(int position) {
        // return super.getItem(position);
        return list.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View row;
        row = convertView;
        PromoHolder promoHolder;
        if (row == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.db_row_layout,parent,false);
            promoHolder = new PromoHolder();
            promoHolder.txtID = (TextView) row.findViewById(R.id.txt_id);
            promoHolder.txtNume = (TextView) row.findViewById(R.id.txt_nume);
            promoHolder.txtMaterie = (TextView) row.findViewById(R.id.txt_materie);
            promoHolder.txtNota = (TextView) row.findViewById(R.id.txt_nota);

            row.setTag(promoHolder);
        } else {
            promoHolder = (PromoHolder) row.getTag();
        }


        DBGetSet DBGetSet = (DBGetSet) this.getItem(position);
        promoHolder.txtID.setText(DBGetSet.getId());
        promoHolder.txtNume.setText(DBGetSet.getNume());
        promoHolder.txtMaterie.setText(DBGetSet.getMaterie());
        promoHolder.txtNota.setText(DBGetSet.getNota());

        Log.d("AdapterPromo", DBGetSet.getId());

        // return super.getView(position, convertView, parent);
        return row;
    }

    static class PromoHolder {
        TextView txtID, txtNume, txtMaterie, txtNota;
    }
}
