using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;

namespace DMX2
{
	public class UniversDMX
	{

		static int nb=1;

		public UniversDMX ()
							
		{
			Nom = "Univers DMX n°" + nb++.ToString ();

			for (int i = 0; i<_dimmers.Length; i++) 
			{
				_dimmers[i].fonctionTransfert = FTransfer.lineaire;
				_dimmers[i].param1 = 100;
			}

		}

		Dimmer[] _dimmers = new Dimmer[512];

		bool[] allumageForce = new bool[512];

		public bool[] AllumageForce {
			get {
				return allumageForce;
			}
		}
		public string Nom;
		public enum FTransfer {
			lineaire,
			log,
			exp
		}

		public struct Dimmer {
			public Circuit circuitAssocié;
			public FTransfer fonctionTransfert;
			public float param1; // Paramètres pour fonction de transfert
			public float param2;
		}

		public Dimmer[] Dimmers  {
			get
			{
				return _dimmers;
			}
		}


		public void CalculUnivers(byte[] valeurs, int offset, int count)
		{
			Dimmer g;
			Debug.Assert(valeurs.Length == _dimmers.Length);
			for(int i = 0 ; i<count; i++)
			{
				if(allumageForce[i]) {
					valeurs[i+offset] = 255;
					break;
				}

				g= _dimmers[i];
				if(g.circuitAssocié !=null) {
					switch (g.fonctionTransfert) {
					case FTransfer.lineaire:
						valeurs[i+offset] = (byte)( g.circuitAssocié.ValeurCourante * g.param1 /100);
						break;
					case FTransfer.log:
						break;
					case FTransfer.exp:
						break;
					default:
						throw new ArgumentOutOfRangeException ();
					}
				}
				else 
					valeurs[i+offset] = 0;
			}
		}

		public void Save (XmlElement parent)
		{
			XmlElement el = parent.OwnerDocument.CreateElement ("UniversDMX");
			parent.AppendChild (el);

			el.SetAttribute ("nom", Nom);
			XmlElement xmlDim; int dim;
			for(dim=0; dim < _dimmers.Length; dim++)
			{
				if(_dimmers[dim].circuitAssocié!=null)
				{
					el.AppendChild(xmlDim = el.OwnerDocument.CreateElement("Dimmer"));
					xmlDim.SetAttribute("num",dim.ToString());
					xmlDim.SetAttribute("circuit",_dimmers[dim].circuitAssocié.ID.ToString());
					xmlDim.SetAttribute("ft",_dimmers[dim].fonctionTransfert.ToString());
					xmlDim.SetAttribute("param1",_dimmers[dim].param1.ToString() );
					xmlDim.SetAttribute("param2",_dimmers[dim].param2.ToString() );
				}

			}
		}

		public static UniversDMX Load (Conduite c, XmlElement el)
		{
			if (el.Name != "UniversDMX")
				throw new ErreurLectureFichier ("<UniversDMX> attendu.");
			UniversDMX univ = new UniversDMX ();

			univ.Nom = el.GetAttribute("nom");

			XmlElement xmlDim; Dimmer dim;
			foreach (var xd in el.GetElementsByTagName("Dimmer")) {
				xmlDim = xd as XmlElement;
				dim.circuitAssocié = c.GetCircuitByID(int.Parse(xmlDim.GetAttribute("circuit")));
				FTransfer.TryParse(xmlDim.GetAttribute("ft"),out dim.fonctionTransfert);
				dim.param1 = float.Parse(xmlDim.GetAttribute("param1"));
				dim.param2 = float.Parse(xmlDim.GetAttribute("param2"));
				univ.Dimmers[int.Parse(xmlDim.GetAttribute("num"))] = dim;
			}
			return univ;
		}
	}

}