using System;
using System.Collections.ObjectModel;
using Machinations.FromFlash.graph;
using Machinations.FromFlash.machinations.model;

namespace Machinations.Log
{
    /// <summary>
    /// provides collection of log records
    /// </summary>
    public class SimulationLog : ILog
    {
        //private readonly SortedSet<ActionLog> m_actions;
        private readonly ObservableCollection<ActionLog> m_actions;
        private int m_step;
        public Action OnChanged { get; set; }

        public SimulationLog()
        {
            m_actions = new ObservableCollection<ActionLog>();
        }


        public ObservableCollection<ActionLog> Actions
        {
            get { return m_actions; }
        }

        public int Step { get { return m_step; } set { m_step = value; } }


        public void Log(GraphNode node, ActivationMode cause)
        {
            m_actions.Add(new ActionLog(m_step, node, cause));
        }

        public void DeleteFrom(int step)
        {
            for (int i = m_actions.Count-1; i >= 0; i--)
            {
                if (m_actions[i].Step > step)
                    m_actions.RemoveAt(i);
                else if(m_actions[i].Step == step && m_actions[i].Mode == ActivationMode.Interactive)
                    m_actions.RemoveAt(i);
            }
        }

        public void Reset()
        {
            m_actions.Clear();
        }
    }
}