package devopsdistilled.operp.client.items.views;

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.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import devopsdistilled.operp.client.abstracts.SubTaskPane;
import devopsdistilled.operp.client.items.controllers.CreateItemPaneController;
import devopsdistilled.operp.client.items.models.observers.ItemModelObserver;
import devopsdistilled.operp.server.data.entity.items.Brand;
import devopsdistilled.operp.server.data.entity.items.Item;
import devopsdistilled.operp.server.data.entity.items.Product;

public class CreateItemPane extends SubTaskPane implements ItemModelObserver {

	@Inject
	private CreateItemPaneController controller;

	private final JPanel pane;
	private final JTextField itemNameField;
	private final JTextField priceField;
	private final JComboBox<Brand> comboBrands;
	private final JComboBox<Product> comboProducts;
	private final JComboBox<Item> comboItems;

	@Override
	public void init() {
		super.init();
	}

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

		JLabel lblProductName = new JLabel("Product Name");
		pane.add(lblProductName, "cell 0 0,alignx trailing");

		comboProducts = new JComboBox<Product>();
		pane.add(comboProducts, "flowx,cell 2 0,growx");

		JLabel lblBrandName = new JLabel("Brand Name");
		pane.add(lblBrandName, "cell 0 1,alignx trailing");

		comboBrands = new JComboBox<Brand>();
		comboItems = new JComboBox<Item>();
		pane.add(comboItems, "flowx,cell 2 1,growx");
		
				JLabel lblItemId = new JLabel("Item Name");
				pane.add(lblItemId, "cell 0 2,alignx trailing");
		
				itemNameField = new JTextField();
				
						pane.add(itemNameField, "cell 2 2,growx");
						itemNameField.setColumns(10);

		JLabel lblPrice = new JLabel("Price");
		pane.add(lblPrice, "cell 0 3,alignx trailing");

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

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getDialog().dispose();
			}
		});
		pane.add(btnCancel, "flowx,cell 2 4");
		JButton btnSave = new JButton("Create");
		btnSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Item item = new Item();
				Brand brand = (Brand) comboBrands.getSelectedItem();
				Product product = (Product) comboProducts.getSelectedItem();
				item.setBrand(brand);
				item.setProduct(product);

			}
		});
		pane.add(btnSave, "cell 2 4");

		JButton btnNewProduct = new JButton("New Product");
		pane.add(btnNewProduct, "cell 2 0");

		JButton btnNewBrand = new JButton("New Brand");
		btnNewBrand.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		pane.add(btnNewBrand, "cell 2 1");

	}

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

	@Override
	public void update(List<Item> entities) {
		for (Item item : entities) {
			comboItems.addItem(item);
		}
	}

}

@@ -581,20 +581,102 @@
 servers.
-Item
+BrandModelObserver;%0Aimport devopsdistilled.operp.client.items.models.observers.Product
 ModelObs
@@ -925,21 +925,46 @@
 plements
- Item
+%0A%09%09ProductModelObserver, Brand
 ModelObs
@@ -1232,51 +1232,8 @@
 cts;
-%0A%09private final JComboBox%3CItem%3E comboItems;
 %0A%0A%09@
@@ -1757,46 +1757,8 @@
 ();%0A
-%09%09comboItems = new JComboBox%3CItem%3E();%0A
 %09%09pa
@@ -1769,20 +1769,21 @@
 dd(combo
-Item
+Brand
 s, %22flow
@@ -1802,21 +1802,17 @@
 rowx%22);%0A
-%09%09%0A%09%09
+%0A
 %09%09JLabel
@@ -1851,18 +1851,16 @@
 me%22);%0A%09%09
-%09
-%09
 pane.add
@@ -1900,21 +1900,17 @@
 ling%22);%0A
-%09%09%0A%09%09
+%0A
 %09%09itemNa
@@ -1941,17 +1941,9 @@
 ();%0A
-%09%09%09%09%0A%09%09%09%09
+%0A
 %09%09pa
@@ -1983,20 +1983,16 @@
 rowx%22);%0A
-%09%09%09%09
 %09%09itemNa
@@ -3266,75 +3266,373 @@
 date
-(List%3CItem%3E entities) %7B%0A%09%09for (Item item : entities) %7B%0A%09%09%09comboItem
+Brands(List%3CBrand%3E brands) %7B%0A%09%09System.out.println(%22updateBrands()%22);%0A%09%09comboBrands.removeAllItems();%0A%09%09for (Brand brand : brands) %7B%0A%09%09%09comboBrands.addItem(brand);%0A%09%09%7D%0A%0A%09%7D%0A%0A%09@Override%0A%09public void updateProducts(List%3CProduct%3E products) %7B%0A%09%09System.out.println(%22updateProducts()%22);%0A%09%09comboProducts.removeAllItems();%0A%09%09for (Product product : products) %7B%0A%09%09%09comboProduct
 s.ad
@@ -3637,20 +3637,23 @@
 addItem(
-item
+product
 );%0A%09%09%7D%0A%09

Implement updateMethods and rearrange UI.