using System.Windows.Media;
using System.Xml;
using Machinations.FromFlash.graph;

namespace Machinations.FromFlash.machinations.model
{
    /// <summary>
    /// Machinations node, can create resources
    /// </summary>
    public class Source : MachinationsNode
    {
        protected Color m_resourceColor;
		
		public Source()
		{
            m_resourceColor = ColorsUtils.InitColor;
            m_activationMode = ActivationMode.Automatic;
        }

        public Color ResourceColor
        {
            get { return m_resourceColor; }
            set { m_resourceColor = value; }
        }

        public override XmlElement GenerateXml(XmlDocument document, XmlElement element = null) 
		{
			element = base.GenerateXml(document, element);
            element.SetAttribute("resourceColor", m_resourceColor.ToNiceString());
			return element;
		}

        public override NodeClass NodeClass => NodeClass.Source;

        public override void ReadXml(XmlElement element) 
		{
			base.ReadXml(element);
			m_resourceColor = ColorsUtils.GetColorFromString(element.GetAttribute("resourceColor"));
		}

		public override void Fire() 
		{
			base.Fire();
            Satisfy();
		}

        public override void CheckInhibition()
        {
            base.CheckInhibition();
        }

        public override void Satisfy() 
		{
		    m_outputs.ForEach(output =>
		    {
		        ResourceConnection res = output as ResourceConnection;
		        if (res != null && !res.Inhibited) res.Produce(Color, ResourceColor);
                StateConnection sta = output as StateConnection;
		        sta?.Fire();
		    });
			base.Satisfy();
		}

        public override string ToMMString()
        {
            return GetMMWhen() + " " + GetMMAct() + " " + GetMMHow() + " source " + GetMMName();
        }
    }
}
