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

import com.intland.codebeamer.functionaltest.page.BasePage;
import com.intland.codebeamer.functionaltest.page.common.EditorComponent;
import com.intland.codebeamer.functionaltest.page.common.WYSIWYGTab;
import com.intland.codebeamer.functionaltest.page.common.WikiTextDialog;
import org.openqa.selenium.By;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;

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

public class CreateEditTrackerItemPage extends BasePage {

    public void setSummary(String summary) {
        $(By.id("summary")).setValue(summary);
    }

    public void setDescriptionInMarkupTab(String description) {
        $(By.id("wiki-markup-tab")).click();
        $(By.id("editor")).waitUntil(Condition.visible, 10000).setValue(description);
    }

    public void setChoiceFieldValue(String fieldName, int indexOfOption) {
        ElementsCollection tds = $(By.className("fieldsTable")).$$(By.tagName("td"));
        for (int i = 0; i < tds.size(); i++) {
            if (tds.get(i).getText().contains(fieldName + ":")) {
                tds.get(i + 1).$(By.tagName("select")).selectOption(indexOfOption);
                break;
            }
        }
    }

    public void setIntegerFieldValue(String fieldName, int value) {
        ElementsCollection tds = $(By.className("fieldsTable")).$$(By.tagName("td"));
        for (int i = 0; i < tds.size(); i++) {
            if (tds.get(i).getText().contains(fieldName + ":")) {
                tds.get(i + 1).$(By.tagName("input")).setValue(String.valueOf(value));
                break;
            }
        }
    }

    public void setTextFieldValue(String fieldName, String value) {
        ElementsCollection tds = $(By.className("fieldsTable")).$$(By.tagName("td"));
        for (int i = 0; i < tds.size(); i++) {
            if (tds.get(i).getText().contains(fieldName + ":")) {
                tds.get(i + 1).$(By.tagName("input")).setValue(value);
                break;
            }
        }
    }

    public void setWikiFieldValue(String fieldName, String value) {
        ElementsCollection tds = $(By.className("fieldsTable")).$$(By.tagName("td"));
        for (int i = 0; i < tds.size(); i++) {
            if (tds.get(i).getText().contains(fieldName + ":")) {
                $(By.xpath(".//a[@data-layout-mode='editable']")).click();
                tds.get(i + 1).parent().$(By.xpath(".//td[2]")).click();
                switchTo().frame("inlinedPopupIframe");
                WikiTextDialog wikiTextDialog = page(WikiTextDialog.class);
                WYSIWYGTab wysiwygTab = (WYSIWYGTab) wikiTextDialog.editorComponent.clickOnTab(EditorComponent.Tab.WYSIWYG);
                wysiwygTab.fillDescription(true, value);
                wikiTextDialog.clickOnSave();
                switchTo().defaultContent();
                break;
            }
        }
    }

    public void setMemberFieldValue(String fieldName, String[] valueArr) {
        ElementsCollection tds = $(By.className("fieldsTable")).$$(By.tagName("td"));
        for (int i = 0; i < tds.size(); i++) {
            if (tds.get(i).getText().contains(fieldName + ":")) {
                for (String memberName : valueArr) {
                    boolean success = false;
                    for (int j = 0; j < 5; j++) {
                        if (!success) {
                            try {
                                tds.get(i + 1).$(By.className("token-input-list-facebook")).$(By.tagName("input")).clear();
                                tds.get(i + 1).$(By.className("token-input-list-facebook")).$(By.tagName("input")).click();
                                tds.get(i + 1).$(By.className("token-input-list-facebook")).$(By.tagName("input")).setValue(memberName);
                                //TODO: figure out what to sync on before dropdown displays
                                sleep(2000);
                                ElementsCollection collection = $$(By.className("token-input-dropdown-facebook")).filter(Condition.visible);
                                if (collection.get(0).$$(By.xpath(".//td[contains(.,'"+memberName+"')]")).size() > 0) {
                                    collection.get(0).$(By.xpath(".//td[contains(.,'"+memberName+"')]")).click();
                                    success = true;
                                }
                            } catch (Throwable t) {
                            }
                        }
                    }
                }
                break;
            }
        }
    }

    public TrackerItemDetailsPage clickOnSave() {
        $(By.className("actionBar")).$(By.name("SUBMIT")).click();
        $(By.linkText("Edit")).shouldBe(Condition.visible);
        return page(TrackerItemDetailsPage.class);
    }

//	public void clickOnClose(){
//		$(".container-close").click();
//	}

    public void clickOnCancel() {
        $(".cancelButton").click();
    }

    public void checkIfSave(Condition condition) {
        $(By.xpath(".//input[@name='SUBMIT']")).shouldBe(condition);
    }
}
