package it.com.atlassian.stash.commit;

import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.bitbucket.content.DiffSegmentType;
import com.atlassian.bitbucket.test.CommitCommentLikeHelper;
import com.atlassian.bitbucket.test.CommitCommentTestHelper;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.webdriver.bitbucket.element.Comment;
import com.atlassian.webdriver.bitbucket.page.CommitUnifiedDiffPage;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class CommitCommentLikeTest extends BaseCommitDiscussionFuncTest {

    private int commentId;
    private CommitCommentLikeHelper likeHelper;

    @Before
    public void setup() {
        testContext.user(FORK_USERNAME, FORK_DISPLAY_NAME, FORK_USERNAME, FORK_EMAIL)
                .projectPermission(PROJECT_KEY, FORK_USERNAME, Permission.PROJECT_ADMIN)
                .fork(PROJECT_KEY, REPO_SLUG, FORK_USERNAME, FORK_USERNAME);

        likeHelper = new CommitCommentLikeHelper(FORK_PROJECT, REPO_SLUG, FORK_COMMIT);
        CommitCommentTestHelper commitHelper = new CommitCommentTestHelper(FORK_PROJECT, REPO_SLUG, FORK_COMMIT);
        commentId = commitHelper.addCommitComment("TEST", COMMENT_ON_FILE, 1, DiffSegmentType.ADDED,
                FORK_USERNAME, FORK_USERNAME);
    }

    @Test
    public void testLikingComment() {
        CommitUnifiedDiffPage diffPage = loginAsAdmin(CommitUnifiedDiffPage.class, FORK_PROJECT, REPO_SLUG, FORK_COMMIT);
        Comment comment = diffPage.getFileContent().getDiffLines().get(0).getComment(commentId);

        Poller.waitUntilTrue("Like button is present", comment.canLike());
        assertTrue("Button is initially a like button", comment.hasLikeButton());
        comment.toggleLike();
        assertFalse("Button is now an unlike button", comment.hasLikeButton());
        assertEquals("Info text has changed", "You like this", comment.getLikeInfo());

        comment = BITBUCKET.visit(CommitUnifiedDiffPage.class, FORK_PROJECT, REPO_SLUG, FORK_COMMIT).getFileContent().getComments().get(0);

        Poller.waitUntilTrue("Like button is present", comment.canLike());
        assertFalse("Button is now an unlike button", comment.hasLikeButton());
        assertEquals("Info text has changed", "You like this", comment.getLikeInfo());
    }

    @Test
    public void testUnlikingComment() {
        likeHelper.likeComment(commentId);

        CommitUnifiedDiffPage diffPage = loginAsAdmin(CommitUnifiedDiffPage.class, FORK_PROJECT, REPO_SLUG, FORK_COMMIT);
        List<Comment> comments = diffPage.getFileContent().getComments();
        Comment comment = comments.get(0);

        Poller.waitUntilTrue("Like button is present", comment.canLike());
        assertFalse("Button is initially an unlike button", comment.hasLikeButton());
        assertEquals("Info text is correct", "You like this", comment.getLikeInfo());
        comment.toggleLike();
        assertTrue("Button is now an like button", comment.hasLikeButton());

        comment = BITBUCKET.visit(CommitUnifiedDiffPage.class, FORK_PROJECT, REPO_SLUG, FORK_COMMIT).getFileContent().getComments().get(0);

        Poller.waitUntilTrue("Like button is present", comment.canLike());
        assertTrue("Button is now an like button", comment.hasLikeButton());
    }
}
