package com.atlassian.webdriver.bitbucket.page.admin.repo;

import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.webdriver.bitbucket.element.AbstractElementPageObject;
import com.atlassian.webdriver.bitbucket.page.BaseRepositoryPage;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.openqa.selenium.By;

import javax.annotation.Nonnull;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

public class RepositoryHooksPage extends BaseRepositoryPage {

    public RepositoryHooksPage(String projectKey, String repositorySlug) {
        super(projectKey, repositorySlug);
    }

    @Override
    public String getUrl() {
        return String.format("/projects/%s/repos/%s/settings/hooks", projectKey, slug);
    }

    private void showActions() {
        javascriptExecutor.executeScript("jQuery('.edit-button').css('visibility', 'visible');");
    }

    public List<HookRow> getPreReceiveHookRows() {
        return getHooks(By.cssSelector("#pre-receive-hook-table tbody tr"));
    }

    public List<HookRow> getPostReceiveHookRows() {
        return getHooks(By.cssSelector("#post-receive-hook-table tbody tr"));
    }

    private List<HookRow> getHooks(By selector) {
        return Lists.newArrayList(Lists.transform(elementFinder.findAll(selector), new Function<PageElement, HookRow>() {
            public HookRow apply(PageElement row) {
                return pageBinder.bind(HookRow.class, row, RepositoryHooksPage.this);
            }
        }));
    }

    public void loadNextPreReceivePage() {
        elementFinder.find(By.cssSelector("#pre-receive-hook-table tfoot tr td .load-more")).click();
        Poller.waitUntilFalse(elementFinder.find(By.cssSelector(".spinner div")).timed().isVisible());
    }

    public static class HookRow extends AbstractElementPageObject {

        private final RepositoryHooksPage parent;

        public HookRow(@Nonnull PageElement row, @Nonnull RepositoryHooksPage parent) {
            super(row);
            this.parent = checkNotNull(parent, "parent");
        }

        public String getName() {
            return find(By.className("hook-name")).getText();
        }

        public String getDescription() {
            return find(By.className("hook-description")).getText();
        }

        public boolean hasEmptyDescription() {
            return find(By.cssSelector(".hook-description .empty-description")).isPresent();
        }

        public boolean isEnabled() {
            return Boolean.valueOf(find(By.className("mode-enabled")).getAttribute("aria-pressed"));
        }

        public void enable() {
            find(By.className("mode-enabled")).click();
        }

        public void disable() {
            find(By.className("mode-disabled")).click();
        }

        public RepositoryHookDialog configure() {
            parent.showActions();
            find(By.className("edit-button")).click();
            return pageBinder.bind(RepositoryHookDialog.class, parent.projectKey, parent.slug);
        }
    }

}
