package io.wealthwizards.appsynctesting.utils;

import io.wealthwizards.appsynctesting.AppSyncState;
import io.wealthwizards.appsynctesting.AppSyncException;

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

public class StatefulUtil {
    private final AppSyncState state;
    private final List<AppSyncException> errors;

    public StatefulUtil(AppSyncState state) {
        this.state = state;
        this.errors = new ArrayList<>();
    }

    public void appendError(String message) {
        appendError(message, null, null, null);
    }

    public void appendError(String message, String type) {
        appendError(message, type, null, null);
    }

    public void appendError(String message, String type, Object error) {
        appendError(message, type, error, null);
    }

    public void appendError(String message, String type, Object error, Object info) {
        errors.add(new AppSyncException(message, type, error, info));
    }

    public void unauthorized() throws AppSyncException {
        throw new AppSyncException("Unauthorized", "Unauthorized", null, null);
    }

    public void error(String message) throws AppSyncException {
        error(message, null, null, null);
    }

    public void error(String message, String type) throws AppSyncException {
        error(message, type, null, null);
    }

    public void error(String message, String type, Object error) throws AppSyncException {
        error(message, type, error, null);
    }

    public void error(String message, String type, Object error, Object info) throws AppSyncException {
        throw new AppSyncException(message, type, error, info);
    }

    public void validate(boolean isValid, String message) throws AppSyncException {
        validate(isValid, message, null, null);
    }

    public void validate(boolean isValid, String message, String type) throws AppSyncException {
        validate(isValid, message, type, null);
    }

    public void validate(boolean isValid, String message, String type, Object error) throws AppSyncException {
        if (!isValid) {
            error(message, type, error);
        }
    }

    public String authType() {
        return state.getAuthType();
    }
}
