using Gtk;
using System;
using System.IO;


namespace FSpot {
	public class FindBox : HBox {
	
		private TagSelectionWidget selector;
		private PhotoQuery query;
		
		
		// Widgetry.		
		private Label find_label;
		private Entry find_entry;
		private HBox warning_box;
		public HBox WarningBox {
			get {
				return warning_box;
			}
		}
		
		private Label result_label;		
		private ComboBox option_combo;
			
		private enum Option : byte {
			Tags
			// more to come?
		};
		
		
		private void SetupWidgets()
		{
			find_label = new Label (Mono.Posix.Catalog.GetString ("Find:"));	
			base.PackStart (find_label, false, false, 0);			
			
			find_entry = new Entry ();
			base.PackStart (find_entry, false, true, 0);			
						
			find_entry.Completion = new EntryCompletion ();
			find_entry.Completion.Model = SetupTagCompletion ();
			find_entry.Completion.TextColumn = 0;			
			
			warning_box = new HBox ();			
			
			Image warning_image = new Gtk.Image ("gtk-dialog-warning", Gtk.IconSize.Button);			
			warning_box.PackStart (warning_image, false, true, 0);				
			
			result_label = new Label ("");
			warning_box.PackStart (result_label, false, true, 0);	
			warning_box.ReorderChild (result_label, 2);			
			
			warning_box.ShowAll ();
			warning_box.Visible = false;
			
			base.PackStart (warning_box);			
			
			String[] categories = new String[1] {Mono.Posix.Catalog.GetString ("Tags")};
			
			option_combo = new ComboBox (categories);			
			base.PackEnd (option_combo, false, false, 0);			
			option_combo.Active = 0;
						
			find_entry.Activated += HandleActivated;
			
			selector.Store.TagCreated += HandleTagsChanged;
			selector.Store.TagChanged += HandleTagsChanged;
			selector.Store.TagDeleted += HandleTagsChanged;
			
		}


		private void HandleTagsChanged(Tag t)
		{	
			if ((Option) option_combo.Active == Option.Tags)
				find_entry.Completion.Model = SetupTagCompletion ();
		}
		
		private void HandleActivated(object sender, EventArgs e)
		{
			switch((Option) option_combo.Active) {
				case Option.Tags:
					SearchTags();							
					break;
			}			
		}		
		
		
		private void SearchTags()
		{
			if (find_entry.Text == "") {
				selector.TagSelection = new Tag[] { };
				warning_box.Visible = false;
				return;
			}
			
			Tag[] tags = selector.Store.Query (find_entry.Text);
			
			if(tags.Length == 0) {
				result_label.Text = Mono.Posix.Catalog.GetString ("No Tags Found");				
			}
			else {
				selector.TagSelection = selector.Store.Query (find_entry.Text);					
			}
			
			warning_box.Visible = (tags.Length < 1);			
		}
		
		private void SearchFilenames()
		{
			if (find_entry.Text == "")
				Console.WriteLine("Reload");
		}
				
		
		private TreeModel SetupTagCompletion()
		{
			ListStore taglist = new ListStore(typeof (string), typeof(uint));
						
			foreach (Tag t in selector.Store.Query (null) )
			{
				taglist.AppendValues (t.Name, t.Id);
			}
			
			return taglist;
		}
		
		public new void Show()
		{
			base.Show ();			
			find_entry.GrabFocus ();			
		}		
		
		//Constructor
		public FindBox(PhotoQuery query, TagSelectionWidget selector) : base (false,0)
		{
			this.query = query;
			this.selector = selector;
			SetupWidgets ();							
		}
		
	}
}
