package no.digipost.signature.client.direct;

import no.digipost.signature.api.xml.XMLDirectSignerResponse;

import java.net.URI;
import java.util.Objects;

public class DirectSignerResponse implements WithSignerUrl {

    public static DirectSignerResponse fromJaxb(XMLDirectSignerResponse signer) {
        if (signer.getPersonalIdentificationNumber() != null) {
            return new DirectSignerResponse(signer.getPersonalIdentificationNumber(), null, URI.create(signer.getHref()), URI.create(signer.getRedirectUrl()));
        } else if (signer.getSignerIdentifier() != null) {
            return new DirectSignerResponse(null, signer.getSignerIdentifier(), URI.create(signer.getHref()), URI.create(signer.getRedirectUrl()));
        } else {
            throw new IllegalStateException(
                    "Unable to convert from " + XMLDirectSignerResponse.class.getSimpleName() + " " +
                    "to " + DirectSignerResponse.class.getSimpleName() + ", because " +
                    "both personalIdentificationNumber and signerIdentifier was null");
        }
    }


    private final String personalIdentificationNumber;
    private final String customIdentifier;
    private final URI signerUrl;
    private final URI redirectUrl;

    DirectSignerResponse(String personalIdentificationNumber, String customIdentifier, URI signerUrl, URI redirectUrl) {
        this.personalIdentificationNumber = personalIdentificationNumber;
        this.customIdentifier = customIdentifier;
        this.signerUrl = signerUrl;
        this.redirectUrl = redirectUrl;
    }

    /**
     * Check if this signer is identified by the given identifier string, either
     * as personal identification number or a custom identifier.
     *
     * @param identifier either a personal identification number or a custom identifier
     * @return {@code true} if this signer has the given identifier, {@code false} otherwise.
     */
    public boolean hasIdentifier(String identifier) {
        return Objects.equals(personalIdentificationNumber, identifier) || Objects.equals(customIdentifier, identifier);
    }

    public boolean isIdentifiedByPersonalIdentificationNumber() {
        return personalIdentificationNumber != null;
    }

    public String getPersonalIdentificationNumber() {
        if (!isIdentifiedByPersonalIdentificationNumber()) {
            throw new IllegalStateException(this + " is not identified by personal identification number, use getCustomIdentifier() instead.");
        }
        return personalIdentificationNumber;
    }

    public String getCustomIdentifier() {
        if (customIdentifier == null) {
            throw new IllegalStateException(this + " is not identified by a custom identifier, use getPersonalIdentificationNumber() instead.");
        }
        return customIdentifier;
    }

    @Override
    public URI getSignerUrl() {
        return signerUrl;
    }

    public URI getRedirectUrl() {
        return redirectUrl;
    }

}

@@ -2119,129 +2119,827 @@
     
-public boolean isIdentifiedByPersonalIdentificationNumber() %7B%0A        return personalIdentificationNumber != null;%0A    %7D%0A
+/**%0A     * Check if this signer is identified by a personal identification number. If%0A     * this method returns %7B@code true%7D, %7B@link #getPersonalIdentificationNumber()%7D can%0A     * safely be called. Likewise, if it returns %7B@code false%7D, then%0A     * %7B@link #getCustomIdentifier()%7D can be called.%0A     *%0A     * @return %7B@code true%7D if this signer is identified by a personal identification number,%0A     *         %7B@code false%7D otherwise.%0A     */%0A    public boolean isIdentifiedByPersonalIdentificationNumber() %7B%0A        return personalIdentificationNumber != null;%0A    %7D%0A%0A    /**%0A     * Get the personal identification number for this signer.%0A     *%0A     * @return the personal identification number%0A     * @throws IllegalStateException if this signer is %3Cem%3Enot%3C/em%3E identified by personal identification number%0A     */
 %0A   
@@ -3248,32 +3248,258 @@
 nNumber;%0A    %7D%0A%0A
+    /**%0A     * Get the custom identifier string for this signer.%0A     *%0A     * @return the custom identifier%0A     * @throws IllegalStateException if this signer is %3Cem%3Enot%3C/em%3E identified by a custom identifier string%0A     */%0A
     public Strin

Add some javadocs to DirectSignerResponse