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.*;

import static cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_transaction.PaymentEngineIngoingArchiv.ID_INPUT;

@Wait(ID_INPUT)
public interface PaymentEngineIngoingArchiv extends WebFlow<PaymentEngineIngoingArchiv>, StoreAccessor {

    String ID_INPUT = "//input[@name='field1#id']";
    String SEARCH_BTN = "//button[@class='x-btn-text'][text()='Hledat']";
    String AJAX_LOADING = "//div[text()='Načítám...']";
    String MANUAL_ACTIONS = "//a[text()='Manuální akce']";
    String RETURN = "//input[following-sibling::label[text()='Return (banka)']]";
    String REASON_CODE = "//input[@name='rReasonCode']/following-sibling::img";
    String REASON_CODE_CUST = "//div[@class='x-combo-list-inner']/div[text()='CUST - Requested By Customer']";
    String OK_BUTTON = "//button[text()='OK']";
    String INVALID_ICON = "//div[@class='x-form-invalid-icon']";
    String CLOSE_TAB = "//a[@class='x-tab-strip-close']";

    @TypeInto(ID_INPUT)
    @Click(value = SEARCH_BTN, andWait = @Wait(value = AJAX_LOADING, until = Until.GONE))
    @Wait(value = MANUAL_ACTIONS, until = Until.CLICKABLE)
    PaymentEngineIngoingArchiv searchTransaction(String transactionId);

    @Click(MANUAL_ACTIONS)
    PaymentEngineIngoingArchiv clickOnManualActions();

    @Wait(value = OK_BUTTON, until = Until.VISIBLE)
    PaymentEngineIngoingArchiv waitForOkButton();

    @CustomAction
    default PaymentEngineIngoingArchiv clickManualActionsIfStillVisible() {
        PaymentEngineEndpoint paymentEngineEndpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (paymentEngineEndpoint.isElementVisible(3, MANUAL_ACTIONS, Lookup.XPATH)) {
            clickOnManualActions().waitForOkButton();
        }
        return null;
    }
    @Click(value = RETURN, andWait = @Wait(value = OK_BUTTON, until = Until.CLICKABLE))
    PaymentEngineIngoingArchiv clickOnReturn();

    @Click(value = REASON_CODE, andWait = @Wait(value = REASON_CODE_CUST, until = Until.VISIBLE))
    PaymentEngineIngoingArchiv clickOnReasonCode();

    @Click(value = REASON_CODE_CUST, andWait = @Wait(value = INVALID_ICON, until = Until.GONE))
    PaymentEngineIngoingArchiv chooseReasonCodeCust();

    @Click(value = OK_BUTTON, andWait = @Wait(value = OK_BUTTON, until = Until.GONE))
    PaymentEngineIngoingArchiv clickOk();

    @Click(CLOSE_TAB)
    PaymentEngineHomePage closeTab();

}
