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

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.PaymentEngineMonitoringProcessListSCTDistributionForm.DISTRIBUTION_GROUP;

@Wait(value = DISTRIBUTION_GROUP, waitSecondsForElement = 100)
public interface PaymentEngineMonitoringProcessListSCTDistributionForm extends WebFlow<PaymentEngineMonitoringProcessListSCTDistributionForm>, StoreAccessor {

    String INVALID_ICON = "//ancestor::div[@class='x-form-field-wrap']//following-sibling::div";
    String PAY_SCHEME = "//label[text()='Platební schéma']/following-sibling::*/div/input[@type='text']";
    String INVALID_PAY_SCHEME = PAY_SCHEME + INVALID_ICON;
    String DISTRIBUTION_GROUP = "//label[text()='Distribuční skupina']/following-sibling::*/div/input[@type='text']";
    String INVALID_DISTRIBUTION_GROUP = DISTRIBUTION_GROUP + INVALID_ICON;
    String OK_BUTTON = "//button[@type='submit']";
    String CLOSE_POP_UP = "//div[@class='x-tool x-tool-close ']";

    @TypeIntos({@TypeInto(value = DISTRIBUTION_GROUP, andWait = @Wait(value = INVALID_DISTRIBUTION_GROUP, until = Until.GONE), clear = true),
            @TypeInto(value = PAY_SCHEME, andWait = @Wait(value = INVALID_PAY_SCHEME, until = Until.GONE), clear = true)})
    @Click(OK_BUTTON)
    PaymentEngineMonitoringProcessListSCTDistributionForm clickAndRunSCTDistribution(String distributionGroup, String payScheme);

    @Wait(explicitWaitSeconds = 2)
    PaymentEngineMonitoringProcessListSCTDistributionForm wait2sec();

    @CustomAction
    default PaymentEngineMonitoringProcessList closeErrorPopUpIfVisible(String distributionGroup, String payScheme) {
        PaymentEngineEndpoint paymentEngineEndpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (paymentEngineEndpoint.isElementVisible(5, CLOSE_POP_UP, Lookup.XPATH)) {
            paymentEngineEndpoint.click(() -> CLOSE_POP_UP);
            clickAndRunSCTDistribution(distributionGroup, payScheme)
                    .wait2sec(); //process is locked for 2 seconds in PAE
        }
        return null;
    }

}