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.PaymentEngineMonitoringChannelInstances.SELECT_STATUS;

@Wait(SELECT_STATUS)
public interface PaymentEngineMonitoringChannelInstances extends WebFlow<PaymentEngineMonitoringChannelInstances>, StoreAccessor {

    String SUBMIT_BUTTON = "//button[text() = 'OK']";
    String SELECT_STATUS = "//div[label[text()='Status:']]//following-sibling::img";
    String STATUS_SUCCESS = "//div[text() = 'Úspěch']";
    String STATUS_FOR_VERIFICATION = "//div[@class='x-combo-list-inner']//div[text() = 'Ke schválení']";
    String SELECT_CHANNEL = "//div[label[text()='Kanál:']]//following-sibling::img";
    String CAP_PACS009_CHANNEL = "//div[@class='x-combo-list-inner']/div[text() = 'I_EBI_CAP_PACS009']";
    String CAP_PAIN001_CHANNEL = "//div[@class='x-combo-list-inner']/div[text() = 'I_EBI_CAP_PAIN001']";
    String MC_CFA_CHANNEL = "//div[@class='x-combo-list-inner']/div[text() = 'I_EBI_MC_CFA']";
    String MC_CFD_CHANNEL = "//div[@class='x-combo-list-inner']/div[text() = 'I_EBI_MC_CFD']";
    String MC_PAIN_CHANNEL = "//div[@class='x-combo-list-inner']/div[text() = 'I_EBI_MC_PAIN']";
    String MT_FROM_SAA_CHANNEL = "//div[@class='x-combo-list-inner']/div[text() = 'I_CCI_SAA_MT']";
    String CCI_FROM_CERTIS_CHANNEL = "//div[@class='x-combo-list-inner']/div[text() = 'I_CCI_CERTIS']";
    String TRX_FROM_STARBUILD_CHANNEL = "//div[@class='x-combo-list-inner']/div[text() = 'I_EBI_SB_PREBK']";
    String APPLY_BUTTON = "//button[text() = 'Aplikovat']";
    String REFRESH_BUTTON = "//div[@class= 'x-panel-tbar x-panel-tbar-noheader x-panel-tbar-noborder']//button[contains(@class,'loading')]";
    String DETAIL_BUTTON = "//a[text() = 'Detail']";
    String VERIFY_BUTTON = "//a[text() = 'Schválit']";
    String RECORDS_BUTTON = "//a[text() = 'Záznamy']";
    String CHECK_SELECTED_DATA_PATH = "//*[contains(@class,'x-grid3-body')]//*[contains(text(),'%s']";
    String AJAX_LOADING = "//div[text()='Načítám...']";

    @Wait(value = AJAX_LOADING, until = Until.GONE)
    @Click(SELECT_STATUS)
    PaymentEngineMonitoringChannelInstances clickOnSelectStatus();

    @Wait(STATUS_SUCCESS)
    @Click(STATUS_SUCCESS)
    PaymentEngineMonitoringChannelInstances selectStatusSuccess();

    @Wait(STATUS_FOR_VERIFICATION)
    @Click(STATUS_FOR_VERIFICATION)
    PaymentEngineMonitoringChannelInstances selectStatusForVerification();

    @Click(SELECT_CHANNEL)
    PaymentEngineMonitoringChannelInstances clickOnSelectChannel();

    @Wait(CAP_PACS009_CHANNEL)
    @Click(value = CAP_PACS009_CHANNEL, andWait = @Wait(value = CAP_PACS009_CHANNEL, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances selectCapPacs009Channel();

    @Wait(CAP_PAIN001_CHANNEL)
    @Click(value = CAP_PAIN001_CHANNEL, andWait = @Wait(value = CAP_PAIN001_CHANNEL, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances selectCapPain001Channel();

    @Wait(MC_CFA_CHANNEL)
    @Click(value = MC_CFA_CHANNEL, andWait = @Wait(value = MC_CFA_CHANNEL, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances selectMcCfaChannel();

    @Wait(MC_CFD_CHANNEL)
    @Click(value = MC_CFD_CHANNEL, andWait = @Wait(value = MC_CFD_CHANNEL, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances selectMcCfdChannel();

    @Wait(MC_PAIN_CHANNEL)
    @Click(value = MC_PAIN_CHANNEL, andWait = @Wait(value = MC_PAIN_CHANNEL, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances selectMcPainChannel();

    @Wait(MT_FROM_SAA_CHANNEL)
    @Click(value = MT_FROM_SAA_CHANNEL, andWait = @Wait(value = MT_FROM_SAA_CHANNEL, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances selectMtFromSaaChannel();

    @Wait(CCI_FROM_CERTIS_CHANNEL)
    @Click(value = CCI_FROM_CERTIS_CHANNEL, andWait = @Wait(value = CCI_FROM_CERTIS_CHANNEL, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances selectCciFromCertisChannel();

    @Wait(TRX_FROM_STARBUILD_CHANNEL)
    @Click(value = TRX_FROM_STARBUILD_CHANNEL, andWait = @Wait(value = TRX_FROM_STARBUILD_CHANNEL, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances selectTrxFromStarBuildChannel();

    @Click(value = APPLY_BUTTON, andWait = @Wait(value = AJAX_LOADING, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances clickOnApplyButton();

    @CheckElementPresent(value = CHECK_SELECTED_DATA_PATH, isStringDynamicXpath = true)
    PaymentEngineMonitoringChannelInstances checkValuePresent(String value);

    @Click(value = VERIFY_BUTTON, andWait = @Wait(value = SUBMIT_BUTTON, until = Until.CLICKABLE))
    PaymentEngineMonitoringChannelInstances clickOnVerify();

    @Click(value = SUBMIT_BUTTON, andWait = @Wait(value = SUBMIT_BUTTON, until = Until.GONE))
    PaymentEngineMonitoringChannelInstances clickOnOk();

    @CustomAction
    default PaymentEngineMonitoringInstanceFileRecords waitForChannelAndClickOnRecords(String value) {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        String ROW_FILE = String.format("//*[contains(@class,'x-grid3-body')]//div[text() = '%s']", value);
        endpoint.waitForElementAndRefresh(ROW_FILE, REFRESH_BUTTON, 30, 5);
        endpoint.click(() -> ROW_FILE);
        endpoint.sleepSeconds(2); //PAE při kliku na záznamy občas nezobrazí hned transakci, proto wait před klikem.
        endpoint.click(() -> RECORDS_BUTTON);
        return null;
    }

    @CustomAction
    default PaymentEngineMonitoringChannelInstances waitForChannelAndVerify(String value) {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        String ROW_FILE = String.format("//*[contains(@class,'x-grid3-body')]//div[text() = '%s']", value);
        endpoint.waitForElementAndRefresh(ROW_FILE, REFRESH_BUTTON, 30, 5);
        clickOnVerify().clickOnOk();
        return null;
    }
}

