package com.intland.codebeamer.functionaltest.page.trackers.documentview;

import com.codeborne.selenide.*;
import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.trackers.*;
import org.openqa.selenium.By;
import org.openqa.selenium.interactions.Actions;

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

/**
 * 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 DocumentViewPage extends TrackerItemListPage {

    public RightPane rightPane = new RightPane();
    public CenterPane centerPane = new CenterPane();
    public LeftPane leftPane = new LeftPane();

    //region Elements
    public SelenideElement rightSlider() {
        return $(".ui-layout-resizer-east");
    }

    public SelenideElement leftSlider() {
        return $(".ui-layout-resizer-west");
    }

    public enum DragAndDropAssociationType {
        LINK_TO("Link to this"),
        DEPENDS_ON("Depends on"),
        RELATED_TO("Related to");

        private final String text;

        DragAndDropAssociationType(String s) {
            text = s;
        }

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

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

    //region Actions
    public void clickOnLeftCloserTogglingIcon() {
        $("#closer-west").click();
        sleep(1000);
    }

    public void clickOnLeftOpenerTogglingIcon() {
        $("#opener-west").click();
        sleep(1000);
    }

    public void clickOnRightCloserTogglingIcon() {
        $("#closer-east").click();
        sleep(1000);
    }

    public void clickOnRightOpenerTogglingIcon() {
        $("#opener-east").click();
        sleep(1000);
    }

    public void adjustRightPaneWidthBy(int pixel) {
        actions().clickAndHold(rightSlider()).moveByOffset(pixel, 0).release().perform();
    }

    public void adjustLeftPaneWidthBy(int pixel) {
        actions().clickAndHold(leftSlider()).moveByOffset(pixel, 0).release().perform();
    }


    public void addAssociationOrReferenceViaDragAndDropBetweenLeftTreeAndCenterPane(String leftTreeItemLabel, int leftTreeIndex, String centerPaneItemLabel, int centerPaneIndex, DragAndDropAssociationType assocType) {
        Actions builder = new Actions(getWebDriver());
        SelenideElement targetItem = centerPane.centerPaneItem(centerPaneItemLabel, centerPaneIndex);
        builder.clickAndHold(leftPane.leftTreeItem(leftTreeItemLabel, leftTreeIndex)).moveToElement(targetItem)
                .moveToElement(targetItem.find(By.xpath(".//span[contains(., '" + assocType.text + "')]"))).release().build().perform();
    }

    //TODO fix it coz clickandhold seems not work
    public void addAssociationOrReferenceViaDragAndDropCenterPane(String sourceCenterPaneItemLabel, int sourceItemIndex, String targetCenterPaneItemLabel, int targetItemIndex, DragAndDropAssociationType assocType) {
        SelenideElement targetItem = centerPane.centerPaneItem(targetCenterPaneItemLabel, targetItemIndex);
        SelenideElement sourceItem = centerPane.centerPaneItem(sourceCenterPaneItemLabel, sourceItemIndex);
        SelenideElement dragHandler = sourceItem.find(".dnd-icon").hover();
        Selenide.actions().clickAndHold(dragHandler).moveToElement(targetItem).moveToElement(targetItem.find(By.xpath(".//span[contains(., '" + assocType.text + "')]"))).release().perform();
        sleep(10000);
    }
    //endregion


    //region Utils
    @Override
    public boolean isPageLoaded() throws UnsupportedOperationException {
        return Helper.doesPageSourceContain("documentView.less");
    }

    public int getRightPaneWidth() {
        return Integer.parseInt(rightPane.rightPane().getCssValue("width").split("px")[0]);
    }

    public SelenideElement getWarning() {
        return $(By.xpath(".//div[@class='warning']"));
    }

    public void waitForLoaderToDisappear() {
        $(By.xpath(".//span[.='ajax.loading']")).should(Condition.disappear);
    }

    @Override
    public void waitForPageToLoad() {
        super.waitForPageToLoad();
        leftPane.waitForLeftTreeToLoad();
        centerPane.waitForCenterPaneToLoad();
    }

    //endregion
}
