/* 

   Copyright (C) Arnaud Houdelette		2012-2014
   Copyright (C) Emmanuel Langlois		2012-2014

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

namespace DMX2
{
	[System.ComponentModel.ToolboxItem(true)]
	public partial class DriverBoitierV3UI : Gtk.Bin
	{
		DriverBoitierV3 drv;
		public DriverBoitierV3UI (DriverBoitierV3 _drv)
		{
			drv = _drv;
			this.Build ();
			ConstruitCBUnivers();
		}

	ListStore lsCbUnivers1 = new ListStore(typeof(UniversDMX));	

	void ConstruitCBUnivers ()
		{
			caseBrk.Text = drv.Break.ToString ();
			caseMab.Text = drv.Mab.ToString ();
			caseCircuits.Text = drv.NbCircuits.ToString();
			caseDMXInt.Text = drv.DmxInterval.ToString();
			caseUSBRef.Text = drv.UsbRefresh.ToString();
			chkMerge1.Active = drv.Merge;
			chkSync.Active = drv.Sync;

			//caseDMXInt.IsEditable = drv.Sync;

			cbUnivers1.Model = lsCbUnivers1;
			var cellCbUnivers1 = new CellRendererText ();
			cbUnivers1.PackStart (cellCbUnivers1, false);
			cbUnivers1.SetCellDataFunc (cellCbUnivers1, new CellLayoutDataFunc (RenderUniversName1));

			int indx = 0;
			int i = 0;
			foreach (UniversDMX u in Conduite.Courante.Patches) {
				lsCbUnivers1.AppendValues (u);
				if (u==drv.patch1) indx=i;
				i++;
			}

			TreeIter iter;
			lsCbUnivers1.GetIterFirst (out iter);
			cbUnivers1.SetActiveIter (iter);
			cbUnivers1.Active=indx;


		}

		void RenderUniversName1 (CellLayout cell_layout, CellRenderer cell, TreeModel tree_model, TreeIter iter)
		{
			UniversDMX univers =  tree_model.GetValue (iter, 0) as UniversDMX;
			if(univers != null)
			(cell as Gtk.CellRendererText).Text = univers.Nom;
		}

		void RenderUniversName2 (CellLayout cell_layout, CellRenderer cell, TreeModel tree_model, TreeIter iter)
		{
			UniversDMX univers =  tree_model.GetValue (iter, 0) as UniversDMX;
			if(univers != null)
			(cell as Gtk.CellRendererText).Text = univers.Nom;
		}		

		protected void OnButtonValider (object sender, EventArgs e)
		{
			TreeIter iter;
			if (cbUnivers1.GetActiveIter (out iter)) {
				drv.patch1 = lsCbUnivers1.GetValue (iter, 0) as UniversDMX;
			}

			int a, b, c,d, f;
			if (!int.TryParse (caseBrk.Text, out a))
				return;
			if (!int.TryParse (caseMab.Text, out b))
				return;
			if (!int.TryParse (caseCircuits.Text, out c))
				return;

			if (!int.TryParse (caseUSBRef.Text, out d))
				return;

			if (!int.TryParse (caseDMXInt.Text, out f))
				return;

			if (a < 92) a = 92;
			if (b < 12) b = 12;
			if (c < 8 || c > 512 ) c = 512;
			if (d < 5 ) d = 22;
			if (f < 5 || f > 255 ) f = 22;

			drv.Break = a;
			drv.Mab = b;
			drv.NbCircuits = c;
			drv.UsbRefresh = d;
			drv.DmxInterval = (byte)f;
			drv.Merge = chkMerge1.Active;
			drv.Sync = chkSync.Active;

		}		

		protected void OnBtnInitClicked (object sender, EventArgs e)
		{
			drv.ReInit();
		}

		protected void OnChkSyncToggled (object sender, EventArgs e)
		{
			//SyncCheck();
		}		

		protected void OnCaseUSBRefChanged (object sender, EventArgs e)
		{
			//SyncCheck();
		}


	}
}

