namespace DxBlazorApplication2.Services
{
    public class TodoService
    {
        private readonly IList<TodoItem> _todoItems;
        public delegate void ToDoListeDelegate(object sender, ToDoListeEventArgs args);
        public event ToDoListeDelegate OnToDoListeChanged;

        public TodoService()
        {
            _todoItems = new List<TodoItem>()
            {
                new TodoItem("Wäsche waschen"),
                new TodoItem("Wäsche trocknen")
            };
        }

        public void AddItem(TodoItem item)
        {
            _todoItems.Add(item);
            if (this.OnToDoListeChanged != null)
                this.OnToDoListeChanged(this, new ToDoListeEventArgs(item));
        }
        public void RemoveItem(TodoItem item)
        {
            _todoItems.Remove(item);
            if (this.OnToDoListeChanged != null)
                this.OnToDoListeChanged(this, new ToDoListeEventArgs(item));
        }
        public IEnumerable<TodoItem> GetItems()
        {
            return _todoItems;
        }
        public void Complete(TodoItem item)
        {
            item.Completed = true;
            if (this.OnToDoListeChanged != null)
                this.OnToDoListeChanged(this, new ToDoListeEventArgs(item));
        }
        public void Uncomplete(TodoItem item)
        {
            item.Completed = false;
            if (this.OnToDoListeChanged != null)
                this.OnToDoListeChanged(this, new ToDoListeEventArgs(item));
        }
    }

    public class ToDoListeEventArgs : EventArgs
    {
        public TodoItem RelatedItem { get; }
        public ToDoListeEventArgs(TodoItem item)
        {
            this.RelatedItem = item;
        }
    }
}
