package com.atlassian.stash.internal.web.admin;

import com.atlassian.stash.internal.web.repos.ViewRef;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.validation.annotation.RequiredString;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;

/**
 * This should only contain fields required by repository-settings.soy and {@link RepositoryAdminController}.
 */
@SuppressWarnings("unused") //All constructors/fields are used, either directly or by SpringMVC
public class RepositoryForm {

    // This is passed in from the controller
    private ViewRef defaultBranch;
    // This is submitted by the form
    private String defaultBranchId;
    private boolean forkable;
    private String name;
    private String scmId;

    public RepositoryForm() {
    }

    public RepositoryForm(Repository repository, ViewRef defaultBranch) {
        this.defaultBranch = defaultBranch;
        forkable = repository.isForkable();
        name = repository.getName();
        scmId = repository.getScmId();
    }

    public ViewRef getDefaultBranch() {
        return defaultBranch;
    }

    public String getDefaultBranchId() {
        return defaultBranchId;
    }

    @RequiredString(size = Repository.NAME_MAX_SIZE, regexp = Repository.NAME_REGEXP,
            message = "{com.atlassian.bitbucket.validation.repository.name.pattern.message}")
    public String getName() {
        return name;
    }

    @Nonnull
    @RequiredString
    public String getScmId() {
        return scmId;
    }

    public boolean isForkable() {
        return forkable;
    }

    public void setDefaultBranch(ViewRef defaultBranch) {
        this.defaultBranch = defaultBranch;
    }

    public void setDefaultBranchId(String defaultBranchId) {
        this.defaultBranchId = defaultBranchId;
    }

    public void setForkable(boolean forkable) {
        this.forkable = forkable;
    }

    public void setName(String name) {
        this.name = StringUtils.trimToNull(name);
    }

    public void setScmId(String scmId) {
        this.scmId = scmId;
    }
}
