/**
 * EventController.java
 *
 *
 * Caitlin
 * 100735219
 */

import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class EventController{
	
	private RecipeBrowser browser;
    private RecipeParser parser;
    private Recipe currentRecipe;
    private String currentTextView;
    private final String ingredientsButton = "ingredients";
    private final String directionsButton = "directions";
    private final String descriptionButton = "description";
	final private String xmlStartTag = "<xml>";
    final private String xmlEndTag = "</xml>";
	
	public EventController(RecipeBrowser aBrowser) {
		browser = aBrowser;
		currentRecipe = null;
		currentTextView = null;
	}
	
    public void openButtonPressed(){
    	String fileName = browser.getFilePanel().getFileText();
    	try{
    		parser = new RecipeParser(fileName);
    		browser.getListPanel().setNamesList(parser.getRecipes().getNames());
    		browser.getListPanel().setSpiceList(parser.getRecipes().getSpices());
    	}
    	catch(FileOpeningException e){
    		 JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    	}
    	catch (ArrayIndexOutOfBoundsException e){
    		//Doesn't affect program
    	}
    }
    
    public void newButtonPressed(){
    	if(parser!=null){
    		currentRecipe = new Recipe("", "", "", "", "", "", "", "", "");
    		displayInfo();
    		browser.getListPanel().clearNamesListSelection();
    	}
    }
    
    public void saveButtonPressed(){
    	if(parser!=null){
    		Recipe newRecipe;
    		
    		String name = browser.getInfoPanel().getRecipeName();
    		String contributor = browser.getInfoPanel().getContributor();
    		String spice = browser.getInfoPanel().getSpices();
    		String rating = browser.getInfoPanel().getRating();
    		
    		String ingredients, directions, description;
    		
    		if(currentTextView == ingredientsButton){
    			ingredients = browser.getTextPanel().getAreaText();
    			directions = currentRecipe.getDirections();
    			description = currentRecipe.getDescription();
    		}
    		else if(currentTextView == directionsButton){
    			ingredients = currentRecipe.getIngredients();
    			directions = browser.getTextPanel().getAreaText();
    			description = currentRecipe.getDescription();
    		}
    		else if(currentTextView == descriptionButton){
    			ingredients = currentRecipe.getIngredients();
    			directions = currentRecipe.getDirections();
    			description = browser.getTextPanel().getAreaText();
    		}
    		else{
    			try{
    				ingredients = currentRecipe.getIngredients();
    				directions = currentRecipe.getDirections();
    				description = currentRecipe.getDescription();
    			}
    			catch(java.lang.NullPointerException e){
    				ingredients = "";
    				directions = "";
    				description = "";
    			}
    		}
    		
    		newRecipe = new Recipe(name, rating, contributor, spice, "", "", description, ingredients, directions);
    		
    		parser.getRecipes().add(newRecipe);
    	}
    }
    
    public void findButtonPressed(){
    	if(parser!=null){
    		String contributor = browser.getInfoPanel().getContributor();
    		browser.getListPanel().setNamesList(parser.getRecipes().findContributor(contributor));
    		if(parser.getRecipes().findContributor(contributor).length == 0){
    			JOptionPane.showMessageDialog(null, "Sorry, that contributor doesn't exist", "Error", JOptionPane.ERROR_MESSAGE);
    		}
    	}
    }
    
    public void namesListSelected(Object obj){
    	if(parser!=null){
    		try{
    			currentRecipe = (Recipe)obj;
    		}
    		catch(java.lang.ClassCastException e){
    			currentRecipe = new Recipe("", "", "", "", "", "", "", "", "");
    			try{
    				browser.getListPanel().setNamesList(parser.getRecipes().getNames());
    				browser.getListPanel().clearSpiceListSelection();
    			}
    			catch(ArrayIndexOutOfBoundsException ex){
    				//Do nothing, it shouldn't affect program
    			}
    		}
    		displayInfo();
    	}
    }
    
    public void spiceListSelected(Object obj){
    	if(parser!=null){
    		String spice = (String)obj;
    		browser.getListPanel().setNamesList(parser.getRecipes().recipesBySpice(spice));
    	}
    }
    
    public void ingredientsButtonPressed(){
    	if(parser!=null){
    		if(currentTextView == directionsButton){
    			currentRecipe.setDirections(browser.getTextPanel().getAreaText());
    		}
    		else if (currentTextView == descriptionButton){
    			currentRecipe.setDescription(browser.getTextPanel().getAreaText());
    		}
    		browser.getTextPanel().setAreaText(currentRecipe.getIngredients());
    		currentTextView = ingredientsButton;
    	}
    }
    
    public void directionsButtonPressed(){
    	if(parser!=null){
    		if(currentTextView == ingredientsButton){
    			currentRecipe.setIngredients(browser.getTextPanel().getAreaText());
    		}
    		else if (currentTextView == descriptionButton){
    			currentRecipe.setDescription(browser.getTextPanel().getAreaText());
    		}
    		browser.getTextPanel().setAreaText(currentRecipe.getDirections());
    		currentTextView = directionsButton;
    	}
    }
    
    public void descriptionButtonPressed(){
    	if(parser!=null){
    		if(currentTextView == ingredientsButton){
    			currentRecipe.setIngredients(browser.getTextPanel().getAreaText());
    		}
    		else if (currentTextView == directionsButton){
    			currentRecipe.setDirections(browser.getTextPanel().getAreaText());
    		}
    		browser.getTextPanel().setAreaText(currentRecipe.getDescription());
    		currentTextView = descriptionButton;
    	}
    }
    
    private void displayInfo(){
    	if(parser!=null){
    		browser.getInfoPanel().setRecipeName(currentRecipe.getName());
    		browser.getInfoPanel().setContributor(currentRecipe.getContributor());
    		browser.getInfoPanel().setSpices(currentRecipe.getSpice());
    		browser.getInfoPanel().setRating(currentRecipe.getRating());
    		if(browser.getTextPanel().isIngredientsSelected()){
    			browser.getTextPanel().setAreaText(currentRecipe.getIngredients());
    		}
    		else if(browser.getTextPanel().isDirectionsSelected()){
    			browser.getTextPanel().setAreaText(currentRecipe.getDirections());
    		}
    		else if(browser.getTextPanel().isDescriptionSelected()){
    			browser.getTextPanel().setAreaText(currentRecipe.getDescription());
    		}
    	}
    }
    
    public void saveOnExit(){
    	String fileName = browser.getFilePanel().getFileText();
    	if(parser!=null){
    		try{
    			PrintWriter xmlResultFile = new PrintWriter(new FileWriter(fileName));
    			xmlResultFile.println(xmlStartTag);
			
				for(Recipe aRecipe : parser.getRecipes().getRecipeArrayList())
					aRecipe.printXMLOn("   ", xmlResultFile); 
			
				xmlResultFile.println(xmlEndTag);
			
				xmlResultFile.close();
    		}
    		catch(IOException e){
    		}
    	}
    	
    }
    
}