using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using KellermanSoftware.NinjaDatabasePro;
using Wpf_Demo.ViewModels;

namespace Wpf_Demo.DataAccess
{
    public static class DatabaseAccessLayer
    {
        private static NinjaDbPro _connectedDatabase;
        public static NinjaDbPro ConnectedDatabase
        {
            get
            {
                if (_connectedDatabase == null)
                {
                    _connectedDatabase = new NinjaDbPro("BucketListDir", "BucketList");
                    _connectedDatabase.OpenDatabase();
                }
                return _connectedDatabase;
            }
        }

        private static ObservableCollection<GoalViewModel> _items;
        public static ObservableCollection<GoalViewModel> Items
        {
            get
            {
                if (_items == null)
                {
                    // Loading items from the db
                    List<GoalViewModel> itemsTable = ConnectedDatabase.LoadAll<GoalViewModel>();

                    // Loading it
                    _items = new ObservableCollection<GoalViewModel>();
                    foreach (var itemViewModel in itemsTable)
                    {
                        itemViewModel.MarkOld();
                        _items.Add(itemViewModel);
                    }
                }
                return _items;
            }
        }

        public static void CloseDatabase()
        {
            if (ConnectedDatabase.IsDatabaseOpen)
                ConnectedDatabase.CloseDatabase();
        }

        public static void UpdateItem(GoalViewModel item)
        {
            if (item == null)
                return;

            ConnectedDatabase.Save(item);
            //ConnectedDatabase.CloseDatabase();
            //ConnectedDatabase.OpenDatabase();

            GoalViewModel modelCopy = ConnectedDatabase.Load<GoalViewModel>(item.Id);
        }

        public static void UpdateItems(IEnumerable<GoalViewModel> items)
        {
            if (items == null)
                return;

            foreach (var item in items)
            {
                UpdateItem(item);
            }
        }

        public static void DeleteItem(GoalViewModel item)
        {
            if (item == null)
                return;

            //Remove from DB
            long deleted = ConnectedDatabase.Delete(item);

            // Updating the items list
            Items.Remove(item);
        }

        public static void DeleteItems(IEnumerable<GoalViewModel> items)
        {
            if (items == null)
                return;

            foreach (var item in items)
            {
                DeleteItem(item);
            }
        }

        public static void AddItem(GoalViewModel item)
        {
            if (item == null)
                return;

            // Updating the list
            if (!Items.Contains(item))
                Items.Add(item);

            // Adding to the db
            ConnectedDatabase.Save(item);
        }

        public static void AddItems(IEnumerable<GoalViewModel> items)
        {
            if (items == null)
                return;

            foreach (var item in items)
            {
                AddItem(item);
            }
        }
    }
}