package main;

import javax.swing.table.AbstractTableModel;
import backupAndRecovery.BackupMetaContainer;

/**
 * This model is used to present list of found backups to user by simple table.
 * 
 * @author Marcel Duris
 */
public class BackupListModel extends AbstractTableModel {

    /*
     * List of found backups
     */
    private BackupMetaContainer[] metas;
    /*
     * Parameters of different columns of table.
     */
    private final int columns = 5;
    private final String FN = "File Name";
    private final String BT = "Backup created on";
    private final String LM = "Last modified on";
    private final String UN = "Backup created by";
    private final String FS = "Size of file";

    /**
     * Constructor loads array containing meta infromation of backups to be
     * presented to user.
     * 
     * @param metas an array of containers storing meta information
     */
    public BackupListModel(BackupMetaContainer[] metas) {
        this.metas = metas;
    }

    /**
     * Returns number of rows required to display information of every found
     * backup
     *
     * @return number of rows of table needed
     */
    public int getRowCount() {
        if (metas == null) {
            return 0;
        }
        return metas.length;
    }

    /**
     * Number of columns in table. Structure of table does not change and
     * therefore constant value is allways returned.
     *
     * @return number of columns in table.
     */
    public int getColumnCount() {
        return columns;
    }

    /**
     * Returns object value of cell in table indetified by coordinates.
     *
     * @param rowIndex row of table
     * @param columnIndex column of table
     * @return value of cell
     */
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex > metas.length) {
            return null;
        }
        BackupMetaContainer meta = metas[rowIndex];
        switch (columnIndex) {
            case 0:
                return meta.getFilename();
            case 1:
                return meta.getBackupTime();
            case 2:
                return meta.getModificationTime();
            case 3:
                return meta.getUserName();
            case 4:
                return sizeToHuman(meta.getSize());
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * Returns names of columns.
     *
     * @param index number of column
     * @return name of column
     */
    @Override
    public String getColumnName(int index) {
        if (index > columns) {
            return null;
        }

        switch (index) {
            case 0:
                return FN;
            case 1:
                return BT;
            case 2:
                return LM;
            case 3:
                return UN;
            case 4:
                return FS;
            default:
                return null;
        }
    }

    /**
     * This method allows loading of data from model. Given index of user
     * selected row, corresponding backup information is returned.
     * 
     * @param index number of row
     * @return meta information belonging to selected backup
     */
    public BackupMetaContainer getMeta(int index) {
        return metas[index];
    }

    /**
     * Used to convert size of file into human readable format. Uses SI units
     * to represent size of file in bytes, kilobytes, megabytes etc.
     * 
     * @param size of file in bytes
     * @return human readable string representing size of file
     */
    private String sizeToHuman(double size) {
        String[] unit = new String[]{"B", "KB", "MB", "GB", "TB", "PB", "EB"};
        int unitNo = 0;
        while (size > 1000) {
            size = size / 1000.0;
            unitNo++;
        }
        size = size * 1000;
        size = Math.round(size);
        size = size / 1000.0;

        return size + " " + unit[unitNo];
    }
}
