package com.atlassian.bitbucket.internal.branch.model;

import com.atlassian.bitbucket.branch.model.BranchType;
import com.atlassian.bitbucket.i18n.I18nService;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Internal extension of {@link BranchTypes}.
 *
 * @since 2.8
 */
class InternalBranchTypes {

    static final InternalBranchTypes BUGFIX = forType(BranchTypes.BUGFIX);
    static final InternalBranchTypes FEATURE = forType(BranchTypes.FEATURE);
    static final InternalBranchTypes HOTFIX = forType(BranchTypes.HOTFIX);
    static final InternalBranchTypes RELEASE = forType(BranchTypes.RELEASE);

    private static final String I18N_PREFIX = "bitbucket.branchmodel.type.";

    private final BranchTypes type;

    private InternalBranchTypes(@Nonnull BranchTypes type) {
        this.type = checkNotNull(type, "type");
    }

    static InternalBranchType asInternalBranchType(@Nonnull BranchType branchType) {
        checkNotNull(branchType, "branchType");
        if (!InternalBranchType.class.isInstance(branchType)) {
            throw new ClassCastException("Unexpected BranchType implementation that is not an InternalBranchType: "
                    + branchType.getClass().getName());
        }
        return (InternalBranchType) branchType;
    }

    static InternalBranchTypes forId(@Nonnull String id) {
        return forType(BranchTypes.forId(id));
    }

    static InternalBranchTypes forType(@Nonnull BranchTypes type) {
        return new InternalBranchTypes(type);
    }

    BranchTypes getType() {
        return type;
    }

    String getId() {
        return type.getId();
    }

    String getDisplayName(@Nonnull I18nService i18nService) {
        return i18nService.getMessage(I18N_PREFIX + type.getId());
    }

    String getDefaultPrefix() {
        return getId().toLowerCase() + "/";
    }
}
