package com.veggie.src.java;

import com.veggie.src.java.form.*;
import com.veggie.src.java.notification.*;
import com.veggie.src.java.database.*;
import com.veggie.src.java.core.*;
import com.veggie.src.java.core.media.MediaItem;
import com.veggie.src.java.core.media.MediaTitle;

import java.util.List;
import java.util.Set;

public class AddItemController implements Controller {

	//Instance Variables
	private Notification notification;
	private Form addItemForm;
	private TitleDatabaseManager manager;
	private int titleStatus;
	
	//Methods
	public AddItemController() {
		AbstractFormBuilder builder = AbstractFormBuilderFactory.getInstance().createFormBuilder();
		builder.addField("ISBN");
		builder.addField("Title");
		builder.addField("Author");
		builder.addField("Description");
		addItemForm = builder.getResult();
		manager = AbstractDatabaseManagerFactory.getInstance().createTitleDatabaseManager();
	}
	
	public Form clickAddItemButton() {
	  return addItemForm;
	}
	
	public Notification submitForm() {
		List<String> formData = addItemForm.getData();
		titleStatus = manager.checkTitle(formData.get(0), addItemForm.getFieldNames(), formData);
		if (titleStatus == MediaTitle.INVALID_TITLE) {
			notification = AbstractNotificationFactory.getInstance().createErrorNotification("Invalid Title: Media title exists, but fields do not match");
		} else {
			notification = AbstractNotificationFactory.getInstance().createConfirmNotification("Confirm item addition?");
		}
		return notification;
	}
	
	public void respondToNotification() {
		if (notification instanceof AbstractErrorNotification) {
			if (notification.ok()) {
				List<String> formData = addItemForm.getData();
				MediaTitle title = new MediaTitle(0, "", "", "", "");
				if (titleStatus == MediaTitle.NEW_TITLE) {
					title = new MediaTitle(0, formData.get(0), formData.get(1), formData.get(2), formData.get(3));
					manager.addTitle(formData.get(0), title);
				} else if (titleStatus == MediaTitle.EXISTING_TITLE) {
					Set<MediaTitle> titleResults = manager.getMatches(addItemForm.getFieldNames(), formData);
					for (MediaTitle t : titleResults) {
						title = t;
					}
				}
				MediaItem newItem = new MediaItem(0, title);
			}
		}
	}

}
@@ -273,16 +273,67 @@
 iaTitle;
+%0Aimport com.veggie.src.java.controllers.Controller;
 %0A%0Aimport
@@ -983,26 +983,16 @@
 orm 
-clickAddItemButton
+activate
 () %7B
@@ -1052,19 +1052,50 @@
 mitForm(
-) %7B
+Form form) %7B%0A%09%09addItemForm = form;
 %0A%09%09List%3C
@@ -1619,19 +1619,9 @@
 ion(
-) %7B%0A%09%09if (n
+N
 otif
@@ -1632,51 +1632,19 @@
 ion 
-instanceof AbstractErrorNotification
+notif
 ) %7B%0A%09%09
-%09
 if (
@@ -1648,23 +1648,16 @@
 f (notif
-ication
 .ok()) %7B
@@ -1660,17 +1660,16 @@
 )) %7B%0A%09%09%09
-%09
 List%3CStr
@@ -1707,17 +1707,16 @@
 Data();%0A
-%09
 %09%09%09Media
@@ -1764,17 +1764,16 @@
 %22, %22%22);%0A
-%09
 %09%09%09if (t
@@ -1810,25 +1810,24 @@
 ITLE) %7B%0A%09%09%09%09
-%09
 title = new 
@@ -1909,17 +1909,16 @@
 et(3));%0A
-%09
 %09%09%09%09mana
@@ -1958,17 +1958,16 @@
 le);%0A%09%09%09
-%09
 %7D else i
@@ -2009,25 +2009,24 @@
 NG_TITLE) %7B%0A
-%09
 %09%09%09%09Set%3CMedi
@@ -2111,17 +2111,16 @@
 a);%0A%09%09%09%09
-%09
 for (Med
@@ -2147,17 +2147,16 @@
 ults) %7B%0A
-%09
 %09%09%09%09%09tit
@@ -2171,18 +2171,15 @@
 %09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%7D%0A
-%09
 %09%09%09M
@@ -2222,21 +2222,16 @@
 title);%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Fix AddItemController