using System.Linq;
using Machinations.FromFlash.graph;
using Machinations.FromFlash.machinations.model;

namespace Machinations.Status
{
    public class ConnectionStatus : IElementStatus
    {
        private readonly double[] m_values;
        protected readonly MachinationsConnection m_connection;
        private readonly int m_step;

        public ConnectionStatus(MachinationsConnection connection, int step)
        {
            m_connection = connection;
            m_values = connection.Label.Save();
            m_step = step;
        }

        public int Step { get { return m_step; } }

        public virtual void Restore()
        {
            m_connection.Label.Restore(m_values);
            m_connection.OnChanged("Status restored");
        }

        public virtual bool NeedNewStatus()
        {
            return !m_values.SequenceEqual(m_connection.Label.Save());
        }
    }

    public class StateConnectionStatus : ConnectionStatus
    {
        private int m_state;
        public StateConnectionStatus(StateConnection connection, int step) : base(connection, step)
        {
            //m_state = connection.State;
        }

        public override void Restore()
        {
            base.Restore();
            //((StateConnection) m_connection).State = m_state;
            //(m_connection.End as MachinationsNode)?.CheckInhibition();
            //(m_connection.End as MachinationsConnection)?.CheckInhibition();
            if(((StateConnection)m_connection).Label.IsCondition())
                ((StateConnection)m_connection).State = ((StateConnection)m_connection).State;

        }
    }
}
