package cz.hobrasoft.pdfmu;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Logger;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.functors.StringValueTransformer;

/**
 *
 * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
 */
public enum PdfmuError {
    PARSER_UNKNOWN,
    PARSER_UNRECOGNIZED_ARGUMENTS,
    PARSER_INVALID_CHOICE,
    INPUT_NOT_VALID_PDF,
    INPUT_NOT_FOUND,
    INPUT_CLOSE,
    ATTACH_FAIL;

    // Configuration
    private static final int defaultErrorCode = -1;
    private static final String errorCodesResourceName = "cz/hobrasoft/pdfmu/operation/errorCodes.properties";
    private static final String errorMessagesResourceBundleBaseName = "cz.hobrasoft.pdfmu.operation.ErrorMessages";

    private static final Logger logger = Logger.getLogger(PdfmuError.class.getName());

    private static final IntProperties errorCodes = new IntProperties(defaultErrorCode);
    private static final ResourceBundle errorMessages = ResourceBundle.getBundle(errorMessagesResourceBundleBaseName);

    /**
     * Returns true iff each of the values of this enum is a key in both
     * {@link #errorCodes} and {@link #errorMessages}.
     */
    private static boolean codesAndMessagesAvailable() {
        PdfmuError[] enumKeyArray = PdfmuError.values();
        List<PdfmuError> enumKeyList = Arrays.asList(enumKeyArray);
        Collection<String> enumKeyStrings = CollectionUtils.collect(enumKeyList, StringValueTransformer.stringValueTransformer());

        Set<String> codeKeySet = errorCodes.stringPropertyNames();
        Set<String> messageKeySet = errorMessages.keySet();

        return codeKeySet.containsAll(enumKeyStrings) && messageKeySet.containsAll(enumKeyStrings);
    }

    // Load error codes from a properties resource
    private static void loadErrorCodes() {
        ClassLoader classLoader = PdfmuError.class.getClassLoader();
        InputStream in = classLoader.getResourceAsStream(errorCodesResourceName);
        if (in != null) {
            try {
                errorCodes.load(in);
            } catch (IOException ex) {
                logger.severe(String.format("Could not load the error codes properties file: %s", ex));
            }
            try {
                in.close();
            } catch (IOException ex) {
                logger.severe(String.format("Could not close the error codes properties file: %s", ex));
            }
        } else {
            logger.severe("Could not open the error codes properties file.");
        }
    }

    static {
        // Load error codes before OperationException is instantiated
        loadErrorCodes();

        // Make sure all a code and a message is available for every enum value
        assert codesAndMessagesAvailable();
    }

    /**
     * Returns the error code associated with this error. The code should
     * uniquely identify the error.
     *
     * @return the error code associated with this error.
     */
    public int getCode() {
        return errorCodes.getIntProperty(toString());
    }

    /**
     * Returns the message pattern associated with this error.
     *
     * @return the message pattern associated with this error.
     */
    public String getMessagePattern() {
        String key = toString();
        if (errorMessages.containsKey(key)) {
            return errorMessages.getString(key);
        } else {
            return null;
        }
    }
}

@@ -134,24 +134,51 @@
 ollection;%0D%0A
+import java.util.HashSet;%0D%0A
 import java.
@@ -2007,24 +2007,408 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * @return true iff the codes stored in %60errorCodes%60 are pairwise different.%0D%0A     */%0D%0A    private static boolean codesUnique() %7B%0D%0A        Collection%3CInteger%3E codes = errorCodes.intPropertyValues();%0D%0A        Set%3CInteger%3E codesUnique = new HashSet%3C%3E(codes);%0D%0A        assert codes.size() %3E= codesUnique.size();%0D%0A        return codes.size() == codesUnique.size();%0D%0A    %7D%0D%0A%0D%0A
     // Load 
@@ -3449,24 +3449,57 @@
 vailable();%0D
+%0A%0D%0A        assert codesUnique();%0D
 %0A    %7D%0D%0A%0D%0A  

static: Assert that the error codes are unique