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

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import cz.moneta.test.dsl.kasanova.KSNPageFlow;
import cz.moneta.test.dsl.kasanova.KSNUtils;
import cz.moneta.test.dsl.kasanova.components.enums.KSNAdress;
import cz.moneta.test.dsl.kasanova.components.enums.KSNBirthPlaces;
import cz.moneta.test.dsl.kasanova.components.enums.KSNCountries;
import cz.moneta.test.dsl.kasanova.components.enums.KSNGender;
import cz.moneta.test.dsl.kasanova.components.enums.KSNIdCards;
import cz.moneta.test.dsl.kasanova.components.enums.KSNNames;
import cz.moneta.test.dsl.kasanova.components.enums.KSNNumberGenerator;
import cz.moneta.test.dsl.kasanova.components.enums.KSNPatterns;
import cz.moneta.test.dsl.util.DateUtils;
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;

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

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

    @CustomAction
    default KSN62Page fillIdentificationAndSymbolsSmenaShort() {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        endpoint.type(() -> VARIABLE_SYMBOL_INPUT, KSNNumberGenerator.getRandomNumberValue(), false);
        endpoint.type(() -> CONSTANT_SYMBOL_INPUT, KSNNumberGenerator.getRandomNumberValue(), false);
        endpoint.type(() -> SPECIFIC_SYMBOL_INPUT, KSNNumberGenerator.getRandomNumberValue(), false);
        endpoint.type(() -> IDENTIFICATION_CLIENT_NAME_INPUT, KSNNames.getRandomNameValue(), true);
        endpoint.type(() -> IDENTIFICATION_PERSONAL_NUMBER_INPUT, DateUtils.getRandomDate(1940, 2000, KSNPatterns.DD_MM_YYYY.getValue()), false);
        endpoint.selectByVisibleText(IDENTIFICATION_SEX_SELECT, KSNGender.getRandomGenderValue());
        endpoint.selectByVisibleText(IDENTIFICATION_PERSONAL_NUMBER_TYPE_SELECT, KSNIdCards.getRandomIdCardValue());
        endpoint.type(() -> IDENTIFICATION_ID_NUMBER_INPUT, KSNNumberGenerator.getRandomNumberValue(), false);
        endpoint.selectByVisibleText(IDENTIFICATION_NATIONALITY_SELECT, KSNCountries.getRandomCountryValue());
        endpoint.type(() -> IDENTIFICATION_ADDRESS_INPUT, KSNAdress.getRandomAdressValue(), false);
        return null;
    }

    @CustomAction
    default KSN62Page fillIdentificationAndSymbolsSmenaLong() {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        endpoint.type(() -> VARIABLE_SYMBOL_INPUT, KSNNumberGenerator.getRandomNumberValue(), false);
        endpoint.type(() -> CONSTANT_SYMBOL_INPUT, KSNNumberGenerator.getRandomNumberValue(), false);
        endpoint.type(() -> SPECIFIC_SYMBOL_INPUT, KSNNumberGenerator.getRandomNumberValue(), false);
        endpoint.type(() -> IDENTIFICATION_CLIENT_NAME_INPUT, KSNNames.getRandomNameValue(), true);
        endpoint.type(() -> IDENTIFICATION_PERSONAL_NUMBER_INPUT, DateUtils.getRandomDate(1940, 2000, KSNPatterns.DD_MM_YYYY.getValue()), false);
        endpoint.selectByVisibleText(IDENTIFICATION_SEX_SELECT, KSNGender.getRandomGenderValue());
        endpoint.selectByVisibleText(IDENTIFICATION_PERSONAL_NUMBER_TYPE_SELECT, KSNIdCards.getRandomIdCardValue());
        endpoint.type(() -> IDENTIFICATION_ID_NUMBER_INPUT, KSNNumberGenerator.getRandomNumberValue(), false);
        endpoint.type(() -> IDENTIFICATION_ID_END_DATE_INPUT, DateUtils.getRandomDate(YEAR_ONE.getYear(), YEAR_MORE.getYear(), KSNPatterns.DDMMYYYY.getValue()), true);
        String coutry = KSNCountries.getRandomCountryValue();
        endpoint.selectByVisibleText(IDENTIFICATION_COUNTRY_ID_SELECT, coutry);
        endpoint.selectByVisibleText(IDENTIFICATION_LONG_TERM_RESIDENCE_SELECT, coutry);
        endpoint.selectByVisibleText(IDENTIFICATION_NATIONALITY_SELECT, coutry);
        endpoint.type(() -> IDENTIFICATION_BIRTH_PLACE_INPUT, KSNBirthPlaces.getRandomBirthPlaceValue(), true);
        endpoint.type(() -> IDENTIFICATION_ADDRESS_INPUT, KSNAdress.getRandomAdressValue(), true);
        return null;
    }

    // Coinsplit table is filled with nominals according to field 'Částka směny' (smena)
    @CustomAction
    default KSN62Page fillCoinsplitTable() {
        final String INPUT_SMENA = "//input[@id='smena']"; // Částka směny
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        String smenaValue = endpoint.getAttribute(INPUT_SMENA, Lookup.XPATH, "value");
        int smenaValueInt = 0;
        try { 
            NumberFormat formatter = NumberFormat.getNumberInstance(Locale.GERMAN);
            smenaValueInt = formatter.parse(smenaValue).intValue();
        } catch(ParseException e) {
             e.printStackTrace();
        }
        List<Integer> tableValues = new ArrayList<>();
        tableValues.add(5000);
        tableValues.add(2000);
        tableValues.add(1000);
        tableValues.add(500);
        tableValues.add(200);
        tableValues.add(100);
        tableValues.add(50);
        tableValues.add(20);
        tableValues.add(10);
        tableValues.add(5);
        tableValues.add(2);
        tableValues.add(1);

        Map<String, String> coinSplitMap = new LinkedHashMap<>();

        for (int value : tableValues) {
            if (smenaValueInt <= 0) {
                break;
            }
            int result = smenaValueInt / value;
            if (result > 0) {
                if (value >= 100) {
                    coinSplitMap.put("csM-CZK-B-" + value, String.valueOf(result));
                } else {
                    coinSplitMap.put("csM-CZK-C-" + value, String.valueOf(result));
                }
                smenaValueInt = smenaValueInt % value;
            }
        }
        for (String item : coinSplitMap.keySet()) {
            String xpath = String.format(COINSPLIT_DYNAMIC_PATH, item);
            endpoint.type(() -> xpath, coinSplitMap.get(item), false, Lookup.XPATH);
        }

        return null;
    }

}
