using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopListImplement.Models;

namespace GiftShopListImplement.Implements
{
    public class GiftStorage : IGiftStorage
    {
        private readonly DataListSingleton _source;

        public GiftStorage()
        {
            _source = DataListSingleton.GetInstance();
        }

        public GiftViewModel? Delete(GiftBindingModel model)
        {
            for (int i = 0; i < _source.Gifts.Count; ++i)
            {
                if (_source.Gifts[i].Id == model.Id)
                {
                    var element = _source.Gifts[i];
                    _source.Gifts.RemoveAt(i);
                    return element.GetViewModel;
                }
            }

            return null;
        }

        public GiftViewModel? GetElement(GiftSearchModel model)
        {
            if (string.IsNullOrEmpty(model.GiftName) && !model.Id.HasValue)
            {
                return null;
            }

            foreach (var package in _source.Gifts)
            {
                if ((!string.IsNullOrEmpty(model.GiftName) && package.GiftName == model.GiftName) || (model.Id.HasValue && package.Id == model.Id))
                {
                    return package.GetViewModel;
                }
            }

            return null;
        }

        public List<GiftViewModel> GetFilteredList(GiftSearchModel model)
        {
            var result = new List<GiftViewModel>();

            if (string.IsNullOrEmpty(model.GiftName))
            {
                return result;
            }

            foreach (var package in _source.Gifts)
            {
                if (package.GiftName.Contains(model.GiftName))
                {
                    result.Add(package.GetViewModel);
                }
            }

            return result;
        }

        public List<GiftViewModel> GetFullList()
        {
            var result = new List<GiftViewModel>();

            foreach (var package in _source.Gifts)
            {
                result.Add(package.GetViewModel);
            }

            return result;
        }

        public GiftViewModel? Insert(GiftBindingModel model)
        {
            model.Id = 1;

            foreach (var package in _source.Gifts)
            {
                if (model.Id <= package.Id)
                {
                    model.Id = package.Id + 1;
                }
            }

            var newGift = Gift.Create(model);

            if (newGift == null)
            {
                return null;
            }

            _source.Gifts.Add(newGift);

            return newGift.GetViewModel;
        }

        public GiftViewModel? Update(GiftBindingModel model)
        {
            foreach (var package in _source.Gifts)
            {
                if (package.Id == model.Id)
                {
                    package.Update(model);
                    return package.GetViewModel;
                }
            }

            return null;
        }
    }
}
