package cz.moneta.test.dsl.ufo.banka.pages.requests;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.loans.creditCard.NKK006_S001_CreditCardScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP006_S001_ExpressScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.flexikredit.NFL006_S001_FlexikreditScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.consolidation.UDC006_S001_ConsolidationScoringPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.one.click.BAL015_S001_PackageDetail;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.WebEndpoint;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.OPN008_S001_SignContractPage.APPLICATION_ID_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.requests.BAL006_S002_ClientRequestPage.FILTER_LAYOUT;
import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;
import static cz.moneta.test.harness.support.web.Lookup.DEFAULT;
import static cz.moneta.test.harness.support.web.Lookup.XPATH;

@Wait(FILTER_LAYOUT)
@Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
public interface BAL006_S002_ClientRequestPage extends WebFlow<BAL006_S002_ClientRequestPage>, StoreAccessor {

    String POPUP = "//div[@class='popupContent']";
    String FILTER_LAYOUT = "BAL006_S002_X001";
    String REQUEST_DETAIL_BUTTON = "BAL006_S002_B005";
    String APPLICATION_ON_REQUEST_DETAIL_XPATH = "//div[@id='BAL006_S002_L011']//b[text()='%s']";
    String SEARCH_BUTTON = "BAL006_S002_B010";
    String SELECTED_LINE_XPATH = "//tr[contains(@class, 'ufo-Table-Row-Selected-even')]";
    String GENERAL_TEXT_CELLS_IN_SELECTED_LINE_XPATH = SELECTED_LINE_XPATH + "//td[contains(@class, 'ufo-Table-BodyCell') and contains(text(),'%s')]";
    String BACK_BUTTON = "BAL006_S002_B001";
    String ID_FIRST_REQUEST_RADIO = "BAL006_S002_G001-0";
    String APPLICATION_ID_XPATH = "//table[@id = 'BAL006_S002_G001_UfoTable']//td[@class = 'ufo-Table-BodyCell' and contains(text(), '%s')]/..//input";
    String APPLICATION_TYPE_XPATH ="//table[@id = 'BAL006_S002_G001_UfoTable']//td[@class = 'ufo-Table-BodyCell' and contains(text(), '%s')]/following-sibling::td[8]";
    String STATE_CHECK_TD_XPATH = "//td[text()='%s']/following-sibling::td[text()='%s']";
    String STATE_STORNO_TD_XPATH = "//td[text()='%s']/following-sibling::td[text()='STORNO' or text()='Storno probíhá']";
    String PRODUCT_FILTER_SELECT = "BAL006_S002_C001";
    String STATE_FILTER_SELECT = "BAL006_S002_C003";
    String CANCEL_FILTER_BUTTON = "BAL006_S002_B007";

    default void loadPage(WebEndpoint endpoint) {
        if (endpoint.isAlertPresent(3)) {
            endpoint.dismissAlert(10);
            endpoint.waitForElementsToLoad(30, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        }
    }

    @CustomAction
    default BAL006_S002_ClientRequestPage checkSelectedLinesTextContent(String... textToCheck) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        Arrays.stream(textToCheck)
                .filter(StringUtils::isNotEmpty)
                .forEach(t -> endpoint.isAtLeastOneElementPresent(String.format(GENERAL_TEXT_CELLS_IN_SELECTED_LINE_XPATH, t), XPATH));
        return null;
    }

    @Click(REQUEST_DETAIL_BUTTON)
    BAL006_S002_ClientRequestPageTransitions clickOpenRequestDetail();

    @Click(value = ID_FIRST_REQUEST_RADIO, andWait = @Wait(value = POPUP, until = Until.GONE))
    BAL006_S002_ClientRequestPage selectFirstApplication();

    @Click(BACK_BUTTON)
    HKO101_S001_MainClientPage clickBackToMainPage();

    @CustomAction
    default BAL006_S002_ClientRequestPage clickRequest(String applicationId) {
        String xPath = String.format(APPLICATION_ID_XPATH, applicationId);
        String applicationIdOnRequestDetailXPath = String.format(APPLICATION_ON_REQUEST_DETAIL_XPATH, applicationId);
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        if (!endpoint.isElementVisible(applicationIdOnRequestDetailXPath, XPATH)) {
            endpoint.click(() -> xPath, Lookup.XPATH);
        }
        endpoint.waitForElementsToLoad(10, Until.VISIBLE, REQUEST_DETAIL_BUTTON);
        return null;
    }

    @CustomAction
    default BAL006_S002_ClientRequestPage waitForStatusAndRefresh(String status) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.waitForElementAndRefresh(String.format(APPLICATION_ID_XPATH, status),
                SEARCH_BUTTON, XPATH, DEFAULT, 200, 5);
        return null;
    }

    @Wait(value = REQUEST_DETAIL_BUTTON, waitSecondsForElement = 15)
    BAL006_S002_ClientRequestPage waitForRequestDetailButton();

    @Click(REQUEST_DETAIL_BUTTON)
    BAL015_S001_PackageDetail clickToRequestDetailButton();

    @Click(REQUEST_DETAIL_BUTTON)
    NFL006_S001_FlexikreditScoringResultPage clickToRequestDetailButtonToFlexiScoringPage();

    @Click(REQUEST_DETAIL_BUTTON)
    NEP006_S001_ExpressScoringResultPage clickToRequestDetailButtonToExpresScoringPage();

    @Click(REQUEST_DETAIL_BUTTON)
    UDC006_S001_ConsolidationScoringPage clickToRequestDetailButtonToConsolidationScoringPage();

    @Click(REQUEST_DETAIL_BUTTON)
    NKK006_S001_CreditCardScoringResultPage clickRequestDetailButtonToCreditCardPage();

    @CustomAction
    default HKO101_S001_MainClientPage clickBackToMainClientPage() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.waitForElementsToLoad(45, BACK_BUTTON);
        endpoint.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        endpoint.sleep(TimeUnit.MILLISECONDS, 500);
        endpoint.click(() -> BACK_BUTTON);
        return null;
    }

    @Select(value = PRODUCT_FILTER_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    BAL006_S002_ClientRequestPage selectProductFilter(String product);

    @Select(value = STATE_FILTER_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    BAL006_S002_ClientRequestPage selectStateFilter(String state);

    @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
    @Click(value = CANCEL_FILTER_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    BAL006_S002_ClientRequestPage clickCancelFilter();

    @CustomAction
    default BAL006_S002_ClientRequestPage storeFirstApplicationId(Harness harness) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.waitForElementsToLoad(10, Until.PRESENT_IN_DOM, ID_FIRST_REQUEST_RADIO);
        String applicationID = endpoint.getAttribute(ID_FIRST_REQUEST_RADIO, "value");
        harness.store(APPLICATION_ID_KEY, applicationID);
        return null;
    }

    @CustomAction
    default BAL006_S002_ClientRequestPage checkApplicationState(String applicationId, String expectedState) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String xpath = String.format(STATE_CHECK_TD_XPATH, applicationId, expectedState);
        endpoint.waitForElementAndRefresh(xpath, SEARCH_BUTTON, Lookup.XPATH, Lookup.ID, 120, 10);
        return null;
    }
}
