package cz.moneta.test.dsl.ib.settings.ib.ediponents;

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.dsl.ib.components.MainMenu;
import cz.moneta.test.dsl.ib.settings.ib.SettingsVerticalMenu;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.Wait;
import org.junit.Assert;
import org.openqa.selenium.WebElement;

import java.util.List;

import static cz.moneta.test.dsl.ib.settings.ib.ediponents.DetailDisponentPage.DETAIL_DISPONENTS_TITLE;

@Wait(DETAIL_DISPONENTS_TITLE)
public interface DetailDisponentPage extends IbPageFlow<DetailDisponentPage>, MainMenu, SettingsVerticalMenu, StoreAccessor {
    String DETAIL_DISPONENTS_TITLE = "//img[@alt='Detail disponenta']";
    String RIGHTS_FOR_ACCOUNTS = "//td[text()='Práva pro účty']//following::label[contains(text(),'%s')]/input[@type='checkbox']";
    String CHECKBOX_PAGE = "//td[text()='Práva pro účty']//following::input";
    String RIGHTS_ACCOUNTS = "//td[text()='Práva pro účty']//following::input[@checked='true']/parent::td";

    @CustomAction
    default DetailDisponentPage checkRightsForAccountAndSmartBank(String account) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        List<WebElement> listElements = ibEndpoint.findAndGetListOfElements(RIGHTS_ACCOUNTS, Lookup.XPATH);
        boolean isAccountMatched = listElements.stream()
                .anyMatch(s -> s.getText().matches("(.*)" + account + "(.*)"));
        Assert.assertTrue("Disponent not contains expected product or contains another products: Numbers of product " + listElements.size() + " and expected product " + account + " is " + isAccountMatched, listElements.size() == 2 && isAccountMatched);
        return null;
    }
}
