package eu.dnetlib.espas.exception;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("UnusedDeclaration")
public class OwsException extends Exception {
    private Map<String, Tuple<String, OwsExceptionCode>> exceptions = new HashMap<String, Tuple<String, OwsExceptionCode>>();

    public OwsException(Throwable throwable, Map<String, Tuple<String, OwsExceptionCode>> exceptions) {
        super(throwable);
        this.exceptions = exceptions;
    }

    public OwsException(Map<String, Tuple<String, OwsExceptionCode>> exceptions) {
        super();
        this.exceptions = exceptions;
    }

    public OwsException(String locator, OwsExceptionCode exceptionCode) {
        super();
        this.exceptions.put(locator, new Tuple<String, OwsExceptionCode>("", exceptionCode));
    }

    public OwsException(String message, String locator, OwsExceptionCode exceptionCode) {
        super(message);
        this.exceptions.put(locator, new Tuple<String, OwsExceptionCode>(message, exceptionCode));
    }

    public Map<String, Tuple<String, OwsExceptionCode>> getExceptions() {
        return exceptions;
    }

    public int getStatus() {
        int status = 400;
        for (Tuple<String, OwsExceptionCode> details : exceptions.values()) {
            OwsExceptionCode code = details.y;
            switch (code) {
                case OPERATION_NOT_SUPPORTED:
                    return code.getCode();
                case OPTION_NOT_SUPPORTED:
                    status = code.getCode();
                    break;
                case NO_APPLICABLE_CODE:
                    status = code.getCode();
                    break;
                default:
                    break;
            }
        }
        return status;
    }

    @Override
    public String getMessage() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        stringBuilder.append("<ows:ExceptionReport xmlns:ows=\"http://www.opengis.net/ows/1.1\" " +
                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                "xsi:schemaLocation=\"http://www.opengis.net/ows/1.1 http://schemas.opengis.net/ows/1.1.0/owsExceptionReport.xsd\" " +
                "version=\"1.0.0\" xml:lang=\"en\">\n");
        for (String locator : exceptions.keySet()) {
            stringBuilder.append("<ows:Exception exceptionCode=\"")
                    .append(exceptions.get(locator).y.getName()).append("\"");
            if (!locator.isEmpty()) {
                stringBuilder.append(" locator=\"").append(locator).append("\"");
            }

            if (!exceptions.get(locator).x.isEmpty())
                stringBuilder.append(">\n").append("<ows:ExceptionText>").append(exceptions.get(locator).x).append("</ows:ExceptionText>\n").append("</ows:Exception>\n");
            else
                stringBuilder.append("/>\n");
        }
        stringBuilder.append("</ows:ExceptionReport>\n");
        return stringBuilder.toString();
    }
}
