package cz.moneta.test.dsl.kasanova.components;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.kasanova.KSNPageFlow;
import cz.moneta.test.dsl.kasanova.KSNUtils;
import cz.moneta.test.harness.endpoints.WebEndpoint;
import cz.moneta.test.harness.endpoints.kasanova.KasanovaEndpoint;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.Until;

public interface KSN42Page extends KSNPageFlow<KSN42Page>, KSNUtils<KSN42Page> {

    default void loadPage(WebEndpoint endpoint) {
        KSNPageFlow.loadLocalPage(endpoint, WITHDRAW_AMOUNT_INPUT);
    }

    /**
     * converts amount of money from CZK currency to foreign currency and result
     * inserts into WITHDRAW_AMOUNT_INPUT field
     * 
     * @param money
     *            - amount of money in CZK currency
     * @return
     */
    @CustomAction
    default KSN42Page calculateWithdrawAmount(double money) {
        final String EXCHANGE_RATE = "//input[@id='exchangeRate']";
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        String exchangeRateValue = endpoint.getAttribute(EXCHANGE_RATE, Lookup.XPATH, "value");
        if (!exchangeRateValue.isEmpty()) {
            double exchangeRateDouble = Double.valueOf(exchangeRateValue);
            if (exchangeRateDouble > 0) {
                money = money / exchangeRateDouble;
            }
        }
        String result = String.format("%.2f", money);
        endpoint.type(() -> WITHDRAW_AMOUNT_INPUT, result, false);
        return null;
    }

    @CustomAction
    default KSN42Page doVerification(Harness harness, String verifyAccount, Credentials verifyBanker) {
        String VERIFY_BANKER_UID = "kasanova-config.verify-banker.uid";
        final String WITHDRAW_AMOUNT = "//input[@id='withdrawAmountId']";
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        String withdrawAmountValue = endpoint.getAttribute(WITHDRAW_AMOUNT, Lookup.XPATH, "value");
        endpoint.waitForElementsToLoad(80, Until.GONE, LOADER_DIV);
        endpoint.waitForAnyElementToLoad(30, Lookup.XPATH, BUTTON_KASADIALOG, VERIFY_DIALOG_IFRAME);
        endpoint.switchToFrame(VERIFY_DIALOG_IFRAME, Lookup.XPATH);
        endpoint.waitForElementsToLoad(20, Lookup.XPATH, VERIFY_DIALOG_AMOUNT);
        endpoint.type(() -> VERIFY_DIALOG_AMOUNT, withdrawAmountValue, false, Lookup.XPATH);
        endpoint.type(() -> VERIFY_DIALOG_ACCOUNT, verifyAccount, false, Lookup.XPATH);
        endpoint.type(() -> VERIFY_DIALOG_UID, harness.getConfig(VERIFY_BANKER_UID), false, Lookup.XPATH);
        endpoint.type(() -> VERIFY_DIALOG_SSOID, verifyBanker.getUsername(), false, Lookup.XPATH);
        endpoint.type(() -> VERIFY_DIALOG_PASSWORD, verifyBanker.getPassword(), false, Lookup.XPATH);
        endpoint.switchToDefaultContent();
        endpoint.switchToFrame(MAIN_IFRAME_NAME);
        endpoint.click(() -> VERIFY_BUTTON, Lookup.XPATH);
        return null;
    }

}
