package net.bugorfeature.basket.service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import net.bugorfeature.basket.model.ShoppingItem;


/**
 * Default static implementation of config service
 *
 * @author Andy Geach
 */
public class StaticConfigService implements ConfigService {

    private Map<ShoppingItem, ShoppingItemConfig> config = new HashMap<>();


    @Override
    public BigDecimal getPriceForItem(ShoppingItem item) {

        ShoppingItemConfig configItem = config.get(item);

        if (configItem == null) {
            throw new IllegalArgumentException("Cannot find item: " + item);
        }

        return configItem.getPrice();
    }

    @Override
    public int getMinimumForItem(ShoppingItem item) {
        ShoppingItemConfig configItem = config.get(item);

        if (configItem == null) {
            throw new IllegalArgumentException("Cannot find item: " + item);
        }

        return configItem.getMinCount();
    }

    @Override
    public Collection<ShoppingItem> itemList() {
        return config.keySet();
    }

    public void setConfigForItem(ShoppingItem item, BigDecimal price, int minimum) {
        ShoppingItemConfig sig = new ShoppingItemConfig();
        sig.setBasketItem(item);
        sig.setPrice(price);
        sig.setMinCount(minimum);
        config.put(item, sig);
    }


    public void buildDefault() {
        for (ShoppingItem item : ShoppingItem.values()) {
            ShoppingItemConfig sig = new ShoppingItemConfig();
            sig.setBasketItem(item);
            sig.setPrice(new BigDecimal("0.10").multiply(new BigDecimal(Integer.toString(item.ordinal() + 1)))); // just dummy data
            sig.setMinCount(1);
            config.put(item, sig);
        }
    }


    public static class ShoppingItemConfig {

        private ShoppingItem basketItem;

        private int minCount;

        private BigDecimal price;

        public ShoppingItem getBasketItem() {
            return basketItem;
        }

        public void setBasketItem(ShoppingItem basketItem) {
            this.basketItem = basketItem;
        }

        public int getMinCount() {
            return minCount;
        }

        public void setMinCount(int minCount) {
            this.minCount = minCount;
        }

        public BigDecimal getPrice() {
            return price;
        }

        public void setPrice(BigDecimal price) {
            this.price = price;
        }
    }
}

@@ -1265,41 +1265,8 @@
 ();%0A
-        sig.setBasketItem(item);%0A
     
@@ -1521,45 +1521,8 @@
 ();%0A
-            sig.setBasketItem(item);%0A
     
@@ -1784,50 +1784,8 @@
  %7B%0A%0A
-        private ShoppingItem basketItem;%0A%0A
     
@@ -1850,210 +1850,8 @@
 e;%0A%0A
-        public ShoppingItem getBasketItem() %7B%0A            return basketItem;%0A        %7D%0A%0A        public void setBasketItem(ShoppingItem basketItem) %7B%0A            this.basketItem = basketItem;%0A        %7D%0A%0A
     

Remove unnecessary reference to shopping item from config item