package cz.moneta.test.dsl.finanso;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.MonetaPortalEndpoint;
import cz.moneta.test.harness.support.web.*;

@Wait(FinansoIncomePage.INCOME_INPUT)
@Wait(value = FinansoIncomePage.LOADER, until = Until.GONE)
public interface FinansoIncomePage extends WebFlow<FinansoIncomePage>, StoreAccessor {
    String LOADER = "//div[contains(@class,'loader')]";
    String INCOME_INPUT = "//input[@id='income']";
    String INSTALLMENT_INPUT = "//input[@id='otherInstallment']";
    String REALITY_AMOUNT_INPUT = "//input[@id='realtyAmount']";
    String MORTGAGE_AMOUNT = "//input[@id='amountMortgage']";
    String MORTGAGE_MATURITY = "//input[@id='term']";
    String MORTGAGE_FIXATION = "//div[@name='cbFixation.code']";
    String BACK_BUTTON = "//button[@data-testid='back']";

    @CustomAction
    default FinansoIncomePage storeIncomeValue(String storeKey) {
        store(storeKey, getInputValue(INCOME_INPUT));
        return null;
    }

    @CustomAction
    default FinansoIncomePage storeInstallmentValue(String storeKey) {
        store(storeKey, getInputValue(INSTALLMENT_INPUT));
        return null;
    }

    @CustomAction
    default FinansoIncomePage storeFinansoAmount(String storeKey) {
        store(storeKey, getInputValue(MORTGAGE_AMOUNT));
        return null;
    }

    @CustomAction
    default FinansoIncomePage storeFinansoRealityAmount(String storeKey) {
        store(storeKey, getInputValue(REALITY_AMOUNT_INPUT));
        return null;
    }

    @CustomAction
    default FinansoIncomePage storeFinansoMaturity(String storeKey) {
        store(storeKey, getInputValue(MORTGAGE_MATURITY));
        return null;
    }

    @CustomAction
    default FinansoIncomePage storeFinansoFixation(String storeKey) {
        store(storeKey, getInputValue(MORTGAGE_FIXATION).replaceFirst("^0+", ""));
        return null;
    }

    @Click(BACK_BUTTON)
    FinansoIncomePage goBack();

    private String getInputValue(String xpath) {
        MonetaPortalEndpoint e = getEndpoint(MonetaPortalEndpoint.class);
        return e.getAttribute(xpath, "value").replaceAll("\\D", "");
    }

    private String getElementText(String xpath) {
        MonetaPortalEndpoint e = getEndpoint(MonetaPortalEndpoint.class);
        return e.getText(xpath).replaceAll("\\D", "");
    }
}
