package net.sf.memoranda.tag;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JComboBox;

import net.sf.memoranda.ui.App;
import net.sf.memoranda.util.Local;
import net.sf.memoranda.util.Util;
import net.sf.memoranda.ProjectManager;
import net.sf.memoranda.Project;
import net.sf.memoranda.ProjectImpl;
 
/**
 * 
 * @author Antonio Roca
 * 
 * TagSearchController provides the control logic for the tag search view. It extends 
 * TagSearchView so that it may make use of its graphical components. The TagSearchView
 * JComboBox is populated with a list of all tags already present in any project through the 
 * Tag class member.
 * 
 * TagSearchController provides four operations: 1) add tag; 2) remove tag; 3) search; 4) cancel.
 * 1) Adding a tag adds any tags specified in the manual tag input field or the tag JComboBox, if
 * those tags are not already selected for searching
 * 2) Removing a tag removes any tags specified in the manual tag input field or the JComboBox
 * 3) Searching parses the tags specified and compares them to tags from current project Node structures
 * 4) Cancelling closes the window
 */
public class TagSearchController extends TagSearchView implements ActionListener {

	public static Action searchByTagAction =
		new AbstractAction(
			Local.getString("Search for projects by tag")) {

		public void actionPerformed(ActionEvent e) {
			searchByTag_actionPerformed(e);
		}
	};	
	
	private Tag tag;
	
	public TagSearchController(Frame frame, String title) {
		super(frame, title);
		
		// set actionlisteners
		searchBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
	
		if (source == searchBtn) {
			search();
		} else if (source == cancelBtn) {
			cancel();
		}
	}
	
	public static void searchByTag_actionPerformed(ActionEvent e) {
		TagSearchController sc = new TagSearchController(App.getFrame(), Local.getString("Search By Tag"));
    	
    	Dimension frmSize = App.getFrame().getSize();
        Point loc = App.getFrame().getLocation();
        sc.setLocation((frmSize.width) / 3 + loc.x, (frmSize.height) / 3 + loc.y);
        sc.setVisible(true);
        if (sc.CANCELLED)
            return;
	}
	
	private void search() {
    	try {
    		DefaultTableModel model = (DefaultTableModel)results.getModel();
			Vector<Project> projects = new Vector<Project>();
			String selectedItem = tagSelectBox.getSelectedItem().toString();
	    	if(!selectedItem.equals("") && !selectedItem.equals(" ")){
	    		projects=ProjectImpl.findProjectByAllTagName(tagSelectBox.getSelectedItem().toString());
	    	}
	    	else if (!manualTagTextField.getText().toString().equals("")){
	    		projects=ProjectImpl.findProjectByAllTagName(manualTagTextField.getText().toString());
	    	}
	 
	    	if(projects.size()==0){
	    		JOptionPane.showMessageDialog(null, Local.getString("No project found for this tag"));
	    	}
	    	else {
	    		model.setRowCount(0);
		    	for(int i=0;i < projects.size();i++){
		    		model.addRow(new Object[]{projects.get(i).getTitle(), projects.get(i).getTagsAsString()});
		    	}
	    	}
    	} catch (NullPointerException e) {
    		Util.error(e);
    		JOptionPane.showMessageDialog(null,  Local.getString("No project found for this tag"));
    	}
		
	}
	
	private void cancel() {
		this.dispose();
	}

}