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

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

public interface PaymentEngineIngoingForVerification extends WebFlow<PaymentEngineIngoingForVerification>, StoreAccessor {

    String ID = "//input[@name='field1#id']";
    String SEARCH_FIELD = "//button[@class='x-btn-text  grid-filter-collapse']";
    String SEARCH_BTN = "//button[@class='x-btn-text'][text() = 'Hledat']";
    String VERIFY_REJECT_TRANSACTION_BUTTON = "//a[text()='Schválit/Zamítnout']";
    String ACCEPT_CHANGES_BUTTON = "//input[@name='manActGroup']";
    String OK_BUTTON = "//button[text()='OK']";
    String CLOSE_TAB = "//a[@class='x-tab-strip-close']";
    String AJAX_LOADING = "//div[text()='Načítám...']";

    @Wait(value = AJAX_LOADING, until = Until.GONE)
    @Click(value = SEARCH_FIELD, andWait = @Wait(value = ID, until = Until.VISIBLE))
    PaymentEngineIngoingForVerification clickOnSearchField();

    @TypeInto(ID)
    @Click(value = SEARCH_BTN, andWait = @Wait(value = AJAX_LOADING, until = Until.GONE))
    PaymentEngineIngoingForVerification searchTransaction(String transactionId);

    @Click(VERIFY_REJECT_TRANSACTION_BUTTON)
    PaymentEngineIngoingForVerification clickOnVerifyOrReject();

    @Wait(value = ACCEPT_CHANGES_BUTTON, until = Until.VISIBLE)
    PaymentEngineIngoingForVerification waitForAcceptChangesButton();

    @CustomAction
    default PaymentEngineIngoingForVerification clickOnVerifyOrRejectStillVisible() {
        PaymentEngineEndpoint paymentEngineEndpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (paymentEngineEndpoint.isElementVisible(3, VERIFY_REJECT_TRANSACTION_BUTTON, Lookup.XPATH)) {
            clickOnVerifyOrReject().waitForAcceptChangesButton();
        }
        return null;
    }

    @Click(value = ACCEPT_CHANGES_BUTTON, andWait = @Wait(value = OK_BUTTON, until = Until.CLICKABLE))
    PaymentEngineIngoingForVerification clickOnAccept();

    @Click(value = OK_BUTTON, andWait = @Wait(value = OK_BUTTON, until = Until.GONE))
    PaymentEngineIngoingForVerification clickOnOk();

    @Click(CLOSE_TAB)
    PaymentEngineHomePage closeTab();
}
