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

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

		ListStore ls = new ListStore(typeof(FileInfo));
		ListStore lsDriver = new ListStore(typeof(String));


		public GestionDriversUI ()
		{
			this.Build ();
	

			lsDriver = new Gtk.ListStore(typeof (String));
			lsDriver.AppendValues("V1  256/0/0/8","0");
			lsDriver.AppendValues("V2 1024/512/16/16","2");

			comboDriver.Model = lsDriver;

			var idUsbCol = new Gtk.TreeViewColumn();
			var idUsbCell = new Gtk.CellRendererText();
			idUsbCol.Title = "Id. USB";
			idUsbCol.PackStart(idUsbCell,true);
			idUsbCol.SetCellDataFunc(idUsbCell, new Gtk.TreeCellDataFunc(	RenderIDUsb) );
			this.listeUsb.AppendColumn(idUsbCol);

			var connStCol = new Gtk.TreeViewColumn();
			var connStCell = new Gtk.CellRendererText();
			connStCol.Title = "Etat";
			connStCol.PackStart(connStCell,true);
			connStCol.SetCellDataFunc(connStCell, new Gtk.TreeCellDataFunc(	RenderSt) );
			this.listeUsb.AppendColumn(connStCol);
			connStCol.Alignment = 1;


			/*
			var nameDriverCol = new Gtk.TreeViewColumn();
			var nameDriverCell = new Gtk.CellRendererCombo();
			nameDriverCol.Title = "Etat";
			nameDriverCol.PackStart(nameDriverCell,false);
			nameDriverCol.AddAttribute (nameDriverCell, "text", 0);
			nameDriverCell.Editable =true;
			nameDriverCell.HasEntry=false;
			nameDriverCell.Model = lsDriver;
			nameDriverCell.TextColumn = 0;

			this.listeUsb.AppendColumn(nameDriverCol);*/

			ls = new Gtk.ListStore(typeof (FileInfo));
			this.listeUsb.Model = ls;
			UpdateListeUsb();
		}



		void RenderIDUsb (Gtk.TreeViewColumn tree_column, Gtk.CellRenderer cell, Gtk.TreeModel tree_model, Gtk.TreeIter iter)
		{
			FileInfo fi =  tree_model.GetValue (iter, 0) as FileInfo;
			(cell as Gtk.CellRendererText).Text = fi.Name.Substring(0,35);
		}

		void RenderSt (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
		{
			FileInfo fi = tree_model.GetValue (iter, 0) as FileInfo;
			DriverDMX drv = Conduite.Courante.GetDriverByID (fi.Name);
			if (drv == null) {
				(cell as Gtk.CellRendererText).Text = "Déconnecté";
				return;
			}
			(cell as Gtk.CellRendererText).Text = drv.GetType().ToString();
		}

		protected void UpdateListeUsb ()
		{
			ls.Clear();
			var dirInfo = new DirectoryInfo ("/dev/serial/by-id/");
			if (dirInfo.Exists) {
				foreach(FileInfo fi in dirInfo.GetFiles()) 
					ls.AppendValues(fi);
			}

		}

		protected void OnButtonOkClicked (object sender, EventArgs e)
		{
			Destroy();
		}		

		protected void OnListeUsbCursorChanged (object sender, EventArgs e)
		{
			TreeViewColumn col;
			TreePath path;
			TreeIter iter;
			listeUsb.GetCursor (out path, out col);
			if (!listeUsb.Model.GetIter (out iter, path))
				return;

			FileInfo fi = listeUsb.Model.GetValue (iter, 0) as FileInfo;
			if (fi == null)
				return;

			DriverDMX drv = Conduite.Courante.GetDriverByID (fi.Name);
			if (drv == null) {
				btnConnect.Visible = btnConnect.Sensitive = true;
				btnDisconnect.Visible = btnDisconnect.Sensitive = false;

				comboDriver.Sensitive = true;
				comboDriver.Active=-1;
				return;
			}

			AfficheDriverUI(drv);
			
		}
		protected void OnBtnConnectClicked (object sender, EventArgs e)
		{
			// Instancie
			TreeViewColumn col;
			TreePath path;
			TreeIter iter;
			listeUsb.GetCursor (out path, out col);
			if (!listeUsb.Model.GetIter (out iter, path))
				return;

			FileInfo fi = listeUsb.Model.GetValue (iter, 0) as FileInfo;
			if (fi == null)
				return;

			DriverDMX drv=null;

			switch (comboDriver.Active) {
			case 0:
				drv = new DriverBoitierV1(fi.FullName, fi.Name);
				Conduite.Courante.Drivers.Add(drv);
				break;
			case 1:
				drv = new DriverBoitierV2(fi.FullName, fi.Name);
				Conduite.Courante.Drivers.Add(drv);
				break;
			default:
				return;
			}

			AfficheDriverUI(drv);

		}

		void AfficheDriverUI(DriverDMX drv){
			btnConnect.Visible = false;
			comboDriver.Sensitive = false;

			btnDisconnect.Visible = btnDisconnect.Sensitive = true;

			frmDrvChild.Child = drv.GetUI();
			frmDrvChild.ShowAll();

		}
	}
}

