package com.atlassian.bitbucket.mesh.git.builder.diff;

import javax.annotation.Nonnull;

/**
 * Defines the type of data which is provided by a given {@link GitDiffHeader}.
 */
public enum GitDiffHeaderType {

    /**
     * Index data, of the format {@code &lt;source hash&gt;..&lt;destination hash&gt; &lt;optional mode&gt;}.
     * <p>
     * The mode is included only if it was not changed in the revision. Otherwise, separate headers provide the old
     * and new modes and it is omitted from the index data.
     */
    INDEX(false),
    /**
     * File mode, as a 6-digit octal number defining both type and permission bits, either for a
     * {@link GitDiffTarget#SOURCE source} or {@link GitDiffTarget#DESTINATION destination}.
     */
    MODE(true),
    /**
     * Path, either for a {@link GitDiffTarget#SOURCE source} or {@link GitDiffTarget#DESTINATION destination}.
     */
    PATH(true),
    /**
     * A percentage, comprised of a rounded down integer followed by a percent sign.
     */
    PERCENTAGE(false);

    private final boolean targeted;

    GitDiffHeaderType(boolean targeted) {
        this.targeted = targeted;
    }

    /**
     * Retrieves a flag indicating whether this type of data requires a specific {@link GitDiffTarget}.
     *
     * @return {@code true} if a target is required; otherwise, {@code false}
     */
    public boolean isTargeted() {
        return targeted;
    }

    /**
     * Verifies that the provided {@link GitDiffTarget} is valid for use with this header type.
     *
     * @param target the target to verify
     * @throws IllegalArgumentException Thrown if the provided {@code target} is not valid for this header type.
     */
    public void verify(@Nonnull GitDiffTarget target) {
        if (targeted && target == GitDiffTarget.NONE) {
            throw new IllegalArgumentException("Type [" + name() + "] requires an explicit DiffTarget");
        } else if (!targeted && target != GitDiffTarget.NONE) {
            throw new IllegalArgumentException("Type [" + name() + "] does not support a DiffTarget");
        }
    }
}
