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

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_monitoring.PaymentEngineMonitoringProcessList.PNCCO_DISTRIBUTION_CZCT;
import static cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_monitoring.PaymentEngineMonitoringProcessList.PNCCO_DISTRIBUTION_XCT;

@Wait(value = {PNCCO_DISTRIBUTION_CZCT, PNCCO_DISTRIBUTION_XCT}, until = Until.VISIBLE)
public interface PaymentEngineMonitoringProcessList extends WebFlow<PaymentEngineMonitoringProcessList>, StoreAccessor {
    String INVALID_ICON = "//ancestor::div[@class='x-form-field-wrap']//following-sibling::div";
    String PNCCO_DISTRIBUTION_XCT = "//div[text()='PNCCO_DISTRIBUTION_XCT']";
    String PNCCO_DISTRIBUTION_CZCT = "//div[text()='PNCCO_DISTRIBUTION_CZCT']";
    String PNCCO_DISTRIBUTION_SCT = "//div[text()='PNCCO_DISTRIBUTION_SCT']";
    String PNCCI_DISTRIBUTION_SB = "//div[text()='PNCCI_DISTRIBUTION_SB']";
    String RELEASE_AFTER_DISPO_CHECK = "//div[text()='ATC_RELEASE']";
    String INIT_BANK_CODE = "//label[text() = 'Kód banky iniciátora']/following-sibling::*/div/input[@type = 'text']";
    String INVALID_INIT_BANK_CODE = INIT_BANK_CODE + INVALID_ICON;
    String PAY_SCHEME = "//label[text() = 'Platební schéma']/following-sibling::*/div/input[@type = 'text']";
    String INVALID_PAY_SCHEME = PAY_SCHEME + INVALID_ICON;
    String NEXT_DAY_DIST = "//label[text() = 'Distr. na další den']/following-sibling::*/div/input[@type = 'text']";
    String INVALID_NEXT_DAY_DIST = NEXT_DAY_DIST + INVALID_ICON;
    String RUN_BUTTON = "//a[text() = 'Spustit']";
    String OK_BUTTON = "//button[@type = 'submit']";
    String CLOSE_POP_UP = "//div[@class='x-tool x-tool-close ']";
    String CLOSE_TAB = "//a[@class = 'x-tab-strip-close']";
    String GENERATE_NOTIFICATION = "//div[label[text()='Generování notifikace']]//following-sibling::img";
    String GENERATE_NOTIFICATION_NO = "(//div[text() = 'Ne'])[2]";

    @Click(PNCCO_DISTRIBUTION_XCT)
    PaymentEngineMonitoringProcessList enterXCTDistribution();

    @Click(value = RUN_BUTTON, andWait = @Wait(OK_BUTTON))
    PaymentEngineMonitoringProcessList clickOnRunButton();

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

    @Click(PNCCO_DISTRIBUTION_CZCT)
    PaymentEngineMonitoringProcessList enterCZCTDistributionForm();

    @Click(PNCCO_DISTRIBUTION_SCT)
    PaymentEngineMonitoringProcessList clickOnSCTDistributionForm();

    @Click(value = RUN_BUTTON, andWait = @Wait(value = RUN_BUTTON, until = Until.GONE))
    PaymentEngineMonitoringProcessListSCTDistributionForm enterSCTDistributionForm();

    @Click(PNCCI_DISTRIBUTION_SB)
    PaymentEngineMonitoringProcessList enterStarBuildDistributionForm();

    @Wait(value = OK_BUTTON, until = Until.CLICKABLE)
    @Click(value = GENERATE_NOTIFICATION, andWait = @Wait(GENERATE_NOTIFICATION_NO))
    PaymentEngineMonitoringProcessList selectGenerateNotification();

    @Click(GENERATE_NOTIFICATION_NO)
    PaymentEngineMonitoringProcessList selectGenerateNotificationNo();

    @Click(RELEASE_AFTER_DISPO_CHECK)
    PaymentEngineMonitoringProcessList clickOnReleaseAfterDispoCheckForm();

    @Click(value = RUN_BUTTON, andWait = @Wait(OK_BUTTON))
    PaymentEngineMonitoringProcessReleaseAfterDispo enterReleaseAfterDispoCheckForm();

    @TypeIntos({@TypeInto(value = INIT_BANK_CODE, andWait = @Wait(value = INVALID_INIT_BANK_CODE, until = Until.GONE), clear = true),
            @TypeInto(value = PAY_SCHEME, andWait = @Wait(value = INVALID_PAY_SCHEME, until = Until.GONE), clear = true),
            @TypeInto(value = NEXT_DAY_DIST, andWait = @Wait(value = INVALID_NEXT_DAY_DIST, until = Until.GONE), clear = true)})
    @Click(OK_BUTTON)
    PaymentEngineMonitoringProcessList clickAndRunCZCTDistribution(String initBankCode, String payScheme, String nextDayDist);

    @Wait(value = OK_BUTTON, until = Until.GONE)
    PaymentEngineMonitoringProcessList waitForOkGone();

    @CustomAction
    default PaymentEngineMonitoringProcessList closeErrorPopUpIfVisibleCZCT(String initBankCode, String payScheme, String nextDayDist) {
        PaymentEngineEndpoint paymentEngineEndpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (paymentEngineEndpoint.isElementVisible(5, CLOSE_POP_UP, Lookup.XPATH)) {
            paymentEngineEndpoint.click(() -> CLOSE_POP_UP);
            clickAndRunCZCTDistribution(initBankCode, payScheme, nextDayDist).waitForOkGone();
        }
        return null;
    }

    @CustomAction
    default PaymentEngineMonitoringProcessList closeErrorPopUpIfVisibleXCT() {
        PaymentEngineEndpoint paymentEngineEndpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (paymentEngineEndpoint.isElementVisible(5, CLOSE_POP_UP, Lookup.XPATH)) {
            paymentEngineEndpoint.click(() -> CLOSE_POP_UP);
            enterXCTDistribution().clickOnRunButton().clickOnOk();
        }
        return null;
    }

    @Click(CLOSE_TAB)
    PaymentEngineHomePage closeTab();
}
