using System;
using System.Collections.Generic;
using Gallery.Resources.UI;
using Gallery.Util.Model;
using Xamarin.Essentials;

namespace Gallery.Services
{
    public class GalleryCollection : List<GalleryItem>, ICollectionChanged
    {
        private static GalleryCollection empty;

        public static GalleryCollection Empty
        {
            get
            {
                if (empty == null)
                {
                    empty = new GalleryCollection();
                }
                return empty;
            }
        }

        public event EventHandler<CollectionChangedEventArgs> CollectionChanged;

        public bool Running { get; set; }

        public GalleryCollection() : base()
        {
            Running = true;
        }

        public GalleryCollection(IEnumerable<GalleryItem> gallery) : base(gallery)
        {
            Running = true;
        }

        public void AddRange(List<GalleryItem> items)
        {
            var e = new CollectionChangedEventArgs
            {
                NewStartingIndex = Count,
                NewItems = items
            };
            base.AddRange(items);
            if (MainThread.IsMainThread)
            {
                CollectionChanged?.Invoke(this, e);
            }
            else
            {
                MainThread.BeginInvokeOnMainThread(() => CollectionChanged?.Invoke(this, e));
            }
        }
    }
}