package main;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import schedule.TaskContainer;
import schedule.ScheduleConfigManager;

/**
 * This class is used to present information about scheduled actions of
 * application to user by table. Backup schedule is loaded from class
 * responsible for parsing schedule configuration.
 * 
 * @author Marcel Duris
 */
public class BackupScheduleModel extends AbstractTableModel {

    /*
     * Number of collumns, structure of infromation does not change.
     */
    private final int COLS = 3;
    /*
     * Headings of collumns
     */
    private final String AC = "Action";
    private final String TM = "Time";
    private final String FP = "Parameters";
    /**
     * List containing information to present to user.
     */
    private ArrayList<TaskContainer> details;

    /**
     * Constructor loads information about schedule from class managing schedule
     * configuration. This infromation does not update when configuration is
     * changed by manager. Manager of that configuration is singleton.
     *
     * @see schedule.ScheduleConfigManager
     */
    public BackupScheduleModel(){
        details = ScheduleConfigManager.getScheduleConfigManager().getDetails();
    }

    /**
     * Returns number of rows in table representing currently loaded scheduler.
     *
     * @return number of rows in table
     */
    public int getRowCount() {
        return details.size();
    }

    /**
     * Table consists of fixed number of collumns that does not change with
     * data. This method allways returns same value.
     * 
     * @return number of collums, 3 as for now
     */
    public int getColumnCount() {
        return COLS;
    }

    /**
     * Returns value of cell in table at given coordinates. This value is taken
     * from currently loaded schedule. Rows of table present different actions
     * while collumns contain names distinguishing different actions, time of
     * their execution and parameters passed to them.
     * 
     * @param rowIndex row of table
     * @param columnIndex collumn of table
     * @return object value of property presented by cell of table
     */
    public Object getValueAt(int rowIndex, int columnIndex) {
        TaskContainer det = details.get(rowIndex);
        switch(columnIndex){
            case 0: return det.getName();
            case 1: return ScheduleConfigManager.timeToHuman(det.getTime());
            case 2: return ScheduleConfigManager.paramToHuman(det.getName(), det.getParam());
        }
        return null;
    }

    /**
     * Returns names of collumns in table.
     *
     * @param index number of collumn
     * @return String name of collumn
     */
    @Override
    public String getColumnName(int index) {
        switch (index) {
            case 0:
                return AC;
            case 1:
                return TM;
            case 2:
                return FP;
        }
        return null;
    }
}
