package com.intland.codebeamer.functionaltest.test;

import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.CreateProjectPage;
import com.intland.codebeamer.functionaltest.page.ProjectTemplatePage;
import com.intland.codebeamer.functionaltest.page.common.EditorComponent;
import com.intland.codebeamer.functionaltest.page.common.MarkupTab;
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.NewChildWikiDialog;
import com.intland.codebeamer.functionaltest.page.wiki.ProjectWikiPage;
import com.intland.codebeamer.functionaltest.page.wiki.UpdateWikiPageDialog;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.page;
import static com.codeborne.selenide.Selenide.switchTo;

/**
 * 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 WikiPluginTests extends BaseTest {

    private MyStartPage myStartPage;
    private static final String PLUGINTEST = "plugintest";
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    private String requirementTrackerId, testCasesTrackerId;
    private String projectName = PLUGINTEST + dateFormat.format(new Date());
    private ProjectWikiPage projectWikiPage;

    @BeforeClass
    public void beforeClass() {
        myStartPage = Helper.loginWithSystemAdmin();
        CreateProjectPage createProjectPage = open("/createProject.spr", CreateProjectPage.class);
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.DEMO_PROJECT);
        createProjectPage.selectSmartTemplate(CreateProjectPage.SmartTemplateOptions.AGILE_SCRUM);
        createProjectPage.clickOnNext();
        ProjectTemplatePage projectTemplatePage = page(ProjectTemplatePage.class);
        projectTemplatePage.fillName(projectName);
        ProjectWikiPage projectWikiPage = projectTemplatePage.clickOnFinish();
        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        requirementTrackerId = trackersPage.getTrackerId("Requirements");
        testCasesTrackerId = trackersPage.getTrackerId("Test Cases");
    }

    @BeforeMethod
    public void beforeMethod() {
        myStartPage = Helper.loginWithSystemAdmin();
        ProjectsPage projectsPage = myStartPage.clickOnProjects();
        projectsPage.clickOnCompactList();
        projectWikiPage = projectsPage.clickOnProject(projectName);
    }

    //https://codebeamer.com/cb/issue/330099
    @Test
    public void checkRiskTraceabilityMatrixPlugin() {
        NewChildWikiDialog newChildWikiDialog = projectWikiPage.centerPane.clickOnNewChildPage();
        switchTo().frame("inlinedPopupIframe");
        String pageName = PLUGINTEST + dateFormat.format(new Date());
        newChildWikiDialog.fillPageName(pageName);
        MarkupTab markupTab = (MarkupTab) newChildWikiDialog.editorComponent.clickOnTab(EditorComponent.Tab.MARKUP);
        markupTab.fillComment("[{RiskTraceabilityMatrix trackers='" + requirementTrackerId + "," + testCasesTrackerId + "'}]");
        newChildWikiDialog.clickOnAdd();
        switchTo().defaultContent();
        projectWikiPage.centerPane.waitForCenterPaneToLoad();
        projectWikiPage.centerPane.getPageErrorMessages().shouldHaveSize(0);
    }
}
