package com.atlassian.stash.internal.scm.git;

import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.ScmRequest;
import com.atlassian.bitbucket.scm.ScmRequestCheck;

import javax.annotation.Nonnull;
import java.io.IOException;

/**
 * Checks for Git HTTP or SSH requests targeting a non-Git repository. Writes out a pretty error message to the Git
 * client explaining that the repository appears to be a different type.
 *
 * @since 1.1
 */
public class GitRepositoryTypeCheck implements ScmRequestCheck {

    private final I18nService i18nService;

    public GitRepositoryTypeCheck(I18nService i18nService) {
        this.i18nService = i18nService;
    }

    @Override
    public boolean check(@Nonnull ScmRequest request) throws IOException {
        if (request instanceof AbstractGitRepositoryScmRequest) {
            Repository repository = request.getRepository();
            if (repository != null && !"git".equals(repository.getScmId())) {
                request.sendError(
                        i18nService.getMessage("bitbucket.scm.git.repository.unsupported", repository.getName()),
                        i18nService.getMessage("bitbucket.scm.git.repository.unsupported.detail",
                                repository.getName(), repository.getScmId()));
                return false;
            }
        }

        return true;
    }
}
