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

import com.atlassian.bitbucket.commit.Commit;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.CommandFailedException;
import com.atlassian.bitbucket.scm.CommitCommandParameters;
import com.atlassian.bitbucket.scm.git.command.GitCommandBuilderFactory;
import com.atlassian.bitbucket.scm.git.command.GitCommandFactory;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.stash.internal.scm.git.AbstractGitTest;
import com.atlassian.stash.internal.scm.git.util.ConfigurableGitScmConfig;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import static com.atlassian.bitbucket.hamcrest.TimeMatchers.closeTo;
import static com.atlassian.stash.internal.scm.git.GitFactoriesTestHelper.*;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.mockito.Mockito.when;

public class CommitCommandTest extends AbstractGitTest {

    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();

    @Autowired
    private ConfigurableGitScmConfig config;
    @Autowired
    private GitCommandBuilderFactory builderFactory;
    @Autowired
    private GitCommandFactory commandFactory;
    private File directory;
    @Mock
    private Repository repository;
    @Mock
    private ApplicationUser user;

    @Before
    public void setUpUser() throws Exception {
        when(user.getDisplayName()).thenReturn("Git Commit Test");
        when(user.getEmailAddress()).thenReturn("git@commit.test");
    }

    @Before
    public void setUpRepository() throws Exception {
        this.directory = createEmptyRepository(tempFolder.getRoot(), builderFactory);
        this.config.setRepositoryDir(repository, this.directory);
    }

    @Test(expected = CommandFailedException.class)
    public void testCommitWithCleanWorkDir() throws Exception {
        createBuilder().commit().author(user).message("test.message").build().call();
    }

    @Test(expected = CommandFailedException.class)
    public void testCommitWithNoChangeInTheIndex() throws Exception {
        addNewFile(false);
        createBuilder().commit().author(user).message("test.message").build().call();
    }

    @Test(expected = NullPointerException.class)
    public void testCommitWithNoMessage() throws Exception {
        addNewFile(true);
        createBuilder().commit().author(user).build().call();
    }

    @Test
    public void testCommit() throws Exception {
        addNewFile(true);
        createBuilder().commit().author(user).message("test.message").build().call();

        // verify the change was successfully committed
        String head = getHead();
        Commit commit = commandFactory.commit(repository,
                new CommitCommandParameters.Builder().commitId(head).build()).call();
        assertNotNull(commit);
        assertEquals("test.message", commit.getMessage());
        assertEquals(user.getDisplayName(), commit.getAuthor().getName());
        assertEquals(user.getEmailAddress(), commit.getAuthor().getEmailAddress());
        assertThat(commit.getAuthorTimestamp(), closeTo(new Date(), 30, SECONDS));
    }

    @Test
    public void testCommitWithCJKMessage() throws Exception {
        addNewFile(true);
        String message = "測試 message";
        createBuilder().commit().author(user).message(message).build().call();

        String head = getHead();
        Commit commit = commandFactory.commit(repository,
                new CommitCommandParameters.Builder().commitId(head).build()).call();
        assertNotNull(commit);
        assertEquals(message, commit.getMessage());
        assertEquals(user.getDisplayName(), commit.getAuthor().getName());
        assertEquals(user.getEmailAddress(), commit.getAuthor().getEmailAddress());
        assertThat(commit.getAuthorTimestamp(), closeTo(new Date(), 30, SECONDS));
    }

    private String getHead() {
        return createBuilder().command("rev-list").argument("HEAD").argument("-1").build(new StringHandler()).call();
    }

    private File addNewFile(boolean addToIndex) throws IOException {
        File file = createTempFile("file", directory, "file-content");
        if (addToIndex) {
            createBuilder().add().path(file.getPath()).build().call();
        }
        return file;
    }

    private GitScmCommandBuilder createBuilder() {
        return builderFactory.builder(repository);
    }

}
