package cz.moneta.test.dsl.ufo.operations.client.clientDetail;

import cz.moneta.test.dsl.ufo.operations.shared.topmenu.XXX702_SharedTopMenu;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.junit.jupiter.api.Assertions;

import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

public interface STS701_S002_InfoServiceOrdersList extends StoreAccessor {

    String CANCEL_INFO_SERVICE_BUTTON = "STS701_S002_B081";
    String ORDER_DETAIL_ACCOUNTS_BUTTON = "STS701_S002_P012-%s";
    String ORDER_DETAIL_CARDS_BUTTON = "STS701_S002_P014-%s";
    String ORDER_DETAIL_OTHERS_BUTTON = "STS701_S002_P016-%s";
    String DELETE_FIRST_ORDER_ACCOUNTS_BUTTON = "STS701_S002_P011-1";
    String DELETE_BUTTONS_IN_ACCOUNTS = "//img[starts-with(@id, 'STS701_S002_P011-')]";
    String EXPRESS_SETTINGS_TAB_XPATH = "//button[@title='STS701_S002_I201 [STS701_S002_I201]']";

    @Click(value = EXPRESS_SETTINGS_TAB_XPATH, by = Lookup.XPATH)
    STS701_S002_AdcProductsPanel clickExpressSettings();

    @CustomAction
    default STS701_S002_AdcProductsPanel cancelInfoService() {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> CANCEL_INFO_SERVICE_BUTTON);
        endpoint.acceptAlert(5);
        endpoint.waitForElementsToLoad(5, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        endpoint.sendKeysOneAtATime(Key.PAGE_UP);
        endpoint.checkElementContent(XXX702_SharedTopMenu.STATUS_BAR, "Zrušení objednávek proběhlo v pořádku.");
        return null;
    }

    @Click(value = ORDER_DETAIL_ACCOUNTS_BUTTON, isStringDynamicXpath = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    STS701_S002_AdcProductsPanel clickOrderInAccountsDetail(String index);

    @Click(value = ORDER_DETAIL_CARDS_BUTTON, isStringDynamicXpath = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME))
    STS701_S002_AdcProductsPanel clickOrderInCardsDetail(String index);

    @Click(value = ORDER_DETAIL_OTHERS_BUTTON, isStringDynamicXpath = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    STS701_S002_AdcProductsPanel clickOrderInOthersDetail(String index);

    @Click(value = DELETE_FIRST_ORDER_ACCOUNTS_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    STS701_S002_AdcProductsPanel clickDeleteFirstOrderInAccounts();

    @CustomAction
    default STS701_S002_AdcProductsPanel deleteFirstAccountOrderAndCheckDeletion() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        int accountOrdersCount = endpoint.findAndGetListOfElements(DELETE_BUTTONS_IN_ACCOUNTS, Lookup.XPATH).size();
        clickDeleteFirstOrderInAccounts();
        Assertions.assertTrue(accountOrdersCount > endpoint.findAndGetListOfElements(DELETE_BUTTONS_IN_ACCOUNTS, Lookup.XPATH).size(), "The count of rows is wrong. Delete action does not work right");
        return null;
    }
}
