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++) {
                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);
        }
    }
}

@@ -3099,16 +3099,18 @@
        %0A
+//
         
@@ -3138,24 +3138,183 @@
 %3C 3; i++) %7B%0A
+//                if(result.get(i).getArticleId().equals(%221%22) %7C%7C result.get(i).getArticleId().equals(%222%22) %7C%7C result.get(i).getArticleId().equals(%223%22)) %7B%0A//    
             
@@ -3344,16 +3344,21 @@
 et(i));%0A
+//   
         
@@ -3361,24 +3361,25 @@
             
+ 
 result.remov
@@ -3384,16 +3384,38 @@
 ove(0);%0A
+//                %7D%0A//
         

revert order things