using System;
using System.Collections.Generic;

namespace DMX2
{
	public partial class SelSeqCircuits : Gtk.Dialog
	{

		Gtk.ListStore lsAll=null;
		Gtk.ListStore lsSel=null;

		List<Circuit> cursel;

		public List<Circuit> GetResultList ()
		{
			return cursel;
		}

		public SelSeqCircuits (IEnumerable<Circuit> selection , Gtk.Window parent) : base ("Selection des  Circuits",parent,Gtk.DialogFlags.Modal)
		{
			this.Build ();

			cursel = new List<Circuit>(selection);

			var allnameCol = new Gtk.TreeViewColumn();
			var allNameCell = new Gtk.CellRendererText();
			allnameCol.Title = "Circuits";
			allnameCol.PackStart(allNameCell,true);
			allnameCol.SetCellDataFunc(allNameCell, new Gtk.TreeCellDataFunc(	RenderCircuitName) );
			this.listeToutCircuits.AppendColumn(allnameCol);

			lsAll = new Gtk.ListStore(typeof (Circuit));
			this.listeToutCircuits.Model = lsAll;
			foreach(var c in Conduite.Courante.Circuits)
				lsAll.AppendValues(c);
			listeToutCircuits.Selection.Mode = Gtk.SelectionMode.Multiple;

			var selNameCol = new Gtk.TreeViewColumn();
			var selNameCell = new Gtk.CellRendererText();
			selNameCol.Title = "Circuits du Sequenceur";
			selNameCol.PackStart(selNameCell,true);
			selNameCol.SetCellDataFunc(selNameCell, new Gtk.TreeCellDataFunc(	RenderCircuitName) );
			this.listeSelCircuits.AppendColumn(selNameCol);

			lsSel = new Gtk.ListStore(typeof (Circuit));
			this.listeSelCircuits.Model = lsSel;
			foreach(var c in cursel)
				lsSel.AppendValues(c);
			listeSelCircuits.Selection.Mode = Gtk.SelectionMode.Multiple;
		}

		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;
		}		

		void UpdateCurSel ()
		{
			lsSel.Clear ();
			foreach(var c in cursel)
				lsSel.AppendValues(c);
		}		

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

		protected void OnAjtButClicked (object sender, EventArgs e)
		{
			foreach (var c in GetSelection(listeToutCircuits,lsAll)) {
				if(!cursel.Contains(c))
					cursel.Add(c);

			}
			UpdateCurSel();
		}		

		protected void OnSupBtClicked (object sender, EventArgs e)
		{
			foreach (var c in GetSelection(listeSelCircuits,lsSel)) {
				cursel.Remove(c);
			}
			UpdateCurSel();
		}		


		protected void OnSupToutButClicked (object sender, EventArgs e)
		{
			cursel.Clear();
			UpdateCurSel();
		}		

		protected void OnMvHautBtClicked (object sender, EventArgs e)
		{

			List<Circuit> selrow = GetSelection(listeSelCircuits,lsSel);
			foreach (var c in selrow) {
				int idx = cursel.IndexOf(c);
				if(idx>0)
				{
					if(!selrow.Contains(cursel[idx-1]))
					{
						cursel.Remove(c);
						cursel.Insert(idx-1,c);
					}
				}
			}
			lsSel.Clear();
			foreach (var item in cursel) {
				var iter = lsSel.AppendValues (item);
				if(selrow.Contains(item))
				   	listeSelCircuits.Selection.SelectIter(iter);
			}
		}		

		protected void OnMvBasBtClicked (object sender, EventArgs e)
		{
			List<Circuit> selrow = GetSelection(listeSelCircuits,lsSel);
			selrow.Reverse();
			foreach (var c in selrow) {
				int idx = cursel.IndexOf(c);
				if(idx<cursel.Count)
				{
					if(!selrow.Contains(cursel[idx+1]))
					{
						cursel.Remove(c);
						cursel.Insert(idx+1,c);
					}
				}
			}

			lsSel.Clear();
			foreach (var item in cursel) {
				var iter = lsSel.AppendValues (item);
				if(selrow.Contains(item))
				   	listeSelCircuits.Selection.SelectIter(iter);
			}
		}

	}
}

