package danielcortes.xyz.views.components.table_model;

import danielcortes.xyz.models.calculo_fondo.CalculoFondo;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class FondoTableModel extends AbstractTableModel {

  private String[] columns;
  private ArrayList<CalculoFondo> rows;
  private NumberFormat nf;


  public FondoTableModel() {
    super();
    this.columns = new String[]{"Valor", "Descripcion"};
    this.rows = new ArrayList<>();
    this.nf = NumberFormat.getIntegerInstance();

  }

  @Override
  public String getColumnName(int col) {
    return columns[col];
  }

  @Override
  public int getColumnCount() {
    return columns.length;
  }

  @Override
  public int getRowCount() {
    return rows.size();
  }

  public void addRow(CalculoFondo calculoFondo) {
    rows.add(calculoFondo);
    this.fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);

  }

  public void removeRow(int row) {
    this.rows.remove(row);
    this.fireTableRowsDeleted(row, row);
  }

  public void removeRows() {
    int rowCount = getRowCount();
    if (rowCount > 0) {
      this.rows.clear();
      this.fireTableRowsDeleted(0, rowCount - 1);
    }
  }

  public void setCalculoFondo(int editingId, CalculoFondo calculoFondo) {
    this.rows.set(editingId, calculoFondo);
    this.fireTableRowsUpdated(0, getRowCount() - 1);
  }

  @Override
  public Object getValueAt(int row, int col) {
    switch (col) {
      case 0:
        return nf.format(rows.get(row).getValor());
      case 1:
        return rows.get(row).getDescripcion();
    }
    return null;
  }

  public CalculoFondo getCalculoFondo(int row) {
    return rows.get(row);
  }
}
