package org.alfresco.po.share;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.alfresco.po.RenderElement.getVisibleRenderElement;

import org.alfresco.po.RenderTime;
import org.alfresco.po.exception.PageException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;

/**
 * @author Olga Antonik
 */
public class AddUserToGroupForm extends SharePage
{
    private Log logger = LogFactory.getLog(this.getClass());
    private static final By USER_FINDER_INPUT = By.cssSelector("input[id*='search-peoplefinder-search-text']");
    private static final By USER_SEARCH_BUTTON = By.cssSelector("button[id*='search-peoplefinder-search-button-button']");
    private static final By ADD_USER_FORM = By.cssSelector("div[id*='peoplepicker_c']");
    private static final String ADD_BUTTON = "//tbody[@class='yui-dt-data']/tr//a[contains(text(),'%s')]/../../../..//button";
    private static final By CLOSE_X = By.xpath("//div[contains(@id,'-peoplepicker')]/a");

    /**
     * (non-Javadoc)
     *
     * @see org.alfresco.po.Render#render()
     */
    @SuppressWarnings("unchecked")
    @Override
    public AddUserToGroupForm render(RenderTime renderTime)
    {
        elementRender(renderTime, getVisibleRenderElement(USER_FINDER_INPUT), getVisibleRenderElement(USER_SEARCH_BUTTON));
        return this;
    }

    /**
     * (non-Javadoc)
     *
     * @see org.alfresco.po.Render#render()
     */
    @SuppressWarnings("unchecked")
    @Override
    public AddUserToGroupForm render()
    {
        return render(new RenderTime(maxPageLoadingTime));
    }
    AddUserToGroupForm addUserToGroupForm;
    GroupsPage groupsPage;
    /**
     * Search user
     *
     * @param user String
     * @return AddUserToGroupForm
     */
    public AddUserToGroupForm searchUser(String user)
    {
        try
        {
            WebElement searchField = findAndWait(USER_FINDER_INPUT);
            searchField.clear();
            searchField.sendKeys(user);
            findAndWait(USER_SEARCH_BUTTON).click();

        }
        catch (TimeoutException te)
        {
            logger.error("Failed to find user search input field");
        }
        return addUserToGroupForm.render();
    }

    /**
     * Click to the Add button
     *
     * @param user String
     * @return GroupsPage
     */
    private GroupsPage clickAddButton(String user)
    {

        try
        {
            WebElement addButton = findAndWait(By.xpath(String.format(ADD_BUTTON, user)));
            addButton.click();

        }
        catch (TimeoutException e)
        {
            throw new PageException("Not found Element: Add User", e);
        }

        return groupsPage.render();
    }

    /**
     * Search and add user to the another group
     *
     * @param user String
     * @return GroupsPage
     */
    public GroupsPage addUser(String user)
    {
        try
        {
            checkNotNull(user);
            findAndWait(ADD_USER_FORM);
            AddUserToGroupForm addUserForm = searchUser(user).render();
            return addUserForm.clickAddButton(user).render();

        }
        catch (TimeoutException e)
        {
            throw new PageException("Add User form is not open", e);
        }

    }

    public void closeForm()
    {
        findAndWait(CLOSE_X).click();
    }

}
