package com.intland.codebeamer.functionaltest.page;

import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;
import com.intland.codebeamer.functionaltest.page.mystart.MyStartPage;
import com.intland.codebeamer.functionaltest.page.systemadmin.UsersPage;
import org.openqa.selenium.By;

import java.security.InvalidParameterException;
import java.util.List;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selectors.byValue;
import static com.codeborne.selenide.Selenide.*;

public class CreateUserPage extends BasePage {

    public enum Group {
        SYSTEM_ADMIN("System Administrator"),
        REGULAR_USER("Regular User"),
        EXTERNAL("External"),
        GUEST("Guest"),
        SUPPORT_USER("Support User");


        private final String text;

        Group(String s) {
            text = s;
        }

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

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

    public enum Status {
        ACTIVATED("Activated"),
        DISABLED("Disabled"),
        IN_ACTIVATION("In activation");

        final String text;

        Status(String text) {
            this.text = text;
        }

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

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


    public MyStartPage clickOnSave() {
        $(byValue("Save")).click();
        sleep(1000);
        return page(MyStartPage.class);
    }

    public void setUserName(String userName) {
        $(By.id("user.name")).setValue(userName);
    }

    public void setPassword(String password) {
        $(By.id("user.password")).setValue(password);
    }

    public void setConfirmPassword(String password) {
        $(By.id("password2")).setValue(password);
    }

    public void setFirstName(String firstName) {
        $(By.id("user.firstName")).setValue(firstName);
    }

    public void setLastName(String lastName) {
        $(By.id("user.lastName")).setValue(lastName);
    }

    public void setEmail(String email) {
        $(By.id("user.email")).setValue(email);
    }

    public void setLicenseTypes(List<LicenseType> licenseTypes, boolean exactMatch) {
        if (licenseTypes.size() > 6) throw new InvalidParameterException("Maximum 6 licenseTypes are allowed");
        ElementsCollection licenseTypeRows = $$("tr.odd");
        //clear checkboxes
        if (exactMatch) {
            for (SelenideElement row : licenseTypeRows) {
                row.$(By.xpath(".//td[@class='checkbox-column-minwidth']/input")).setSelected(false);
            }
        }

        for (SelenideElement row : licenseTypeRows) {
            for (LicenseType licenseType : licenseTypes) {
                if (row.$(".textData.columnSeparator").getText().equals(licenseType.getProduct().product)) {
                    row.$(By.xpath(".//td[@class='checkbox-column-minwidth']/input")).setSelected(true);
                    row.$(By.xpath(".//td[@class='textData']/select")).shouldBe(visible).selectOption(licenseType.getLicense().license);
                }
            }
        }
    }

    public void selectStatus(Status status) {
        $(By.id("user.status")).selectOption(status.text);
    }

    public void tickGroups(boolean exactMatch, Group... groups) {
        if (groups.length > 5) throw new InvalidParameterException("Maximum 5 groups are allowed");
        ElementsCollection labels = $$(By.xpath(".//td[@id='rolesList']//label"));
        //clear checkboxes
        if (exactMatch) {
            for (SelenideElement label : labels) {
                label.$(By.xpath("./input[@type='checkbox']")).setSelected(false);
            }
        }

        for (SelenideElement label : labels) {
            for (Group group : groups) {
                if (label.getText().equals(group.text)) {
                    label.$(By.xpath("./input[@type='checkbox']")).setSelected(true);
                }
            }
        }
    }

    public static class LicenseType {

        public enum Product {
            AG("AG"),//Agile
            ALM("ALM"),//Application Lifecyle Management
            CM("CM"),//Development Management (former CM)
            COLLAB("Collab"),//Collaboration Platform
            DR("DR"),//Document Reviewer - only View, Comment abilities
            QA_TEST("QA-Test"),//QA/Test Management
            RM("RM"),//Requirements Management
            SD("SD");//Service Desk

            final String product;

            Product(String product) {
                this.product = product;
            }

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

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

        public enum License {
            FLOATING("Users with floating license"),
            NAMED("Users with named license");

            private final String license;

            License(String license) {
                this.license = license;
            }

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

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

        private Product product;
        private License license;

        public LicenseType(Product product, License license) {
            this.product = product;
            this.license = license;
        }


        public Product getProduct() {
            return product;
        }

        public License getLicense() {
            return license;
        }

    }
}
