/* 

   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 DriverBoitierV2UI : Gtk.Bin
	{
		DriverBoitierV2 drv;
		public DriverBoitierV2UI (DriverBoitierV2 _drv)
		{
			drv = _drv;
			this.Build ();
			ConstruitCBUnivers();
		}

	ListStore lsCbUnivers1 = new ListStore(typeof(UniversDMX));	
	ListStore lsCbUnivers2 = new ListStore(typeof(UniversDMX));
	void ConstruitCBUnivers ()
		{
			caseBrk.Text = drv.Break.ToString ();
			caseMab.Text = drv.Mab.ToString ();

			chkMerge1.Active = drv.Flag_merge1 == 1;
			chkMerge2.Active = drv.Flag_merge2 == 1;

			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;

			cbUnivers2.Model = lsCbUnivers2;
			var cellCbUnivers2 = new CellRendererText ();
			cbUnivers2.PackStart (cellCbUnivers2, false);
			cbUnivers2.SetCellDataFunc (cellCbUnivers2, new CellLayoutDataFunc (RenderUniversName2));

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

			//TreeIter iter;
			lsCbUnivers2.GetIterFirst(out iter);
			cbUnivers2.SetActiveIter(iter);
			cbUnivers2.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;
			}
			if (cbUnivers2.GetActiveIter (out iter)) {
				drv.patch2 = lsCbUnivers2.GetValue (iter, 0) as UniversDMX;
			}

			int a, b;
			if (!int.TryParse (caseBrk.Text, out a))
				return;
			if (!int.TryParse (caseMab.Text, out b))
				return;
			if (a < 92) {
				a = 92;
			}
			if (b < 12) {
				b = 12;
			}
			drv.SetBreak(a,b,
			             (byte)(chkMerge1.Active?1:0),
			             (byte)(chkMerge2.Active?1:0));

		}		

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



	}
}

