package ru.job4j.collections.orderbook;

import org.xml.sax.SAXException;

import javax.xml.parsers.*;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Order book.
 * @author Hincu Andrei (andreih1981@gmail.com)on 23.10.2017.
 * @version $Id$.
 * @since 0.1.
 */
public class OrderBook {
    /**
     * Хранилище книг.
     */
    private Map<String, HashMap<Integer, Order>> orders = new HashMap<>();

    /**
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public void parseXml() throws ParserConfigurationException, IOException, SAXException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        Handler handler = new Handler(this);
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new File("order.xml"), handler);
    }

    public void addOrRemove(Order order, boolean flag) {
        orders.putIfAbsent(order.getBook(), new HashMap<Integer, Order>());
        if (flag) {
            HashMap<Integer, Order> map = orders.get(order.getBook());
            map.put(order.getId(), order);
        } else {
            orders.get(order.getBook()).remove(order.getId());
        }
    }

    public HashMap<String, TreeSet<Order>> aggregation() {
        HashMap<String, TreeSet<Order>> result = new HashMap<>();
        for (HashMap<Integer, Order> map : orders.values()) {

            for (Order order : map.values()) {
                boolean f = false;
                result.putIfAbsent(order.getBook(), new TreeSet<Order>());
                TreeSet<Order> tree = result.get(order.getBook());
                if (!tree.isEmpty()) {
                    for (Iterator<Order> iterator = tree.iterator(); iterator.hasNext(); ) {
                        Order treeOrder = iterator.next();

                        final float prise = order.getPrice() - treeOrder.getPrice();
                        final String treeOrderOperation = treeOrder.getOperation();
                        final String orderOperation = order.getOperation();
                        final int volume = order.getVolume() - treeOrder.getVolume();
                        if (prise == 0 && treeOrderOperation.equals(orderOperation)) {
                            treeOrder.setVolume(treeOrder.getVolume() + order.getVolume());
                            f = true;
                            break;
                        }
                        if (prise >= 0) {
                            if (orderOperation.equals("BUY") && treeOrderOperation.equals("SELL")) {
                                if (volume > 0) {
                                    iterator.remove();
                                    // tree.remove(treeOrder);
                                    order.setVolume(volume);
                                    tree.add(order);
                                    f = true;
                                    break;
                                }
                                if (volume < 0) {
                                    treeOrder.setVolume(Math.abs(volume));
                                    //  treeOrder.setPrice(order.getPrice());
                                    f = true;
                                    break;
                                }
                                if (volume == 0) {
                                    iterator.remove();
                                    f = true;
                                    break;
                                }
                            }
                        }
                        if (prise <= 0) {
                            if (orderOperation.equals("SELL") && treeOrderOperation.equals("BUY")) {
                                if (volume > 0) {
                                    // tree.remove(treeOrder);
                                    iterator.remove();
                                    order.setVolume(volume);
                                    tree.add(order);
                                    f = true;
                                    break;
                                }
                                if (volume < 0) {
                                    treeOrder.setVolume(Math.abs(volume));
                                    // treeOrder.setPrice(order.getPrice());
                                    f = true;
                                    break;
                                }
                                if (volume == 0) {
                                    // tree.remove(treeOrder);
                                    iterator.remove();
                                    f = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!f) {
                        tree.add(order);
                    }

                } else {
                    tree.add(order);
                }
            }
        }
        return result;
    }
}
@@ -836,16 +836,17 @@
 e(%22order
+s
 .xml%22), 
@@ -1413,17 +1413,16 @@
 es()) %7B%0A
-%0A
         
@@ -1464,43 +1464,8 @@
 ) %7B%0A
-                boolean f = false;%0A
     
@@ -1789,24 +1789,66 @@
 tor.next();%0A
+                        boolean f = false;
 %0A           
@@ -2350,46 +2350,8 @@
 ));%0A
-                            f = true;%0A
     
@@ -2410,32 +2410,13 @@
    %7D
-%0A                       
+ else
  if 
@@ -2625,36 +2625,44 @@
 iterator.remove(
+treeOrder
 );%0A
- 
                 
@@ -2825,53 +2825,8 @@
 r);%0A
-                                    f = true;
 %0A   
@@ -2890,33 +2890,45 @@
                %7D
-%0A
+ else %7B%0A     
                 
@@ -2934,33 +2934,32 @@
                 
- 
 if (volume %3C 0) 
@@ -2945,33 +2945,34 @@
      if (volume 
-%3C
+%3E=
  0) %7B%0A          
@@ -3001,47 +3001,38 @@
     
-treeOrder.setVolume(Math.abs(volume));%0A
+    if (volume == 0) %7B%0A       
     
@@ -3069,45 +3069,30 @@
   //
- 
  tree
-Order.setPrice(order.getPrice()
+.remove(treeOrder
 );%0A 
@@ -3122,34 +3122,59 @@
             
-f = true;%0A
+        iterator.remove();%0A        
             
@@ -3228,32 +3228,40 @@
                 
+  
+      
 %7D%0A              
@@ -3278,32 +3278,26 @@
         
-if (volume == 0)
+    %7D else
  %7B%0A     
@@ -3327,32 +3327,56 @@
         
-iterator.remove(
+    treeOrder.setVolume(Math.abs(volume)
 );%0A     
@@ -3402,32 +3402,33 @@
             
-f = true
+    break
 ;%0A          
@@ -3445,38 +3445,33 @@
                 
-break;
+%7D
 %0A               
@@ -3548,32 +3548,13 @@
    %7D
-%0A                       
+ else
  if 
@@ -3746,33 +3746,32 @@
                 
- 
 // tree.remove(t
@@ -3954,54 +3954,8 @@
 r);%0A
-                                    f = true;%0A
     
@@ -4156,131 +4156,8 @@
 ));%0A
-                                    // treeOrder.setPrice(order.getPrice());%0A                                    f = true;%0A
     
@@ -4307,33 +4307,32 @@
                 
- 
 // tree.remove(t
@@ -4401,54 +4401,8 @@
 ();%0A
-                                    f = true;%0A
     
@@ -4534,60 +4534,8 @@
   %7D%0A
-                    %7D%0A                    if (!f) %7B%0A
     
@@ -4589,25 +4589,24 @@
           %7D%0A
-%0A
             

Order Book test