package cz.moneta.test.dsl.ufo.banka.pages.loans.expres;

import cz.moneta.test.dsl.ufo.banka.pages.requests.*;
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.loans.expres.NEP006_S001_ExpressScoringResultPage.*;
import static cz.moneta.test.dsl.ufo.banka.tasks.expres.ExpressTasks.APPL_KEY_STORE_KEY;

@Wait(value = PAGE_ID_XPATH, by = Lookup.XPATH, waitSecondsForElement = 120)
@Wait({APP_KEY_DIV, APP_ID_DIV, APP_STAT_DIV})
@Log(value = APP_ID_DIV, template = "APP ID is {}")
@Log(value = APP_STAT_DIV, template = "APP State is {}")
public interface NEP006_S001_ExpressScoringResultPage extends WebFlow<NEP006_S001_ExpressScoringResultPage>, StoreAccessor, SharedElements {

    String PAGE_ID_XPATH = "//div[text()='NEP006_S001']";
    String APP_ID_DIV = "NEP006_S001_L030";
    String APP_ID_STORE = "APP_ID_STORE";
    String APP_KEY_DIV = "NEP006_S001_L007";
    String APP_KEY_STORE = "APP_KEY_STORE";
    String APP_STAT_DIV = "NEP006_S001_L015";
    String STATUS_GENERAL_XPATH = "//div[@id='NEP006_S001_L015' and contains(text(), '%s')]";
    String APP_STAT_STORE = "APP_STAT_STORE";
    String END_BUTTON = "NEP006_S001_B002";
    String CREATE_ACCOUNTS_BUTTON = "NEP006_S001_B005";
    String CREATE_ACCOUNT_BUTTON_XPATH = "//button[@id='" + CREATE_ACCOUNTS_BUTTON + "' and not(@disabled)]";
    String RELOAD_PAGE_BUTTON = "NEP006_S001_S001";
    String CANCEL_BUTTON = "NEP006_S001_B006";
    String UPLOAD_DOCUMENTS_BUTTON = "NEP006_S001_B011";
    String FINISH_REQUEST_BUTTON = "NEP006_S001_B008";

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

    @Log(value = APP_ID_DIV, template = "Client Express Application ID is {}")
    @StoreElementContent(storeKey = APP_ID_STORE, value = APP_ID_DIV)
    NEP006_S001_ExpressScoringResultPage storeAppId();

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

    @Wait(APP_STAT_DIV)
    @Log(value = APP_STAT_DIV, template =  "APP State is {}")
    NEP006_S001_ExpressScoringResultPage logAppState();

    @GetElementContent(APP_KEY_DIV)
    String getAppKey();

    @GetElementContent(APP_ID_DIV)
    String getAppId();

    @GetElementContent(APP_STAT_DIV)
    String getAppStat();

    @Click(END_BUTTON)
    NEP006_S001_ExpressScoringResultPageTransitions clickEnd();

    @Click(CREATE_ACCOUNTS_BUTTON)
    NEP006_S001_ExpressScoringResultPageTransitions clickCreateAccounts();

    @CustomAction
    default NEP006_S001_ExpressScoringResultPage waitForStatusAndRefresh(String status) {//TODO JBR - use enum
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        store(APPL_KEY_STORE_KEY, endpoint.getText(APP_KEY_DIV));
        endpoint.waitForElementAndRefresh(String.format(STATUS_GENERAL_XPATH, status), RELOAD_PAGE_BUTTON, Lookup.XPATH, Lookup.DEFAULT, 240, 15);
        endpoint.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        return null;
    }

    @CustomAction
    default NEP006_S001_ExpressScoringResultPage waitForActiveCreateAccountButton() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.waitForElementAndRefresh(CREATE_ACCOUNT_BUTTON_XPATH, RELOAD_PAGE_BUTTON, Lookup.XPATH, Lookup.DEFAULT,300, 10);
        return null;
    }

    @Click(CANCEL_BUTTON)
    NEP013_S001_RequestCancellationPage clickCancelButton();

    @Click(UPLOAD_DOCUMENTS_BUTTON)
    NEP016_S021_DocumentedEvidenceListPage clickFillInDocuments();

    @Click(FINISH_REQUEST_BUTTON)
    NEP044_S001_FinishRequestCreatedByClientInInternetPage clickFinishRequestButton();

}