package zserio.runtime.validation;

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

/**
 * Defines validation report which is returned from validation code generated by Zserio.
 * <p>
 * Validation code checks if SQL tables conform tables definition in Zserio.</p>
 * <p>
 * The following validation steps are taken:</p>
 * <p>
 * 1. Table schema validation.
 *    Table schema is read from SQLite and checked according to the Zserio table specification in Zserio.
 *    It's checked if number of columns is correct and if each column has expected type and expected
 *    'isNotNull' and 'isPrimaryKey' flags.</p>
 * <p>
 * 2. Validation of column values.
 *    Each blob or integer value stored in table is read from SQLite and checked. Blobs are read from
 *    the bit stream and written again. Then read bit stream is binary compared with the written stream.
 *    Integer values are checked according to their boundaries specified in Zserio.</p>
 */
public class ValidationReport
{
    /**
     * Constructs a default validation report.
     */
    public ValidationReport()
    {
        numberOfValidatedTables = 0;
        numberOfValidatedRows = 0;
        totalValidationTime = 0;
        totalParameterProviderTime = 0;
        errors = new ArrayList<ValidationError>();
    }

    /**
     * Constructs a new validation report from given arguments.
     *
     * @param numberOfValidatedTables    Number of validated tables to construct from.
     * @param numberOfValidatedRows      Number of validated rows to construct from.
     * @param totalValidationTime        Total validation time in milliseconds to construct from.
     * @param totalParameterProviderTime Total parameter provider time in milliseconds to construct from.
     * @param errors                     List of validation error to construct from.
     */
    public ValidationReport(int numberOfValidatedTables, int numberOfValidatedRows, long totalValidationTime,
            long totalParameterProviderTime, List<ValidationError> errors)
    {
        this.numberOfValidatedTables = numberOfValidatedTables;
        this.numberOfValidatedRows = numberOfValidatedRows;
        this.totalValidationTime = totalValidationTime;
        this.totalParameterProviderTime = totalParameterProviderTime;
        this.errors = errors;
    }

    /**
     * Gets the number of validated tables.
     *
     * @return Number of validated tables.
     */
    public int getNumberOfValidatedTables()
    {
        return numberOfValidatedTables;
    }

    /**
     * Gets the number of validated table rows.
     *
     * @return Number of validated table rows or 0 if table schema is not valid.
     */
    public int getNumberOfValidatedRows()
    {
        return numberOfValidatedRows;
    }

    /**
     * Gets total time in milliseconds spent in validation method generated by Zserio.
     *
     * @return Total validation time in milliseconds.
     */
    public long getTotalValidationTime()
    {
        return totalValidationTime;
    }

    /**
     * Gets total time in milliseconds spent in parameter provider callback implemented by called application.
     *
     * @return Total parameter provider time in milliseconds.
     */
    public long getTotalParameterProviderTime()
    {
        return totalParameterProviderTime;
    }

    /**
     * Gets the list of all validation errors.
     *
     * @return List of all validation errors.
     */
    public List<ValidationError> getErrors()
    {
        return errors;
    }

    /**
     * Adds other validation report to this report.
     *
     * This method just adds up all validation report parameters and stores result to this report.
     *
     * @param other Validation report to add.
     */
    public void add(ValidationReport other)
    {
        numberOfValidatedTables += other.numberOfValidatedTables;
        numberOfValidatedRows += other.numberOfValidatedRows;
        totalValidationTime += other.totalValidationTime;
        totalParameterProviderTime += other.totalParameterProviderTime;
        errors.addAll(other.errors);
    }

    private int                     numberOfValidatedTables;
    private int                     numberOfValidatedRows;
    private long                    totalValidationTime;
    private long                    totalParameterProviderTime;
    private List<ValidationError>   errors;
}
