package cz.moneta.test.dsl.ufo.banka.pages.products.sme;

import cz.moneta.test.dsl.ufo.banka.pages.requests.BAL006_S002_ClientRequestPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.SMA002_S001_SMEAutomatsApplicationOverview.SMA002_S001_PAGE_LABEL_XPATH;

@Wait(value = SMA002_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 60)
public interface SMA002_S001_SMEAutomatsApplicationOverview extends WebFlow<SMA002_S001_SMEAutomatsApplicationOverview>, StoreAccessor {

    String SMA002_S001_PAGE_LABEL_XPATH = "//div[text()='SMA002_S001']";
    String ACCOUNT_NUMBER_KEY = "ACCOUNT_NUMBER";

    String LOADER_DIV = "SMA002_S001_X080";
    String BACK_TO_APPLICATIONS_LIST_BUTTON = "SMA002_S001_B006";
    String LOADER_AFTER_MODULATION_DIV = "SMA002_S001_X013";
    String PERSON_DETAIL_BUTTON = "SMA002_S001_B019";
    String WIFE_HUSBAND_AGREEMENT_BUTTON = "SMA002_S001_B020";
    String DOCUMENTS_PERSONS_TO_SIGN_BUTTON = "SMA002_S001_B009";
    String APPLICATION_RADIO = "//td[text()=\"%s\"]/..//input";
    String EXCEPTION_BUTTON = "SMA002_S001_B003";
    String EXPRESMAN_DECISION_BUTTON = "SMA002_S001_B001";
    String FILL_DOCUMENTS_BUTTON = "SMA002_S001_B004";
    String REQUIRED_PRODUCT_BUTTON = "SMA002_S001_B002";
    String PARAMETERS_VARIANTS_BUTTON = "SMA002_S001_B007";
    String PRINT_OFFER_BUTTON = "SMA002_S001_B016";
    String PRINT_SIGN_DEAL_BUTTON = "SMA002_S001_B017";
    String STORNO_SELECTED_APPLICATION_BUTTON = "SMA002_S001_B024";
    String APPLICATION_DUPLICATE_BUTTON = "SMA002_S001_B005";
    String CONSOLIDATED_COMMITMENTS_BUTTTON = "SMA002_S001_B010";
    String EXPRESS_BUSINESS_SECURED_BUTTON = "SMA002_S001_B012";
    String STORNO_ALL_APPLICATIONS_BUTTON = "SMA002_S001_B013";
    String ACCEPTATION_BUTTON = "SMA002_S001_B014";
    String ACCEPTATION_MODAL_BUTTON = "SMA002_S001_B015";
    String ACCOUNT_NUMBER_XPATH = "//table[@id='SMA002_S001_G002_UfoTable']//td[text()='%s']/following-sibling::td[2]";

    @Click(BACK_TO_APPLICATIONS_LIST_BUTTON)
    BAL006_S002_ClientRequestPage clickBackToApplicationsList();

    @Click(PERSON_DETAIL_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickPersonDetail();

    @Click(WIFE_HUSBAND_AGREEMENT_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickwifeHusbandAgreement();

    @Click(DOCUMENTS_PERSONS_TO_SIGN_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickDocumentsPersonsToSign();

    @StoreElementContent(storeKey = ACCOUNT_NUMBER_KEY, value = ACCOUNT_NUMBER_XPATH, by = Lookup.XPATH, isStringDynamicXpath = true)
    SMA002_S001_SMEAutomatsApplicationOverview storeAccountNumber(String application);

    @Wait(value = LOADER_DIV, until = Until.GONE)
    @Wait(value = LOADER_AFTER_MODULATION_DIV, until = Until.GONE)
    @Click(value = APPLICATION_RADIO, by = Lookup.XPATH, isStringDynamicXpath = true, andWait = @Wait(value = LOADER_DIV, until = Until.GONE))
    SMA002_S001_SMEAutomatsApplicationOverview clickApplication(String application);

    @Click(EXCEPTION_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickException();

    @Click(EXPRESMAN_DECISION_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickExpresmanDecision();

    @Click(FILL_DOCUMENTS_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickFillDocuments();

    @Click(REQUIRED_PRODUCT_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickRequiredProduct();

    @Wait(value = LOADER_DIV, until = Until.GONE)
    @CustomAction
    default SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickParametersVariants(String application) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> PARAMETERS_VARIANTS_BUTTON);
        if ((endpoint.isAlertPresent(5)) &&
                (endpoint.getAlertText(1).equals("Čekejte, prosím - probíhá zpracování předchozího požadavku."))) {
            endpoint.acceptAlert(3);
            endpoint.sleepSeconds(5);
            clickApplication(application);
            endpoint.click(() -> PARAMETERS_VARIANTS_BUTTON);
        }
        return null;
    }

    @Click(PRINT_OFFER_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickPrintOffer();

    @Wait(value = LOADER_DIV, until = Until.GONE)
    @CustomAction
    default SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickPrintSignDeal() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> PRINT_SIGN_DEAL_BUTTON);
        if ((endpoint.isAlertPresent(5)) &&
                (endpoint.getAlertText(1).equals("Čekejte, prosím - probíhá zpracování předchozího požadavku."))) {
            endpoint.acceptAlert(3);
            endpoint.sleepSeconds(5);
            endpoint.click(() -> PRINT_SIGN_DEAL_BUTTON);
        }
        return null;
    }

    @Click(STORNO_SELECTED_APPLICATION_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickStornoSelectedApplication();

    @Click(APPLICATION_DUPLICATE_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickApplicationDuplicate();

    @Click(CONSOLIDATED_COMMITMENTS_BUTTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickConsolidatedCommitments();

    @Click(EXPRESS_BUSINESS_SECURED_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickExpressBusinessSecured();

    @Click(STORNO_ALL_APPLICATIONS_BUTTON)
    SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickStornoAllApplications();

    @Wait(value = LOADER_DIV, until = Until.GONE)
    @CustomAction
    default SMA002_S001_SMEAutomatsApplicationOverview clickAcceptation() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> ACCEPTATION_BUTTON);
        if ((endpoint.isAlertPresent(5)) &&
                (endpoint.getAlertText(1).equals("Čekejte, prosím - probíhá zpracování předchozího požadavku."))) {
            endpoint.acceptAlert(3);
            endpoint.waitForElementsToLoad(10, Lookup.DEFAULT, Until.GONE, LOADER_DIV);
            endpoint.sleepSeconds(5);
            endpoint.click(() -> ACCEPTATION_BUTTON);
        }
        return null;
    }

    @Wait(value = LOADER_DIV, until = Until.GONE)
    @CustomAction
    default SMA002_S001_SMEAutomatsApplicationsOverviewTransitions clickAcceptationModalIfPresent() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(ACCEPTATION_MODAL_BUTTON, Lookup.DEFAULT)) {
            endpoint.click(() -> ACCEPTATION_MODAL_BUTTON);
        }
        if ((endpoint.isAlertPresent(5)) &&
                (endpoint.getAlertText(1).equals("Čekejte, prosím - probíhá zpracování předchozího požadavku."))) {
            endpoint.acceptAlert(3);
            endpoint.waitForElementsToLoad(10, Lookup.DEFAULT, Until.GONE, LOADER_DIV);
            endpoint.sleepSeconds(5);
            endpoint.click(() -> ACCEPTATION_MODAL_BUTTON);
        }
        endpoint.waitForElementsToLoad(120, Lookup.DEFAULT, Until.GONE, LOADER_AFTER_MODULATION_DIV);

        return null;
    }

}

