package com.intland.codebeamer.functionaltest.test;

import com.codeborne.selenide.CollectionCondition;
import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.common.*;
import com.intland.codebeamer.functionaltest.page.mystart.MyStartPage;
import com.intland.codebeamer.functionaltest.page.projects.ProjectsPage;
import com.intland.codebeamer.functionaltest.page.systemadmin.ProjectListPage;
import com.intland.codebeamer.functionaltest.page.systemadmin.SystemAdminPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackersPage;
import com.intland.codebeamer.functionaltest.page.wiki.*;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

import static com.codeborne.selenide.Condition.*;
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 firstSixProductLicense agreement
 * you entered into with Intland.
 */
public class WikiPageTests extends BaseTest {

    public static final String HIERARCHY = "hierarchy";
    public static final String ADVANCED = "advanced";
    public static final String WOCONTENT = "wocontent";
    private MyStartPage myStartPage;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    private ProjectWikiPage projectWikiPage;
    private String requirementTrackerId;
    private String projectId1, projectId2;
    private final String projectName2 = "test-project-sel-526861927 1";

    @BeforeClass(groups = "mywiki")
    public void beforeClass() {
        myStartPage = Helper.loginWithSystemAdmin();
        SystemAdminPage systemAdminPage = myStartPage.clickOnSystemAdmin();
        ProjectListPage projectListPage = systemAdminPage.clickOnProjectList();
        projectId1 = projectListPage.getProjectId(projectName);
        projectId2 = projectListPage.getProjectId(projectName2);

        ProjectsPage projectsPage = projectListPage.clickOnProjects();
        projectsPage.clickOnCompactList();
        projectWikiPage = projectsPage.clickOnProject("test-project-GEN-0001000 selenide");
        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        requirementTrackerId = trackersPage.getTrackerId("Requirements");
    }

    @BeforeMethod(groups = {"mywiki"})
    public void beforeMethodMyWiki() {
        myStartPage = Helper.loginWithSystemAdmin();
        myStartPage.clickOnMyStartPage();
        //myStartPage.leftPane.deleteAllLeftTreeItemExceptDummy();
    }

    @BeforeMethod(groups = {"projectwiki"})
    public void beforeMethodProjectWiki() {
        myStartPage = Helper.loginWithSystemAdmin();
        //myStartPage.centerPane.waitForCenterPaneToLoad();
        ProjectsPage projectsPage = myStartPage.clickOnProjects();
        projectsPage.clickOnCompactList();
        projectWikiPage = projectsPage.clickOnProject(projectName);
        //projectWikiPage.leftPane.deleteAllLeftTreeItemExceptDummy();
    }

    //https://codebeamer.com/cb/issue/693753
    @Test(groups = {"permissions"})
    public void checkThePermissionsOfTheProjectWikiPage() {
        beforeMethodProjectWiki();
        WikiPropertiesPage wikiPropertiesPage = (WikiPropertiesPage) projectWikiPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.PROPERTIES);
        WikiPropertiesPage.PermissionsTab permissionsTab = (WikiPropertiesPage.PermissionsTab) wikiPropertiesPage.clickOnTab(WikiPropertiesPage.TabType.PERMISSIONS);
        permissionsTab.tickPermission(WikiPropertiesPage.PermissionsTab.Role.DEVELOPER, WikiPropertiesPage.PermissionsTab.Permission.EDIT, false);
        permissionsTab.clickOnSave();
        refresh();
        sleep(2000);
        permissionsTab.checkIfPermission(WikiPropertiesPage.PermissionsTab.Role.DEVELOPER, WikiPropertiesPage.PermissionsTab.Permission.EDIT, not(selected));
        //revert
        permissionsTab.tickPermission(WikiPropertiesPage.PermissionsTab.Role.DEVELOPER, WikiPropertiesPage.PermissionsTab.Permission.EDIT, true);
        permissionsTab.clickOnSave();
    }

    //https://codebeamer.com/cb/issue/693753
    @Test(groups = {"permissions"})
    public void checkThePermissionsOfTheMyWikiPage() {
        beforeMethodMyWiki();
        WikiPropertiesPage wikiPropertiesPage = (WikiPropertiesPage) myStartPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.PROPERTIES);
        wikiPropertiesPage.checkIfTab(WikiPropertiesPage.TabType.PERMISSIONS, hidden);
    }

    //https://codebeamer.com/cb/issue/672860
    @Test(groups = {"mywiki"})
    public void checkTheWikiPageCreationProcessPageWoContent() {
        myStartPage.leftPane.checkIfRootNode(true);
        NewChildWikiDialog newChildWikiDialog = myStartPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        String pageName = WOCONTENT + dateFormat.format(new Date());
        newChildWikiDialog.fillPageName(pageName);
        newChildWikiDialog.clickOnAdd();
        switchTo().defaultContent();
        myStartPage.centerPane.waitForCenterPaneToLoad();
        //myStartPage.leftPane.getAllLeftTreeItems().shouldHave(CollectionCondition.texts("bond's Personal Wiki", "dummyDontDeleteIt", pageName));
        myStartPage.leftPane.checkIfBranchIsPartOfLeftTree("bond's Personal Wiki", 1, Pattern.compile("\\[\\{\"text\":\"bond's Personal Wiki\",\"children\":\\[.*\\{\"text\":\"" + pageName + "\",\"children\":\\[\\]\\}.*\\]\\}\\]"));
    }

    //https://codebeamer.com/cb/issue/672860
    @Test(groups = {"projectwiki"})
    public void checkTheWikiPageCreationProcessMultiplePagesWoFormatting() throws IOException {
        NewChildWikiDialog newChildWikiDialog = projectWikiPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        String pageName1 = "multiple" + dateFormat.format(new Date());
        newChildWikiDialog.fillPageName(pageName1);
        MarkupTab markupTab = (MarkupTab) newChildWikiDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.fillComment("unformatted text");
        newChildWikiDialog.clickOnAdd();
        switchTo().defaultContent();
        sleep(1000);
        Image imgToCompareWithOriginal = Helper.takeScreenshotOfElement($(By.xpath("//span[@class='wikiContent']")), "checkTheWikiPageCreationProcessMultiplePagesWoFormatting_1.png");
        Image imgOriginal = ImageIO.read(new File("checkTheWikiPageCreationProcessMultiplePagesWoFormatting_1.png"));
        Assert.assertTrue(Helper.compareImages(imgOriginal, imgToCompareWithOriginal, null));
        projectWikiPage.leftPane.clickOnLeftTreeItem("Welcome to " + projectName, 1);
        newChildWikiDialog = projectWikiPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        pageName1 = "multiple" + dateFormat.format(new Date());
        newChildWikiDialog.fillPageName(pageName1);
        markupTab = (MarkupTab) newChildWikiDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.fillComment("unformatted text");
        newChildWikiDialog.clickOnAdd();
        switchTo().defaultContent();
        imgToCompareWithOriginal = Helper.takeScreenshotOfElement($(By.xpath("//span[@class='wikiContent']")), null);
        imgOriginal = ImageIO.read(new File("checkTheWikiPageCreationProcessMultiplePagesWoFormatting_1.png"));
        Assert.assertTrue(Helper.compareImages(imgOriginal, imgToCompareWithOriginal, null));
    }

    //https://codebeamer.com/cb/issue/672860
    @Test(groups = {"mywiki"})
    public void checkTheWikiPageCreationProcessMultiplePagesWithHierarchy() throws IOException {
        myStartPage.leftPane.checkIfRootNode(true);
        NewChildWikiDialog newChildWikiDialog = myStartPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        String pageName1 = HIERARCHY + dateFormat.format(new Date());
        newChildWikiDialog.fillPageName(pageName1);
        MarkupTab markupTab = (MarkupTab) newChildWikiDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.fillComment("!1 h1\n" +
                "\n" +
                "h1 content\n" +
                "\n" +
                "!2 h2\n" +
                "* 1\n" +
                "* 2\n" +
                "* 3\n" +
                "!3 h3\n" +
                "\n" +
                "h3 content\n" +
                "\n" +
                "!4 h4\n");
        newChildWikiDialog.clickOnAdd();
        switchTo().defaultContent();
        sleep(1000);//needs some wait because the dom changes
        Image imgToCompareWithOriginal = Helper.takeScreenshotOfElement($(By.xpath("//span[@class='wikiContent']/div")), null);
        Image imgOriginal = ImageIO.read(new File("checkTheWikiPageCreationProcessMultiplePagesWithHierarchy_1.png"));
        Assert.assertTrue(Helper.compareImages(imgOriginal, imgToCompareWithOriginal, null));

        newChildWikiDialog = myStartPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        String pageName2 = HIERARCHY + dateFormat.format(new Date());
        newChildWikiDialog.fillPageName(pageName2);
        markupTab = (MarkupTab) newChildWikiDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.fillComment("!1 h1\n" +
                "\n" +
                "h1 content\n" +
                "\n" +
                "!2 h2\n" +
                "* 1\n" +
                "* 2\n" +
                "* 3\n" +
                "!3 h3\n" +
                "\n" +
                "h3 content\n" +
                "\n" +
                "!4 h4\n");
        newChildWikiDialog.clickOnAdd();
        switchTo().defaultContent();
        sleep(1000);
        imgToCompareWithOriginal = Helper.takeScreenshotOfElement($(By.xpath("//span[@class='wikiContent']/div")), "c:\\images\\checkTheWikiPageCreationProcessMultiplePagesWithHierarchy_1.png");
        imgOriginal = ImageIO.read(new File("checkTheWikiPageCreationProcessMultiplePagesWithHierarchy_1.png"));
        Assert.assertTrue(Helper.compareImages(imgOriginal, imgToCompareWithOriginal, null));
        myStartPage.leftPane.checkIfBranchIsPartOfLeftTree(pageName1, 1, Pattern.compile("\\{\"text\":\"" + pageName1 + "\",\"children\":\\[.*\\{\"text\":\"" + pageName2 + "\",\"children\":\\[\\]\\}.*\\]\\}"));
        //myStartPage.leftPane.getAllLeftTreeItems().shouldHave(CollectionCondition.texts("bond's Personal Wiki", "dummyDontDeleteIt", pageName1, pageName2));

    }

    //https://codebeamer.com/cb/issue/1011590
    @Test(groups = {"projectwiki"})
    public void checkTheWikiPageCreationProcessPageWithAdvancedFormatting() throws IOException {
        NewChildWikiDialog newChildWikiDialog = projectWikiPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        String pageName1 = "advanced 국제우승 결승전" + dateFormat.format(new Date());
        newChildWikiDialog.fillPageName(pageName1);
        MarkupTab markupTab = (MarkupTab) newChildWikiDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.fillComment("!1 h1\n" +
                "[{Table\n" +
                "\n" +
                "|Red\n" +
                "|Blue\n" +
                "|Green\n" +
                "\n" +
                "|#\n" +
                "|2\n" +
                "|3\n" +
                "}]\n" +
                "\n" +
                "\n" +
                "%%(color:#ff6600;background-color:#000000;)colored text %!(/)\n" +
                "\n" +
                "[https://www.google.hu]");
        newChildWikiDialog.clickOnAdd();
        switchTo().defaultContent();
        sleep(1000);
        Image imgToCompareWithOriginal = Helper.takeScreenshotOfElement($(By.xpath("//span[@class='wikiContent']/div")), null);
        Image imgOriginal = ImageIO.read(new File("checkTheWikiPageCreationProcessPageWithAdvancedFormatting_1.png"));
        Assert.assertTrue(Helper.compareImages(imgOriginal, imgToCompareWithOriginal, null));
    }

    //https://codebeamer.com/cb/issue/672861
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessPageWoContent"})
    public void checkModificationProcessAddTableTextDecorations() throws IOException {
        myStartPage.leftPane.clickOnLeftTreeItem(WOCONTENT, 1);
        UpdateWikiPageDialog updateWikiPageDialog = myStartPage.centerPane.clickOnEdit();
        Helper.switchToWindowByRegex(WOCONTENT);
        MarkupTab markupTab = (MarkupTab) updateWikiPageDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.fillComment("!1 h1\n" +
                "[{Table\n" +
                "\n" +
                "|Red\n" +
                "|Blue\n" +
                "|Green\n" +
                "\n" +
                "|#\n" +
                "|2\n" +
                "|3\n" +
                "}]\n" +
                "\n" +
                "\n" +
                "%%(color:#ff6600;background-color:#000000;)colored text %!(/)\n" +
                "\n" +
                "[https://www.google.hu]"
        );
        updateWikiPageDialog.clickOnSaveAndClose();
        switchTo().window(0);
        sleep(10000);//wait for rendering to finish
        Image imgToCompareWithOriginal = Helper.takeScreenshotOfElement($(By.xpath("//span[@class='wikiContent']/div")), null);
        Image imgOriginal = ImageIO.read(new File("checkTheWikiPageCreationProcessPageWithAdvancedFormatting_1.png"));
        Assert.assertTrue(Helper.compareImages(imgOriginal, imgToCompareWithOriginal, null));
    }

    //https://codebeamer.com/cb/issue/672861
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessMultiplePagesWithHierarchy"})
    public void checkModificationProcessAddImage() throws IOException {
        //Click on the pencil icon next to a header line.
        EditorComponent editorComponent = myStartPage.centerPane.editInlineByPencil(HIERARCHY, 1, "h1");
        MarkupTab markupTab = (MarkupTab) editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        //Add two images: one in WYSIWYG mode (just insert it) and an other in Markup mode (base64 - you can find a sample in the attached txt) then click on "Save" icon.
        markupTab.fillComment(Keys.chord(Keys.CONTROL, Keys.END), "[{Image src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyAgMAAABjUWAiAAAACVBMVEUAqF0vuHsNrGXlCLtEAAAAFElEQVQoz2MYBUMJrJoaQDZv2AEAUzcHzLwn4cQAAAAASUVORK5CYII=' alt='' }]\n");
        WYSIWYGTab wysiwygTab = (WYSIWYGTab) editorComponent.clickOnTab(EditorComponent.Tab.WYSIWYG);
        Helper.insertImageIntoClipboard("checkTheWikiPageCreationProcessPageWithAdvancedFormatting_1.png");
        wysiwygTab.fillDescription(false, Keys.chord(Keys.CONTROL, Keys.END), Keys.CONTROL + "v");
        wysiwygTab.clickOnSave();
        sleep(5000);//needs some wait because the dom changes
        Image imgToCompareWithOriginal = Helper.takeScreenshotOfElement($(By.xpath("//div[@id='pagecontent']")), null);
        Image imgOriginal = ImageIO.read(new File("checkModificationProcessAddImage_1.png"));
        Assert.assertTrue(Helper.compareImages(imgOriginal, imgToCompareWithOriginal, null));

    }

    //https://codebeamer.com/cb/issue/672861
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessMultiplePagesWithHierarchy"})
    public void checkModificationProcessEditLink() {
        myStartPage.leftPane.clickOnLeftTreeItem(HIERARCHY, 2);
        UpdateWikiPageDialog updateWikiPageDialog = myStartPage.centerPane.clickOnEdit();
        Helper.switchToWindowByRegex(HIERARCHY);
        MarkupTab markupTab = (MarkupTab) updateWikiPageDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.fillComment("[{ProjectList}]\n" +
                "[{ProjectInfo projectId='" + projectId1 + "'}]\n" +
                "[{Members projectId='" + projectId1 + "'}]\n" +
                "[{TrackerList projectId='" + projectId1 + "'}]\n" +
                "[{MostActiveProjects period='Last 30 days' max='5'}]\n" +
                "[{ProjectActivityTrends projectId='" + projectId1 + "' period='Last 30 days' grouping='Daily'}]\n" +
                "[{ActivityStream projectId='" + projectId1 + "' title='Recently in the Development Project' cssStyle='-moz-box-shadow: 0 0 1em gray;' filter='issue' period='Last 3 months' max='10'}]\n" +
                "[{ArtifactActivityTrends artifactId='1000,1002' display='table'}]\n" +
                "[{IssueCountTrends projectId='" + projectId1 + ",2' display='table'}]\n" +
                "[{IssueCountByField projectId='" + projectId1 + "' display='table'}]\n" +
                "[{ReleaseActivityTrends projectId='" + projectId1 + "'}]\n" +
                "[{ReleaseGanttChart releaseIds='12345'}]\n" +
                "[{RiskMatrixDiagram trackerId='" + requirementTrackerId + "'}]\n" +
                "[{ResourcingExtraction projectIds='" + projectId1 + "' displayGroups='true'}]\n" +
                "[{MyOpenTrackerItemsPlugin title='New title'}]\n" +
                "[{TrackerStateTransitionChart trackerId='" + requirementTrackerId + "'}]\n" +
                "[{TrackerClassDiagram projectId='2' trackerId='" + requirementTrackerId + "'}]");
        updateWikiPageDialog.clickOnSaveAndClose();
        switchTo().window(0);
        myStartPage.centerPane.getPageErrorMessages().shouldHaveSize(0);
    }

    //https://codebeamer.com/cb/issue/672862
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkModificationProcessAddTableTextDecorations"})
    public void checkModificationProcessRemoveTableTextDecorations() {
        myStartPage.leftPane.clickOnLeftTreeItem(WOCONTENT, 1);
        UpdateWikiPageDialog updateWikiPageDialog = myStartPage.centerPane.clickOnEdit();
        Helper.switchToWindowByRegex(WOCONTENT);
        MarkupTab markupTab = (MarkupTab) updateWikiPageDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.clearComment();
        updateWikiPageDialog.clickOnSaveAndClose();
        switchTo().window(0);
        sleep(5000);//needs some wait because the dom changes
        Assert.assertEquals($(By.xpath("//div[@id='pagecontent']")).getCssValue("height"), "0px");
    }

    //https://codebeamer.com/cb/issue/672862
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkModificationProcessAddImage"})
    public void checkModificationProcessRemoveImage() {
        //Click on the pencil icon next to a header line.
        EditorComponent editorComponent = myStartPage.centerPane.editInlineByPencil(HIERARCHY, 1, "h1");
        MarkupTab markupTab = (MarkupTab) editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.clearComment();
        markupTab.clickOnSave();
        sleep(5000);//needs some wait because the dom changes
        Assert.assertEquals($(By.xpath("//div[@id='pagecontent']")).getCssValue("height"), "0px");
    }

    //https://codebeamer.com/cb/issue/672862
    @Test(groups = {"projectwiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessMultiplePagesWoFormatting"})
    public void checkModificationProcessRename() {
        projectWikiPage.leftPane.rename("multiple", 1, "renametest");
        sleep(3000);//waits for reordering
        myStartPage.leftPane.checkIfBranchIsPartOfLeftTree("Welcome to " + projectName, 1, Pattern.compile("\\{\"text\":\"Welcome to " + projectName + "\",\"children\":\\[.*\\{\"text\":\"multiple.*\",\"children\":\\[\\]},\\{\"text\":\"renametest\",\"children\":\\[\\]\\}.*\\]\\}"));
    }

    //https://codebeamer.com/cb/issue/672863
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessMultiplePagesWithHierarchy", "checkTheWikiPageCreationProcessPageWoContent"})
    public void checkModificationProcessMoveParentPageDown() {
        myStartPage.leftPane.dragAndDrop(new String[]{HIERARCHY}, new int[]{1}, WOCONTENT, 1, LeftPane.DragAndDropAction.AFTER);
        myStartPage.leftPane.checkIfBranchIsPartOfLeftTree("bond's Personal Wiki", 1, Pattern.compile("\\[\\{\"text\":\"bond's Personal Wiki\",\"children\":\\[.*\\{\"text\":\"wocontent.*\",\"children\":\\[]},\\{\"text\":\"hierarchy.*\",\"children\":\\[\\{\"text\":\"hierarchy.*\",\"children\":\\[]}]}]}]"));
    }

    //https://codebeamer.com/cb/issue/672863
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessMultiplePagesWithHierarchy"})
    public void checkModificationProcessMoveOneSubPageUp() {
        myStartPage.leftPane.dragAndDrop(new String[]{HIERARCHY}, new int[]{2}, HIERARCHY, 1, LeftPane.DragAndDropAction.AFTER);
        myStartPage.waitForPageToLoad();
        myStartPage.leftPane.checkIfBranchIsPartOfLeftTree("bond's Personal Wiki", 1, Pattern.compile("\\[\\{\"text\":\"bond's Personal Wiki\",\"children\":\\[.*\\{\"text\":\"hierarchy.*\",\"children\":\\[\\]\\},\\{\"text\":\"hierarchy.*\",\"children\":\\[\\]\\}\\]\\}\\]"));
    }

    //https://codebeamer.com/cb/issue/672863
//    @Test(groups = {"projectwiki"}, dependsOnMethods = {/*"checkModificationProcessRename"*/})
//    public void checkModificationProcessMoveMultiplePagesUp() throws IOException {
//        //TODO refactor drag and drop to handle multiple items at once
//        myStartPage.leftPane.dragAndDrop(new String[]{"multiple", "renametest"}, new int[]{1, 1}, "advanced", 1, LeftPane.DragAndDropAction.BEFORE);
//        myStartPage.leftPane.checkIfBranchIsPartOfLeftTree("Welcome to "+projectName, 1, Pattern.compile("\\[\\{\"text\":\"bond's Personal Wiki\",\"children\":\\[.*\\{\"text\":\"hierarchy.*\",\"children\":\\[\\]\\},\\{\"text\":\"hierarchy.*\",\"children\":\\[\\]\\}\\]\\}\\]"));
//    }

    //https://codebeamer.com/cb/issue/672863
    @Test(groups = {"projectwiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessPageWithAdvancedFormatting"})
    public void checkModificationProcessCopyBetweenProjects() {
        projectWikiPage.leftPane.clickOnLeftTreeItem(ADVANCED, 1);
        projectWikiPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.COPY);

        ProjectsPage projectsPage = projectWikiPage.clickOnProjects();
        projectsPage.clickOnCompactList();
        projectWikiPage = projectsPage.clickOnProject(projectName2);
        PasteDialog pasteDialog = (PasteDialog) projectWikiPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.PASTE);
        Helper.switchToWindowByRegex(".*Paste a copy of Wiki page.*");
        WikiPropertiesPage wikiPropertiesPage = pasteDialog.clickOnPaste();
        Helper.switchToWindowByRegex("Welcome to " + projectName2);
        wikiPropertiesPage.getInfoMessages().shouldHave(CollectionCondition.texts("Successfully copied 1 pages to Welcome to " + projectName2 + " Wiki page."));
    }

    //https://codebeamer.com/cb/issue/786217
    @Test(groups = {"mywiki"})
    public void checkFilteringOptionInWikiTreeEnglish() {
        myStartPage.leftPane.filter("dum");
        myStartPage.leftPane.getAllLeftTreeItems().shouldHave(CollectionCondition.texts("bond's Personal Wiki", "dummyDontDeleteIt"));
    }

    //https://codebeamer.com/cb/issue/786217
    @Test(groups = {"projectwiki"}, dependsOnMethods = "checkTheWikiPageCreationProcessPageWithAdvancedFormatting")
    public void checkFilteringOptionInWikiTreeKorean() {
        myStartPage.leftPane.filter("국제우");
        myStartPage.leftPane.getAllLeftTreeItems().shouldHave(Helper.matchesText("Welcome to "+projectName, "advanced 국제우승 결승전.*"));
    }

    //https://codebeamer.com/cb/issue/672865
    @Test(groups = {"mywiki"})
    public void checkErrorMessagesWithoutName() {
        NewChildWikiDialog newChildWikiDialog = myStartPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        newChildWikiDialog.clickOnAdd();
        Alert alert = switchTo().alert();
        Assert.assertEquals(alert.getText(), "\"Name\" is required!");
        alert.accept();
    }

    //https://codebeamer.com/cb/issue/672865
    @Test(groups = {"projectwiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessPageWithAdvancedFormatting"})
    public void checkErrorMessagesExistingName() {
        String existingPageName = projectWikiPage.leftPane.getAllLeftTreeItems().get(0).getText();
        NewChildWikiDialog newChildWikiDialog = projectWikiPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        newChildWikiDialog.fillPageName(existingPageName);
        newChildWikiDialog.clickOnAdd();
        newChildWikiDialog.getWarningMessages().shouldHave(Helper.containsText("there is already another page with this name"));
    }

    //https://codebeamer.com/cb/issue/672865
    @Test(groups = {"projectwiki"}, dependsOnMethods = {"checkModificationProcessCopyBetweenProjects"})
    public void checkErrorMessagesCopyBetweenProjects() {
        projectWikiPage.leftPane.clickOnLeftTreeItem(ADVANCED, 1);
        projectWikiPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.COPY);

        ProjectsPage projectsPage = projectWikiPage.clickOnProjects();
        projectsPage.clickOnCompactList();
        projectWikiPage = projectsPage.clickOnProject(projectName2);
        PasteDialog pasteDialog = (PasteDialog) projectWikiPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.PASTE);
        Helper.switchToWindowByRegex(".*Paste a copy of Wiki page.*");
        WikiPropertiesPage wikiPropertiesPage = pasteDialog.clickOnPaste();
        Helper.switchToWindowByRegex("Welcome to " + projectName2);
        wikiPropertiesPage.getErrorMessages().shouldHave(Helper.containsText(".*Selection cannot be pasted.*"));
    }

    //https://codebeamer.com/cb/issue/672865
    @Test(groups = {"projectwiki"}, dependsOnMethods = {"checkModificationProcessRename"})
    public void checkErrorMessagesRenameToExistingOne() {
        projectWikiPage.leftPane.rename(ADVANCED, 1, "renametest");
        myStartPage.centerPane.getOverlayErrorMessages().shouldHave(Helper.matchText(".*page with the same name already exists..*"));
    }

    //https://codebeamer.com/cb/issue/672864
    @Test(groups = {"projectwiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessPageWithAdvancedFormatting", "checkTheWikiPageCreationProcessMultiplePagesWoFormatting"})
    public void checkDeleteProcessSubPage() {
        //precondition makes a hierarchy
        projectWikiPage.leftPane.dragAndDrop(new String[]{"multiple"}, new int[]{1}, ADVANCED, 1, LeftPane.DragAndDropAction.INTO);
        projectWikiPage.leftPane.checkIfBranchIsPartOfLeftTree(ADVANCED, 1, Pattern.compile("\\{\"text\":\"advanced.*\",\"children\":\\[\\{\"text\":\"multiple.*\",\"children\":\\[]}]}"));

        projectWikiPage.leftPane.clickOnLeftTreeItem("multiple", 1);
        ConfirmationDialogPage confirmationDialogPage = (ConfirmationDialogPage) projectWikiPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.DELETE);
        confirmationDialogPage.clickOnYes();

        projectWikiPage.leftPane.checkIfBranchIsPartOfLeftTree(ADVANCED, 1, Pattern.compile("\\{\"text\":\"advanced.*\",\"children\":\\[]}"));
    }

    //https://codebeamer.com/cb/issue/672864
    @Test(groups = {"projectwiki"}, dependsOnMethods = {"checkTheWikiPageCreationProcessPageWithAdvancedFormatting", "checkModificationProcessRename"})
    public void checkDeleteProcessMultiplePages() {

        projectWikiPage.leftPane.clickOnMultipleLeftTreeItems(new String[]{ADVANCED, "renametest"}, new int[]{1, 1});
        ConfirmationDialogPage confirmationDialogPage = (ConfirmationDialogPage) projectWikiPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.DELETE);
        confirmationDialogPage.clickOnYes();

        projectWikiPage.leftPane.getAllLeftTreeItems().filter(text(ADVANCED)).shouldHave(CollectionCondition.sizeGreaterThan(0));
        projectWikiPage.leftPane.getAllLeftTreeItems().filter(text("renametest")).shouldHaveSize(0);
    }

    //https://codebeamer.com/cb/issue/672864
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkModificationProcessRemoveTableTextDecorations"})
    public void checkDeleteProcessParentWithoutSubPages() {
        myStartPage.leftPane.clickOnLeftTreeItem(WOCONTENT, 1);
        ConfirmationDialogPage confirmationDialogPage = (ConfirmationDialogPage) myStartPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.DELETE);
        confirmationDialogPage.clickOnYes();

        myStartPage.leftPane.getAllLeftTreeItems().filter(text(WOCONTENT)).shouldHaveSize(0);
    }

    //https://codebeamer.com/cb/issue/672864
    @Test(groups = {"mywiki"}, dependsOnMethods = {"checkModificationProcessRemoveImage"})
    public void checkDeleteProcessParentWithSubPages() {
        //precondition
        myStartPage.leftPane.clickOnLeftTreeItem(HIERARCHY, 1);
        myStartPage.centerPane.waitForCenterPaneToLoad();
        sleep(4000);//else way it adds the new dashboard to the root
        NewChildWikiDialog newChildWikiDialog = myStartPage.centerPane.clickOnNewChildDashboard();
        switchTo().frame("inlinedPopupIframe");
        newChildWikiDialog.fillPageName("todelete");
        newChildWikiDialog.clickOnAdd();
        switchTo().defaultContent();
        myStartPage.leftPane.dragAndDrop(new String[]{HIERARCHY}, new int[]{2}, HIERARCHY, 1, LeftPane.DragAndDropAction.INTO);
        myStartPage.leftPane.checkIfBranchIsPartOfLeftTree(HIERARCHY, 1, Pattern.compile("\\{\"text\":\"hierarchy.*\",\"children\":\\[\\{\"text\":\"todelete\",\"children\":\\[]},\\{\"text\":\"hierarchy.*\",\"children\":\\[]}]}"));

        myStartPage.leftPane.clickOnLeftTreeItem(HIERARCHY, 1);
        ConfirmationDialogPage confirmationDialogPage = (ConfirmationDialogPage) myStartPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.DELETE);
        confirmationDialogPage.clickOnYes();

        myStartPage.leftPane.getAllLeftTreeItems().filter(text(HIERARCHY)).shouldHaveSize(0);
    }


    @AfterSuite(groups = {"mywiki"})
    public void afterMethodMyWiki() {
        beforeMethodMyWiki();
        myStartPage.leftPane.deleteAllLeftTreeItemExceptDummy();
    }

    @AfterSuite(groups = {"projectwiki"})
    public void afterMethodProjectWiki() {
        beforeMethodProjectWiki();
        projectWikiPage.leftPane.deleteAllLeftTreeItemExceptDummy();
    }
}
