package de.ibsys.planningTool.controller.tab;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.jfoenix.controls.JFXButton;

import de.ibsys.planningTool.controller.MainController;
import de.ibsys.planningTool.controller.tab.productionOrderTab.ChildBikeController;
import de.ibsys.planningTool.controller.tab.productionOrderTab.MenBikeController;
import de.ibsys.planningTool.controller.tab.productionOrderTab.WomenBikeController;
import de.ibsys.planningTool.model.xmlExportModel.Item;
import de.ibsys.planningTool.service.ProductionService;
import javafx.fxml.FXML;
import javafx.stage.Stage;

/**
 * Created by minhnguyen on 22.09.16.
 */
public class ProductionController extends BaseTabController {

    private MainController mainController;

    @FXML
    ChildBikeController childBikeController;

    @FXML
    MenBikeController menBikeController;

    @FXML
    WomenBikeController womenBikeController;

    @FXML
    public JFXButton saveBtn;

    @Override
    public void start(Stage primaryStage) throws Exception {
    }

    @Override
    public void init(MainController main) {
        super.init(main);
        mainController = main;
        childBikeController.init(this);
        menBikeController.init(this);
        womenBikeController.init(this);
    }

    public MainController getMainController() {
        return mainController;
    }

    public void setMainController(MainController mainController) {
        this.mainController = mainController;
    }

    public ResourceBundle getTranslation() {
        return main.getTranslation();
    }

    @FXML
    public void saveBtnPressed() {
        initProductionControllerCalculation();
    }

    public void initProductionControllerCalculation () {
        try {
            childBikeController.storeNewReserve();
            menBikeController.storeNewReserve();
            womenBikeController.storeNewReserve();
            childBikeController.initUIComponents();
            menBikeController.initUIComponents();
            womenBikeController.initUIComponents();

            List<Item> result = new ArrayList<>();

            result.clear();

            result = new ProductionService().calculate( // do calculation and reduce 
                    Stream.concat(childBikeController.setList().parallelStream(), //
                            Stream.concat(menBikeController.setList().parallelStream(), //
                                    womenBikeController.setList().parallelStream() //
                            ).collect(Collectors.toList()).parallelStream()) // get all list from men and women in one list
                            .collect(Collectors.toList())) // get all list form views together
                    .stream()
                    .sorted((item1, item2) -> Integer.valueOf(item1.getArticleId()) // sorted things 
                            .compareTo(Integer.valueOf(item2.getArticleId()))) //
                    .collect(Collectors.toList()); // return a list
            
//            for(int i = 0; i < 3; i++) {
//                if(result.get(i).getArticleId().equals("1") || result.get(i).getArticleId().equals("2") || result.get(i).getArticleId().equals("3")) {
//                    result.add(result.get(i));
//                    result.remove(0);
//                }
//            }

            getMainController().setProductionList(result);
            main.initWorkThings();
            main.orderController.getData();
            main.cappla.setDisable(false);
            main.orderTab.setDisable(false);
            main.productionpriorityTab.setDisable(false);
            logger.info("save new stuff successfull");
        } catch (NullPointerException e) {
            logger.info(e);
        }
    }
}

@@ -44,161 +44,8 @@
 b;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.ResourceBundle;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream;%0A%0A
 impo
@@ -79,17 +79,16 @@
 Button;%0A
-%0A
 import d
@@ -550,16 +550,169 @@
 Stage;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.ResourceBundle;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream;%0A%0A
 /**%0A * C
@@ -3081,24 +3081,25 @@
 turn a list%0A
+%0A
             
@@ -3102,58 +3102,148 @@
     
-%0A//            for(int i = 0; i %3C 3; i++) %7B%0A//
+if (result.get(0).getArticleId().equals(%221%22)) %7B%0A                result.add(result.get(0));%0A                result.remove(0);%0A        
     
+%7D%0A%0A
     
@@ -3252,16 +3252,17 @@
       if
+ 
 (result.
@@ -3257,33 +3257,33 @@
  if (result.get(
-i
+0
 ).getArticleId()
@@ -3295,59 +3295,123 @@
 ls(%22
-1%22) %7C%7C result.get(i).getArticleId().equals(%222%22) %7C%7C 
+2%22)) %7B%0A                result.add(result.get(0));%0A                result.remove(0);%0A            %7D%0A%0A            if (
 resu
@@ -3417,17 +3417,17 @@
 ult.get(
-i
+0
 ).getArt
@@ -3450,22 +3450,16 @@
 %223%22)) %7B%0A
-//    
         
@@ -3492,15 +3492,13 @@
 get(
-i
+0
 ));%0A
-//
     
@@ -3501,36 +3501,32 @@
                 
-    
 result.remove(0)
@@ -3531,30 +3531,8 @@
 0);%0A
-//                %7D%0A//
     

Darth vader