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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.home.PaymentEngineHomePage;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
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.PaymentEngineRunInstance.UPLOAD_FILE_BUTTON;

@Wait(value = UPLOAD_FILE_BUTTON, until = Until.VISIBLE)
public interface PaymentEngineRunInstance extends WebFlow<PaymentEngineRunInstance>, StoreAccessor {
    String LAST_SEQUENCE_NUMBER_KEY = "LAST_SEQUENCE_NUMBER_KEY";
    String LAST_SEQUENCE_NUMBER_XPATH = "//input[@name = 'LAST_FILE_NUMBER']";
    String LAST_NONPRIORITY_SEQUENCE_NUMBER_KEY = "LAST_NONPRIORITY_SEQUENCE_NUMBER_KEY";
    String LAST_NONPRIORITY_SEQUENCE_NUMBER_XPATH = "//input[@name = 'CZ_LAST_FILE_NUMBER_L']";
    String UPLOAD_FILE_BUTTON = "//a[text() = 'Vložit soubory']";
    String FILE_INPUT_XPATH = "//input[@type='file']";
    String SEND_BUTTON_POPUP = "//button[text() = 'Send']";
    String OK_BUTTON_POPUP = "//td[@class = 'x-panel-btn-td']//table[@class = 'x-btn-wrap x-btn ']//button[text() = 'OK']";
    String OK_BUTTON = "//div[@class = 'x-plain-body x-plain-body-noheader x-plain-body-noborder']//button[text() = 'OK']";
    String CANCEL_BUTTON = "//div[@class = 'x-plain-body x-plain-body-noheader x-plain-body-noborder']//button[text() = 'Storno']";
    String TAB = "//em//span[text() = '%s']";
    String REFRESH_BUTTON = "//div[@class = 'x-panel x-panel-noborder x-grid-panel ']//div[@class = 'x-panel-tbar x-panel-tbar-noborder']//button";
    String CLOSE_TAB = "//a[@class='x-tab-strip-close']";
    String FILE_NAME_XPATH = "//div[contains(text(),'.out')]";
    String CHECKER_FOR_QUE = "(//div[@class='x-grid3-hd-checker'])[2]";

    @CustomAction
    default PaymentEngineRunInstance storeLastNumberInSequence(Harness harness) {
        PaymentEngineUtils.storeElementAttribute(harness, LAST_SEQUENCE_NUMBER_XPATH, LAST_SEQUENCE_NUMBER_KEY);
        return null;
    }

    @CustomAction
    default PaymentEngineRunInstance storeLastNonpriorityNumberInSequence(Harness harness) {
        PaymentEngineUtils.storeElementAttribute(harness, LAST_NONPRIORITY_SEQUENCE_NUMBER_XPATH, LAST_NONPRIORITY_SEQUENCE_NUMBER_KEY);
        return null;
    }

    @Click(UPLOAD_FILE_BUTTON)
    PaymentEngineRunInstance clickOnUploadFileButton();

    @FileUpload(value = FILE_INPUT_XPATH)
    PaymentEngineRunInstance uploadFile(String filepath);

    @Click(value = SEND_BUTTON_POPUP, andWait = @Wait(value = OK_BUTTON_POPUP, until = Until.CLICKABLE))
    PaymentEngineRunInstance clickSendPopupButton();

    @Click(value = OK_BUTTON_POPUP, andWait = @Wait(value = OK_BUTTON_POPUP, until = Until.GONE))
    PaymentEngineRunInstance clickOkPopupButton();

    @Click(value = OK_BUTTON, andWait = @Wait(value = OK_BUTTON, until = Until.GONE))
    PaymentEngineMonitoringChannelList clickOkButton();

    @Click(value = CANCEL_BUTTON, andWait = @Wait(value = CANCEL_BUTTON, until = Until.GONE))
    PaymentEngineMonitoringChannelList clickCancelButton();

    @Click(value = TAB, isStringDynamicXpath = true, by = Lookup.XPATH)
    PaymentEngineRunInstance clickOnFileTab(String tabName);

    @Click(CHECKER_FOR_QUE)
    PaymentEngineRunInstance chooseAll();

    @CustomAction()
    default PaymentEngineRunInstance waitForFileAndClickOnIt(String fileName) {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        String ROW_FILE = String.format("//div[text() = '%s']", fileName);
        endpoint.waitForElementAndRefresh(ROW_FILE, REFRESH_BUTTON, 30, 3);
        endpoint.click(() -> ROW_FILE);
        return null;
    }

    @CustomAction
    default PaymentEngineMonitoringChannelList clearQueForMTIfExists() {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (endpoint.isElementPresent(FILE_NAME_XPATH, Lookup.XPATH)) {
            chooseAll()
                    .clickOkButton()
                    .wait4sec(); //process is locked for 4 seconds in PAE
        } else {
            clickCancelButton();
        }
        return null;
    }

    @Click(CLOSE_TAB)
    PaymentEngineHomePage closeTab();
}
