package com.github.phasebash.katas.vendingmachine.core.input;

import com.github.phasebash.katas.vendingmachine.core.bank.Coin;
import lombok.NonNull;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * A default implementation of a coin parser.
 */
public class DefaultCoinParser implements CoinParser {

    private static final Map<String, Coin> SUPPORTED_COIN_MAP = new HashMap<>();

    static {
        SUPPORTED_COIN_MAP.put("$0.05", Coin.nickle);
        SUPPORTED_COIN_MAP.put("$0.10", Coin.dime);
        SUPPORTED_COIN_MAP.put("$0.25", Coin.quarter);
    }

    /**
     * Parse coin strings.  Coins are in the form $0.25, $0.05, and $0.10.  All other tokens are considered
     * UNSUPPORTED.
     *
     * @param coinString The uncontrolled coin string.
     * @return The list of Coin enum values.  Always immutable.
     */
    @Override
    public @NonNull
    List<Coin> parseCoins(@NonNull final String coinString) {
        final String trimmed = coinString.trim();

        if ("".equals(trimmed)) {
            return Collections.emptyList();
        }

        final List<Coin> coinz =
                Arrays.asList(coinString.trim().split("\\s+"))
                        .stream().map(this::findCoinType).collect(Collectors.toList());

        return Collections.unmodifiableList(coinz);
    }

    private Coin findCoinType(final String centValue) {
        Coin coin = SUPPORTED_COIN_MAP.get(centValue);

        if (coin == null) {
            coin = Coin.unsupported;
        }

        return coin;
    }

}

@@ -121,16 +121,82 @@
 k.Coin;%0A
+import com.github.phasebash.katas.vendingmachine.core.bank.Coins;%0A
 import l
@@ -627,15 +627,18 @@
 Coin
+s
 .nickle
+()
 );%0A 
@@ -684,13 +684,16 @@
 Coin
+s
 .dime
+()
 );%0A 
@@ -739,16 +739,17 @@
 Coin
+s
 .quarter
 );%0A 
@@ -744,16 +744,18 @@
 .quarter
+()
 );%0A    %7D
@@ -1674,19 +1674,75 @@
 oin.
-unsupported
+builder().value(0).recognized(false).description(centValue).build()
 ;%0A  

refactor DefaultCoinParser to use Coin class