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

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.ex.UIAssertionError;
import com.google.common.base.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.codeborne.selenide.Selenide.*;

/**
 * Copyright by Intland Software
 * <p>
 * All rights reserved.
 * <p>
 * This software is the confidential and proprietary information
 * of Intland Software. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Intland.
 */
public class AddNewMemberDialog {

    public enum Role {
        DEVELOPER("Developer"),
        PRODUCT_OWNER("Product Owner"),
        PROJECT_ADMIN("Project Admin"),
        ROLE_WITH_FOUR_USERS("role with four users"),
        SCRUM_MASTER("Scrum Master"),
        STAKEHOLDER("Stakeholder"),
        TEST_ENGINEER("Test Engineer"),
        TESTER("Tester"),
        TEST_LEAD("Test Lead");

        private final String text;

        Role(String s) {
            text = s;
        }

        public boolean equalsName(String otherSelector) {
            return (otherSelector == null) ? false : text.equals(otherSelector);
        }

        public String toString() {
            return this.text;
        }
    }

    public void clickOnAdd() {
        $(By.name("ADD")).click();
    }

    public void clickOnCancel() {
        $(By.xpath(".//a[text()='Cancel']")).click();
    }

    public void fillUser(String username) {
        Wait().withTimeout(10, TimeUnit.SECONDS)
                .pollingEvery(2500, TimeUnit.MILLISECONDS)
                .ignoring(NoSuchElementException.class)
                .ignoring(UIAssertionError.class)
                .until((Function<WebDriver, Boolean>) driver -> {
                    SelenideElement input = $(By.xpath(".//ul[@class='token-input-list-facebook']//input"));
                    input.clear();
                    input.sendKeys(username);
                    sleep(2000);
                    ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                    if (collection.isEmpty()) {
                        return false;
                    }
                    collection.first().click();
                    return true;
                });
    }

    public void clickOnAddNewMemberFromCodebeamerTab(){
        $("#addMemberTab-tab").click();
    }

    public void tickRoles(List<Role> roles, boolean exactMatch) {
        ElementsCollection checkboxes = $$(By.xpath(".//div[@id='addMemberTab']//div[@class='roles']/div/*"));

        if (exactMatch) {
            for (SelenideElement checkbox : checkboxes) {
                checkbox.$("input").setSelected(false);
            }
        }

        for (SelenideElement checkbox: checkboxes) {
            for (Role role : roles) {
                if (checkbox.$("label").getText().equals(role.text)){
                    checkbox.$("input").setSelected(true);
                }
            }
        }
    }
}
