package cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_transaction;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.payment_engine.PaymentEngineEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.payment_engine.PaymentEngineForeignPaymentDetailPage.SUBMIT_BUTTON;

@Wait(SUBMIT_BUTTON)
public interface PaymentEngineDistributionToClearingArchivEdit extends WebFlow<PaymentEngineDistributionToClearingArchivEdit>, StoreAccessor {

    String AJAX_LOADING = "//div[text()='Načítám...']";
    String REQUEST_FOR_CANCELLATION_BANK = "//label[text()='Žádost o storno (banka)']/../input";
    String CODE_INPUT_DROPDOWN = "//input[@name='rReasonCode']//following-sibling::input//following-sibling::img";
    String CODE_INVALID_ICON = CODE_INPUT_DROPDOWN + "//following::div[@class='x-form-invalid-icon']";
    String CODE_ITEM = "//div[@class='x-combo-list-item'][contains(text(), '%s')]";
    String SUBMIT_BUTTON = "//button[text()='OK']";

    @Wait(value = REQUEST_FOR_CANCELLATION_BANK, until = Until.VISIBLE)
    @Click(value = REQUEST_FOR_CANCELLATION_BANK, andWait = @Wait(value = SUBMIT_BUTTON, until = Until.CLICKABLE))
    PaymentEngineDistributionToClearingArchivEdit clickOnRequestForCancellationBank();

    @Wait(value = CODE_INPUT_DROPDOWN, until = Until.VISIBLE)
    @Click(value = CODE_INPUT_DROPDOWN)
    PaymentEngineDistributionToClearingArchivEdit clickOnCode();

    @Wait(value = CODE_ITEM, isStringDynamicXpath = true, until = Until.VISIBLE)
    @Click(value = CODE_ITEM, isStringDynamicXpath = true, andWait = @Wait(value = CODE_INVALID_ICON, until = Until.GONE))
    PaymentEngineDistributionToClearingArchivEdit chooseCode(String code);

    @Wait(value = SUBMIT_BUTTON, until = Until.CLICKABLE)
    @Click(value = SUBMIT_BUTTON, jsClick = true)
    PaymentEngineDistributionToClearingArchivEdit clickSubmit();

    @Wait(value = AJAX_LOADING, until = Until.GONE)
    PaymentEngineDistributionToClearingArchiv waitForAjax();

    @CustomAction
    default PaymentEngineDistributionToClearingArchiv clickSubmitIfStillVisible() {
        PaymentEngineEndpoint paymentEngineEndpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (!paymentEngineEndpoint.isElementVisible(5, PaymentEngineDistributionToClearingArchiv.SEARCH_ID_BUTTON, Lookup.XPATH)) {
            clickSubmit().waitForAjax();
        }
        return null;
    }
}