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

import com.atlassian.bitbucket.branch.model.BranchModel;
import com.atlassian.bitbucket.internal.branch.model.restriction.type.ModelBranchMatcherType;
import com.atlassian.bitbucket.repository.Branch;
import com.atlassian.bitbucket.repository.MinimalRef;
import com.atlassian.bitbucket.repository.ref.restriction.AbstractRefMatcher;

import javax.annotation.Nonnull;

public class ModelBranchMatcher extends AbstractRefMatcher {

    private final String currentRefId;
    private final String displayId;

    public ModelBranchMatcher(String branchType, String displayId, BranchModel model) {
        super(ModelBranchMatcherType.getInstance(), branchType);

        this.displayId = displayId;

        if ("development".equals(branchType)) {
            currentRefId = model.getDevelopment().getId();
        } else if ("production".equals(branchType)) {
            Branch production = model.getProduction();
            currentRefId = production == null ? null : production.getId();
        } else {
            currentRefId = null;
        }
    }

    @Nonnull
    @Override
    public String getDisplayId() {
        return displayId;
    }

    @Override
    public boolean matches(@Nonnull MinimalRef ref) {
        return ref.getId().matches(currentRefId);
    }
}
