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

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.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.Select;
import cz.moneta.test.harness.support.web.Wait;
import org.junit.jupiter.api.Assertions;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

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

    String AUTH_PERSON_NAME = "//select[@name='authPerson']";
    String PAYCHECK = "//input[@id='vyplata']";

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

    /**
     * this method ensures that paycheck and output nominals still have same
     * values
     * 
     * @param expectedPaycheck
     * @return
     */
    @CustomAction
    default KSN33Page checkPaycheck(Double expectedPaycheck) {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        String paycheck = endpoint.getAttribute(PAYCHECK, Lookup.XPATH, "value");
        double paycheckDouble = 0.0;
        try {
            paycheckDouble = NumberFormat.getInstance(Locale.GERMAN).parse(paycheck).doubleValue();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Assertions.assertEquals(expectedPaycheck, paycheckDouble);
        return null;
    }

    @Select(value = AUTH_PERSON_NAME, by = Lookup.XPATH)
    KSN33Page authPerson(String text);

    @Wait(explicitWaitSeconds = 3)
    KSN33Page waitSec();
}
