package cz.moneta.test.dsl.ufo.operations.recorder.panels;

import cz.moneta.test.dsl.ufo.operations.recorder.CRC710_S001_RecorderMainPage;
import cz.moneta.test.dsl.ufo.operations.recorder.RecorderNameOfGroup;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.operations.recorder.CRC710_S001_RecorderMainPage.GENERAL_TREE_ITEM_XPATH;
import static cz.moneta.test.dsl.ufo.operations.recorder.CRC710_S001_RecorderMainPage.LOAD_IMG_XPATH;
import static cz.moneta.test.dsl.ufo.operations.recorder.panels.CRC710_S001_RequirementsRecorderPanel.REQUESTS_PANEL_XPATH;
import static cz.moneta.test.harness.support.web.Lookup.XPATH;
import static cz.moneta.test.harness.support.web.Until.GONE;

@Wait(value = REQUESTS_PANEL_XPATH, by = Lookup.XPATH)
public interface CRC710_S001_RequirementsRecorderPanel extends StoreAccessor {

    String REQUESTS_PANEL_XPATH = "//fieldset[@id='CRC710_S001_Y002']";

    String GENERAL_TREE_ICON_XPATH = GENERAL_TREE_ITEM_XPATH + "//ancestor::div[contains(@style, 'treeClosedLinedSimple.gif')]";

    @Wait(value = GENERAL_TREE_ITEM_XPATH, by = XPATH)
    @Click(value = GENERAL_TREE_ICON_XPATH, by = XPATH, jsClick = true, andWait = @Wait(value = LOAD_IMG_XPATH, until = GONE, by = XPATH))
    CRC710_S001_RecorderMainPage openGroupInTreeMenu(RecorderNameOfGroup nameOfGroup);

    @CustomAction
    default CRC710_S001_RecorderMainPage openItemInTreeMenu(String itemName) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String treeItemXpath = String.format(GENERAL_TREE_ITEM_XPATH, itemName);
        endpoint.jsClick(() -> REQUESTS_PANEL_XPATH + treeItemXpath, XPATH);
        endpoint.jsDoubleClick(() -> REQUESTS_PANEL_XPATH + treeItemXpath, XPATH);
        return null;
    }

    @CheckElementPresent(value = REQUESTS_PANEL_XPATH + GENERAL_TREE_ITEM_XPATH, by = XPATH, isStringDynamicXpath = true)
    CRC710_S001_RecorderMainPage checkRequirementIsVisible(String requirement);

    @CustomAction
    default CRC710_S001_RecorderMainPage checkRequirementsAreVisible(RecorderNameOfGroup... requirements) {
        for (RecorderNameOfGroup requirement : requirements) {
            checkRequirementIsVisible(requirement.getValue());
        }
        return null;
    }
}