package com.cjpowered.learn.inventory.ace;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import com.cjpowered.learn.inventory.InventoryDatabase;
import com.cjpowered.learn.inventory.InventoryManager;
import com.cjpowered.learn.inventory.Item;
import com.cjpowered.learn.inventory.Order;
import com.cjpowered.learn.marketing.MarketingInfo;

public final class AceInventoryManager implements InventoryManager {

	private final InventoryDatabase database;
	private final MarketingInfo marketInfo;
	
	public AceInventoryManager(InventoryDatabase database, MarketingInfo marketInfo){
		this.database = database;
		this.marketInfo = marketInfo;
	}
	
     @Override
    public List<Order> getOrders(final LocalDate today) {
    	 	final List<Order> orders = new ArrayList<>();
    	 	final List<Item> items = database.stockItems();
    	 	
    	 	for (Item item : items){
//    	 		final Optional<Order> order = item.createOrder(today, database, marketInfo);
    	 		final Order order = item.createOrder(today, database, marketInfo);
//    	 		System.out.println(order);
    	 		orders.add(order);
//    	 		order.ifPresent(orders.add(order));
    	 	}
    	 	
//    	 	for(Item item : items){
//    	 		final int onHand = database.onHand(item);
//    	 		final boolean onSale = marketInfo.onSale(item);
//    	 		final int toOrder;
//    	 		final boolean inSeason = marketInfo.season(today).equals(item.season());
//    	 		
//    	 		if (onSale){
//    	 			toOrder = item.wantOnHand() - onHand + 20;
//    	 		} else if(inSeason){
//    	 			toOrder = item.wantOnHand() * 2 - onHand;
//    	 		} else {
//    	 			toOrder = item.wantOnHand() - onHand;
//    	 		}
//    	 		if (toOrder > 0){
//        	 		final Order order = new Order(item, toOrder);
//        	 		orders.add(order);
//    	 		}
//    	 	}
    	 	return orders;
    }
}

@@ -1116,36 +1116,123 @@
 o);%0A
-//    %09 %09%09System.out.println
+    %09 %09%09if (order.quantity %3E 0)%7B%0A    %09 %09%09%09orders.add(order);%0A    %09 %09%09%7D%0A//    %09 %09%09order.quantity == 0 ? : orders.add
 (ord
@@ -1228,32 +1228,34 @@
 ers.add(order);%0A
+//
     %09 %09%09orders.a

refactor for tests to pass, used order.quantity instead of Optionals