package devopsdistilled.operp.client.stock.panes;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.inject.Inject;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import devopsdistilled.operp.client.abstracts.SubTaskPane;
import devopsdistilled.operp.client.stock.controllers.WarehouseController;
import devopsdistilled.operp.client.stock.models.observers.WarehouseModelObserver;
import devopsdistilled.operp.client.stock.panes.controllers.TransferStockPaneController;
import devopsdistilled.operp.client.stock.panes.models.observers.TransferStockPaneModelObserver;
import devopsdistilled.operp.server.data.entity.stock.Warehouse;

public class TransferStockPane extends SubTaskPane implements
		TransferStockPaneModelObserver, WarehouseModelObserver {

	@Inject
	private TransferStockPaneController controller;

	@Inject
	private WarehouseController warehouseController;

	private final JPanel pane;
	private final JTextField quantityField;
	private final JComboBox<Warehouse> toWarehouseCombo;
	private final JComboBox<Warehouse> fromWarehouseCombo;

	public TransferStockPane() {
		pane = new JPanel();
		pane.setLayout(new MigLayout("", "[][grow]", "[][][][][]"));

		JLabel lblFromWarehouse = new JLabel("From Warehouse");
		pane.add(lblFromWarehouse, "cell 0 0,alignx trailing");

		fromWarehouseCombo = new JComboBox<>();
		pane.add(fromWarehouseCombo, "cell 1 0,growx");

		JLabel lblToWarehouse = new JLabel("To Warehouse");
		pane.add(lblToWarehouse, "cell 0 1,alignx trailing");

		toWarehouseCombo = new JComboBox<>();
		pane.add(toWarehouseCombo, "flowx,cell 1 1,growx");

		JLabel lblQuantity = new JLabel("Quantity");
		pane.add(lblQuantity, "cell 0 2,alignx trailing");

		JButton btnNewWarehouse = new JButton("New Warehouse");
		btnNewWarehouse.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				warehouseController.create();
			}
		});
		pane.add(btnNewWarehouse, "cell 1 1");

		quantityField = new JTextField();
		pane.add(quantityField, "cell 1 2,growx");
		quantityField.setColumns(10);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getDialog().dispose();
			}
		});
		pane.add(btnCancel, "flowx,cell 1 4");

		JButton btnTransfer = new JButton("Transfer");
		btnTransfer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Warehouse fromWarehouse = (Warehouse) fromWarehouseCombo
						.getSelectedItem();
				Warehouse toWarehouse = (Warehouse) toWarehouseCombo
						.getSelectedItem();

				try {
					Long quantity = Long.parseLong(quantityField.getText()
							.trim());

					controller.validate(fromWarehouse, toWarehouse, quantity);

					controller.transfer(fromWarehouse, toWarehouse, quantity);

				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(getPane(),
							"Quantity must be a numeric value");
				}
			}
		});
		pane.add(btnTransfer, "cell 1 4");
	}

	@Override
	public JComponent getPane() {
		return pane;
	}

	@Override
	public void updateWarehouse(List<Warehouse> warehouses) {
		Warehouse prevSelectedFrom = (Warehouse) fromWarehouseCombo
				.getSelectedItem();
		Warehouse prevSelectedTo = (Warehouse) toWarehouseCombo
				.getSelectedItem();

		fromWarehouseCombo.removeAllItems();
		toWarehouseCombo.removeAllItems();

		for (Warehouse warehouse : warehouses) {
			fromWarehouseCombo.addItem(warehouse);
			toWarehouseCombo.addItem(warehouse);

			if (prevSelectedFrom != null)
				if (prevSelectedFrom.compareTo(warehouse) == 0)
					fromWarehouseCombo.setSelectedItem(warehouse);

			if (prevSelectedTo != null)
				if (prevSelectedTo.compareTo(warehouse) == 0)
					toWarehouseCombo.setSelectedItem(warehouse);
		}
	}

}

@@ -810,32 +810,92 @@
 eModelObserver;%0A
+import devopsdistilled.operp.server.data.entity.items.Item;%0A
 import devopsdis
@@ -1363,16 +1363,59 @@
 seCombo;
+%0A%09private final JComboBox%3CItem%3E itemsCombo;
 %0A%0A%09publi
@@ -1519,16 +1519,18 @@
 %5B%5D%5B%5D%5B%5D%5B%5D
+%5B%5D
 %22));%0A%0A%09%09
@@ -1729,32 +1729,198 @@
 l 1 0,growx%22);%0A%0A
+%09%09JLabel lblItem = new JLabel(%22Item%22);%0A%09%09pane.add(lblItem, %22cell 0 1,alignx trailing%22);%0A%0A%09%09itemsCombo = new JComboBox%3C%3E();%0A%09%09pane.add(itemsCombo, %22cell 1 1,growx%22);%0A%0A
 %09%09JLabel lblToWa
@@ -1984,33 +1984,33 @@
 ehouse, %22cell 0 
-1
+2
 ,alignx trailing
@@ -2093,25 +2093,25 @@
 lowx,cell 1 
-1
+2
 ,growx%22);%0A%0A%09
@@ -2180,33 +2180,33 @@
 antity, %22cell 0 
-2
+3
 ,alignx trailing
@@ -2469,17 +2469,17 @@
 %22cell 1 
-1
+2
 %22);%0A%0A%09%09q
@@ -2541,25 +2541,25 @@
 ld, %22cell 1 
-2
+3
 ,growx%22);%0A%09%09
@@ -2818,25 +2818,25 @@
 lowx,cell 1 
-4
+5
 %22);%0A%0A%09%09JButt
@@ -3566,9 +3566,9 @@
 l 1 
-4
+5
 %22);%0A

Add Item Selection Field