using Machinations.FromFlash.graph;

namespace Machinations.FromFlash.machinations.model
{
    /// <summary>
    /// machinations node,able of stopping a game while triggered, uninhibited or receive recource
    /// </summary>
    public class EndCondition : MachinationsNode 
    {
        public override NodeClass NodeClass => NodeClass.EndCondition;


        public override void Prepare(bool doEvents) 
		{
			base.Prepare(doEvents);
			Inhibited = true;
		    m_inhibitedNewState = true;
			if (doEvents) OnChanged(GraphEvent.ElementChange);
		}

        public override void Update(double time) 
		{
			if (!Inhibited && !(m_graph as MachinationsGraph).Ended)
            {
				(m_graph as MachinationsGraph).End(m_caption);

                Firing = 0.25;
				if (m_doEvents) OnChanged(GraphEvent.ElementChange);
			}
			if (!Inhibited) {
				if (Firing >= 0)
                {
                    Firing -= time;
                    if (Firing < 0 && m_doEvents) OnChanged(GraphEvent.ElementChange);
                } else {
                    Firing -= time;
                    if (Firing < -0.25 && m_doEvents)
                    {
                        Firing += 0.5;
                        OnChanged(GraphEvent.ElementChange);
                    }
                }
            }
		}
		
		public override void Fire() 
		{
			base.Fire();
            (m_graph as MachinationsGraph).End(m_caption);
            Firing = 0.25;
            Inhibited = false;
        }

  //      public override MyVector GetConnection(MyVector towards) 
		//{
		//	MyVector positionClone = Position.Clone();
		//	MyVector u = towards.Subtract(Position);
		//	u.Normalize();
		//	MyVector p = MathUtil.GetSquareOutlinePoint(u, 0.5 * Size + Thickness + 1);
		//	positionClone.IncrementBy(p);
		//	return positionClone;			
		//}
    }
}
