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 com.veggie.src.java.controllers.Controller;

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 activate() {
	  return addItemForm;
	}

	public Notification submitForm(Form form) {
		addItemForm = form;
		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(Notification notif) {
		if (notif.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);
		}
	}

}

@@ -543,17 +543,64 @@
 Manager 
-m
+titleManager;%0A%09private ItemDatabaseManager itemM
 anager;%0A
@@ -922,17 +922,22 @@
 lt();%0A%09%09
-m
+titleM
 anager =
@@ -1012,16 +1012,106 @@
 ager();%0A
+%09%09itemManager = AbstractDatabaseManagerFactory.getInstance().createItemDatabaseManager();%0A
 %09%7D%0A%0A%0A%09pu
@@ -1288,25 +1288,30 @@
 tleStatus = 
-m
+titleM
 anager.check
@@ -2057,17 +2057,22 @@
 ));%0A%09%09%09%09
-m
+titleM
 anager.a
@@ -2197,17 +2197,22 @@
 sults = 
-m
+titleM
 anager.g
@@ -2376,16 +2376,49 @@
 title);%0A
+%09%09%09itemManager.addItem(newItem);%0A
 %09%09%7D%0A%09%7D%0A%0A

Fix AddItemController