package com.writeoff.common.model;

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

public class ImportResult {
    private int total;
    private int success;
    private int failed;
    private boolean partialSuccess;
    private List<ImportRowError> errors = new ArrayList<ImportRowError>();

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
        refreshFlags();
    }

    public int getSuccess() {
        return success;
    }

    public void setSuccess(int success) {
        this.success = success;
        refreshFlags();
    }

    public int getFailed() {
        return failed;
    }

    public void setFailed(int failed) {
        this.failed = failed;
        refreshFlags();
    }

    public boolean isPartialSuccess() {
        return partialSuccess;
    }

    public void setPartialSuccess(boolean partialSuccess) {
        this.partialSuccess = partialSuccess;
    }

    public List<ImportRowError> getErrors() {
        return errors;
    }

    public void setErrors(List<ImportRowError> errors) {
        this.errors = errors == null ? new ArrayList<ImportRowError>() : errors;
        refreshFlags();
    }

    public void markSuccess() {
        this.success++;
        refreshFlags();
    }

    public void addError(int rowNo, String identifier, String message) {
        this.errors.add(new ImportRowError(rowNo, identifier, message));
        this.failed++;
        refreshFlags();
    }

    private void refreshFlags() {
        if (this.total <= 0) {
            this.total = this.success + this.failed;
        }
        this.partialSuccess = this.success > 0 && this.failed > 0;
    }
}
