using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreListImplement.Models;

namespace JewelryStoreListImplement.Implements
{
    public class JewelStorage : IJewelStorage 
    {
        private readonly DataListSingleton _source;

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

        public List<JewelViewModel> GetFullList()
        {
            var result = new List<JewelViewModel>();
            foreach (var jewel in _source.Jewels)
            {
                result.Add(jewel.GetViewModel);
            }
            return result;
        }

        public List<JewelViewModel> GetFilteredList(JewelSearchModel model)
        {
            var result = new List<JewelViewModel>();
            if (string.IsNullOrEmpty(model.JewelName))
            {
                return result;
            }
            foreach (var jewel in _source.Jewels)
            {
                if (jewel.JewelName.Contains(model.JewelName))
                {
                    result.Add(jewel.GetViewModel);
                }
            }
            return result;
        }

        public JewelViewModel? GetElement(JewelSearchModel model)
        {
            if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var jewel in _source.Jewels)
            {
                if ((!string.IsNullOrEmpty(model.JewelName) && jewel.JewelName == model.JewelName) ||
                (model.Id.HasValue && jewel.Id == model.Id))
                {
                    return jewel.GetViewModel;
                }
            }
            return null;
        }

        public JewelViewModel? Insert(JewelBindingModel model)
        {
            model.Id = 1;
            foreach (var jewel in _source.Jewels)
            {
                if (model.Id <= jewel.Id)
                {
                    model.Id = jewel.Id + 1;
                }
            }
            var newJewel = Jewel.Create(model);
            if (newJewel == null)
            {
                return null;
            }
            _source.Jewels.Add(newJewel);
            return newJewel.GetViewModel;
        }

        public JewelViewModel? Update(JewelBindingModel model)
        {
            foreach (var jewel in _source.Jewels)
            {
                if (jewel.Id == model.Id)
                {
                    jewel.Update(model);
                    return jewel.GetViewModel;
                }
            }
            return null;
        }

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