using System;
using System.Collections.Generic;
using Gtk;

namespace DMX2
{
	public partial class GestionCircuits : Gtk.Dialog
	{
		Gtk.ListStore ls;
		public GestionCircuits (Gtk.Window parent) : base ("Circuits",parent,Gtk.DialogFlags.Modal)
		{

			this.Build ();

			var idCol = new Gtk.TreeViewColumn();
			var idCell = new Gtk.CellRendererText();
			idCol.Title = "ID";
			idCol.PackStart(idCell,true);
			idCol.SetCellDataFunc(idCell, new Gtk.TreeCellDataFunc(	RenderCircuitID) );
			this.listeCircuits.AppendColumn(idCol);


			var nameCol = new Gtk.TreeViewColumn();
			var nameCell = new Gtk.CellRendererText();
			nameCol.Title = "Circuit";
			nameCol.PackStart(nameCell,true);
			nameCol.SetCellDataFunc(nameCell, new Gtk.TreeCellDataFunc(	RenderCircuitName) );
			nameCell.Editable =true;
			nameCell.Edited	+= OnNameCellEdited;
			this.listeCircuits.AppendColumn(nameCol);

			var snameCol = new Gtk.TreeViewColumn();
			var snameCell = new Gtk.CellRendererText();
			snameCol.Title = "Nom Court";
			snameCol.PackStart(snameCell,true);
			snameCol.SetCellDataFunc(snameCell, new Gtk.TreeCellDataFunc(RenderCircuitShortName) );
			snameCell.Editable =true;
			snameCell.Edited += OnShortNameCellEdited;
			this.listeCircuits.AppendColumn(snameCol);


			ls = new Gtk.ListStore(typeof (Circuit));
			this.listeCircuits.Model = ls;
			UpdateListeCircuits();
			listeCircuits.Selection.Mode = Gtk.SelectionMode.Multiple;
		}

		void OnNameCellEdited (object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			ls.GetIter (out iter, new Gtk.TreePath (args.Path));
			Circuit c = ls.GetValue(iter,0) as Circuit;
			c.Name = args.NewText;
			if(c.Name.Length <= Circuit.SNLen) 
				c.ShortName = c.Name;
		}

		void OnShortNameCellEdited (object o, EditedArgs args)
		{
			Gtk.TreeIter iter;
			ls.GetIter (out iter, new Gtk.TreePath (args.Path));
			Circuit c = ls.GetValue(iter,0) as Circuit;
			c.ShortName = args.NewText;
		}

		void RenderCircuitID (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
		{
			Circuit c =  tree_model.GetValue (iter, 0) as Circuit;
			(cell as Gtk.CellRendererText).Text = c.ID.ToString();
		}
		void RenderCircuitShortName (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
		{
			Circuit c =  tree_model.GetValue (iter, 0) as Circuit;
			(cell as Gtk.CellRendererText).Text = c.ShortName;
		}

	    void RenderCircuitName(Gtk.TreeViewColumn tree_column, Gtk.CellRenderer cell, Gtk.TreeModel tree_model, Gtk.TreeIter iter) {
			Circuit c =  tree_model.GetValue (iter, 0) as Circuit;
			(cell as Gtk.CellRendererText).Text = c.Name;
		}		

		protected void UpdateListeCircuits ()
		{
			ls.Clear();
			foreach(var c in Conduite.Courante.Circuits)
				ls.AppendValues(c);
		}

		protected void OnAddActionActivated (object sender, EventArgs e)
		{

			var dlg = new Gtk.Dialog ("Test", this, Gtk.DialogFlags.DestroyWithParent);
			dlg.VBox.Add (new Gtk.Label ("Nombre de circuits :"));
			var etry = new Gtk.Entry ("1");
			etry.ActivatesDefault=true;
			dlg.VBox.Add (etry);
			dlg.AddButton (Gtk.Stock.Close, Gtk.ResponseType.Close ).GrabDefault();
			dlg.VBox.ShowAll ();
			dlg.Run ();
			int nb;
			if (int.TryParse (etry.Text, out nb) && nb > 0) {
				for (int i=0; i<nb; i++)
					Conduite.Courante.NouveauCircuit ();
				UpdateListeCircuits ();
			} else {
				var md = new Gtk.MessageDialog(this, Gtk.DialogFlags.Modal,Gtk.MessageType.Error, Gtk.ButtonsType.Ok,"Veuillez saisir un nombre positif");
				md.Run(); md.Destroy();
			}
			dlg.Destroy ();
		}
//		protected void OnRemoveActionActivated (object sender, EventArgs e)
//		{
//			Conduite.Courante.SupprimeCircuits (GetSelection());
//			UpdateListeCircuits();
//		}		

		protected List<Circuit> GetSelection()
		{
			Gtk.TreeIter iter; List<Circuit> selection = new List<Circuit>();
			foreach(var row in listeCircuits.Selection.GetSelectedRows())
			{
				ls.GetIter (out iter, row);
				selection.Add( ls.GetValue(iter,0) as Circuit);
			} 
			return selection;
		}

		protected void OnResp (object o, Gtk.ResponseArgs args)
		{
			Hide ();
			Destroy();
		}
	}
}
