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

import cz.moneta.test.dsl.ufo.banka.pages.loans.NEP017_S014_SelectApprovedVariantsPage;
import cz.moneta.test.dsl.ufo.banka.pages.requests.BAL006_S002_ClientRequestPage;
import cz.moneta.test.dsl.ufo.banka.pages.requests.NEP013_S001_RequestCancellationPage;
import cz.moneta.test.dsl.ufo.operations.shared.SharedElements;
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.consolidation.UDC006_S001_ConsolidationScoringPage.APP_KEY_DIV;
import static cz.moneta.test.dsl.ufo.banka.pages.products.consolidation.UDC006_S001_ConsolidationScoringPage.PAGE_ID;
import static cz.moneta.test.harness.support.web.Lookup.DEFAULT;
import static cz.moneta.test.harness.support.web.Lookup.XPATH;

@Wait(value = {PAGE_ID, APP_KEY_DIV}, waitSecondsForElement = 80)
public interface UDC006_S001_ConsolidationScoringPage extends WebFlow<UDC006_S001_ConsolidationScoringPage>, StoreAccessor, SharedElements {

    String PAGE_ID = "UDC006_S001_X001";
    String APP_ID_DIV = "UDC006_S001_L030";
    String APP_ID_STORE = "APP_ID_STORE";
    String APP_KEY_DIV = "UDC006_S001_L018";
    String APP_KEY_STORE = "APP_KEY_STORE";
    String APP_STAT_DIV = "UDC006_S001_L015";
    String APP_STAT_STORE = "APP_STAT_STORE";
    String PROCESS_REQUEST_BUTTON = "UDC006_S001_B008";
    String DETAILS_ON_REQUEST = "DETAILS_ON_REQUEST_STORE";
    String DETAILS_ON_REQUEST_DIV = "UDC006_S001_L003";
    String CONDITIONAL_AGREEMENT_BUTTON = "UDC006_S001_B012";
    String ACCOUNT_FOUNDATION_BUTTON = "UDC006_S001_B005";
    String STATUS_GENERAL_XPATH = "//div[@id='UDC006_S001_L015' and contains(text(), '%s')]";
    String RELOAD_BUTTON = "UDC006_S001_B015";
    String PROCESS_REQUEST_BUTTON_XPATH = "//button[@id='" + PROCESS_REQUEST_BUTTON + "' and not(@disabled)]";
    String STORNO_BUTTON = "UDC006_S001_B006";
    String END_BUTTON = "UDC006_S001_B002";

    @StoreElementContent(storeKey = APP_KEY_STORE, value = APP_KEY_DIV)
    UDC006_S001_ConsolidationScoringPage storeAppKey();

    @GetElementContent(APP_KEY_DIV)
    String getAppKey();

    @StoreElementContent(storeKey = APP_ID_STORE, value = APP_ID_DIV)
    UDC006_S001_ConsolidationScoringPage storeAppId();

    @GetElementContent(APP_ID_DIV)
    String getAppId();

    @StoreElementContent(storeKey = APP_STAT_STORE, value = APP_STAT_DIV)
    UDC006_S001_ConsolidationScoringPage storeAppStat();

    @GetElementContent(APP_STAT_DIV)
    String getAppStat();

    @StoreElementContent(storeKey = DETAILS_ON_REQUEST, value = DETAILS_ON_REQUEST_DIV)
    UDC006_S001_ConsolidationScoringPage storeDetailsOnRequest();

    @GetElementContent(DETAILS_ON_REQUEST_DIV)
    String getDetailsOnRequestDiv();

    @Click(PROCESS_REQUEST_BUTTON)
    UDC003_S001_CommitmentsToConsolidationPage clickProcessRequestButton();

    @Click(CONDITIONAL_AGREEMENT_BUTTON)
    UDC004_S001_LoanConsolidationPreApprovedOptions clickConditionalAgreementButton();

    @Click(ACCOUNT_FOUNDATION_BUTTON)
    NEP017_S014_SelectApprovedVariantsPage clickAccountFoundationButton();

    @CustomAction
    default UDC006_S001_ConsolidationScoringPage waitForStatusAndRefresh(String status) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.waitForElementAndRefresh(String.format(STATUS_GENERAL_XPATH, status),
                RELOAD_BUTTON, XPATH, DEFAULT, 40, 15);
        endpoint.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        return null;
    }

    @CustomAction
    default UDC006_S001_ConsolidationScoringPage waitForButtonAndRefresh() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> RELOAD_BUTTON);
        endpoint.waitForElementAndRefresh(PROCESS_REQUEST_BUTTON_XPATH,
                RELOAD_BUTTON, XPATH, DEFAULT, 40, 15);
        endpoint.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        return null;
    }

    @CheckElementPresent(APP_STAT_DIV)
    UDC006_S001_ConsolidationScoringPage checkStatus(String status);

    @Click(STORNO_BUTTON)
    NEP013_S001_RequestCancellationPage clickStorno();

    @Click(END_BUTTON)
    BAL006_S002_ClientRequestPage clickEnd();

}