package com.intland.codebeamer.functionaltest.page.systemadmin;

import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.impl.WebElementsCollection;
import com.intland.codebeamer.functionaltest.page.BasePage;
import com.intland.codebeamer.functionaltest.page.CreateUserPage;
import com.intland.codebeamer.functionaltest.page.mystart.MyStartPage;
import com.intland.codebeamer.functionaltest.page.mystart.EditAccountPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.ArrayList;
import java.util.List;

import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.$$;
import static com.codeborne.selenide.Selenide.page;
import static org.testng.Assert.assertNotNull;

public class UsersPage extends BasePage {

    public CreateUserPage clickOnNewAccount() {
        $(By.linkText("New Account")).click();
        return page(CreateUserPage.class);
    }

    public void fillFilter(String text) {
        $("#userFilter").sendKeys(text);
    }

    public void clickOnGo() {
        $("#filterBtn").click();
    }

    public void filter(String text) {
        fillFilter(text);
        clickOnGo();
    }

    /**
     * WARNING it can handle only one page
     *
     * @return
     */
    public ElementsCollection getAllRows() {
        ElementsCollection rows = $$(By.xpath(".//table[@id='user']/tbody//tr"));
        if (rows.first().getAttribute("class").contains("empty")) {
            return new ElementsCollection(new WebElementsCollection() {
                @Override
                public List<WebElement> getActualElements() {
                    return new ArrayList<>();
                }

                @Override
                public String description() {
                    return null;
                }

            });
        }
        return rows;
    }

    public boolean isUserInTheList(String username) {
        for (SelenideElement row : getAllRows()) {
            if (username.equals(row.find(By.xpath(".//td[@class='textData']/a")).getText())) return true;
        }
        return false;
    }

    public SelenideElement hoverOverUser(String username){
        SelenideElement usersRow = null;
        for (SelenideElement row : getAllRows()) {
            if (username.equals(row.find(By.xpath(".//td[@class='textData']/a")).getText())){
                usersRow = row;
                break;
            }
        }
        return usersRow;
    }

    public EditAccountPage clickOnEditInContextMenu(String username){
        SelenideElement usersRow = hoverOverUser(username);
        assertNotNull(usersRow, "User with username: " + username + "cannot be found");
        //TODO retry
        usersRow.hover();
        usersRow.$(".menuArrowDown").click();
        usersRow.$(By.linkText("Edit")).click();

        return page(EditAccountPage.class);
    }

    public boolean doesUserExist(String username){
//        MyStartPage myStartPage = Helper.loginWithSystemAdmin();
//        SystemAdminPage systemAdminPage = myStartPage.clickOnSystemAdmin();
//        UsersPage usersPage = systemAdminPage.clickOnUserAccounts();
        filter(username);
        return isUserInTheList(username);
    }

    public MyStartPage createUser(String userName, String password, String email, CreateUserPage.Group... groups ) {
        CreateUserPage createUserPage = clickOnNewAccount();
        createUserPage.setUserName(userName);
        createUserPage.setPassword(password);
        createUserPage.setConfirmPassword(password);
        createUserPage.setFirstName(password);
        createUserPage.setLastName(password);
        createUserPage.setEmail(email);
        createUserPage.tickGroups(true, groups);
        createUserPage.clickOnSave();
        return page(MyStartPage.class);
    }
}
