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

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;
import com.intland.codebeamer.functionaltest.page.common.EditorComponent;
import com.intland.codebeamer.functionaltest.page.common.IsPageLoaded;
import com.intland.codebeamer.functionaltest.page.common.MoreMenuHelper;

import com.intland.codebeamer.functionaltest.page.trackers.AddTagDialog;
import org.openqa.selenium.By;

import org.openqa.selenium.support.ui.ExpectedConditions;

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 firstSixProductLicense agreement
 * you entered into with Intland.
 */
public class CenterPane {

    private LeftPane leftPane = new LeftPane();

    public IsPageLoaded clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem moreMenuItem) {
        return MoreMenuHelper.clickOnMoreMenuItem(moreMenuItem);
    }

    public NewChildWikiDialog clickOnNewChildPage() {
        $(By.xpath("//a[@title='Add New Child Page']")).click();
        return page(NewChildWikiDialog.class);
    }


    public NewChildWikiDialog clickOnNewChildDashboard() {
        $(By.xpath("//a[text()='New Child Dashboard']")).click();
        return page(NewChildWikiDialog.class);
    }

    public EditorComponent editInlineByPencil(String itemLabel, int index, String headerText) {
        leftPane.clickOnLeftTreeItem(itemLabel, index);
        waitForCenterPaneToLoad();
        $(By.xpath("//div[@class='sectionEditable']//*[text()='" + headerText + "']")).hover();
        $(By.xpath("//img[@class='inplaceEditableIcon']")).click();
        return new EditorComponent();
    }

    public EditorComponent editInlineByDoubleClick(String itemLabel, int index, String headerText) {
        leftPane.clickOnLeftTreeItem(itemLabel, index);
        waitForCenterPaneToLoad();
        $(By.xpath("//div[@class='sectionEditable']//*[text()='" + headerText + "']")).doubleClick();
        return new EditorComponent();
    }

    public void waitForCenterPaneToLoad() {
        Wait().withTimeout(1, TimeUnit.MINUTES)
                .pollingEvery(1, TimeUnit.SECONDS)
                .until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//img[contains(@src, 'ajax-loader.gif')]")));
        //$(By.xpath("//img[contains(@src, 'ajax-loader.gif')]")).should(disappear);
    }

    public ElementsCollection errorMessages() {
        return $$("#globalMessages li");
    }

    public UpdateWikiPageDialog clickOnEdit() {
        $(By.linkText("Edit")).click();
        return page(UpdateWikiPageDialog.class);
    }

    public AddTagDialog clickOnTags() {
        $(By.xpath(".//a[text()='Tags:']")).click();
        return page(AddTagDialog.class);
    }

    public void checkIfEditViaOffice(Condition condition) {
        $(By.linkText("Edit via Office")).shouldBe(condition);
    }

    public void clickOnWikiPage(String linkText) {
        $(By.linkText(linkText)).click();
    }

    public ElementsCollection getPageErrorMessages() {
        return $$(By.xpath("//span[contains(@class,'error')]"));
    }

    public ElementsCollection getOverlayErrorMessages() {
        return $$(By.xpath("//div[@id='overlayMessageBoxContainer']//div[contains(@class,'error')]"));
    }

    public ElementsCollection getGlobalErrorMessages() {
        return $$(By.xpath("//div[@id='globalMessages']/div[contains(@class,'error')]//li"));
    }

}
