using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesListImplement.Models;

namespace SewingDressesListImplement.Implements
{
    public class DressStorage : IDressStorage
    {
        private readonly DataListSingleton _source;
        public DressStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public List<DressViewModel> GetFullList()
        {
            var result = new List<DressViewModel>();
            foreach (var component in _source.Dresses)
            {
                result.Add(component.GetViewModel);
            }
            return result;
        }
        public List<DressViewModel> GetFilteredList(DressSearchModel model)
        {
            var result = new List<DressViewModel>();
            if (string.IsNullOrEmpty(model.DressName))
            {
                return result;
            }
            foreach (var component in _source.Dresses)
            {
                if (component.DressName.Contains(model.DressName))
                {
                    result.Add(component.GetViewModel);
                }
            }
            return result;
        }
        public DressViewModel? GetElement(DressSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DressName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var component in _source.Dresses)
            {
                if ((!string.IsNullOrEmpty(model.DressName) && component.DressName == model.DressName) ||
                    (model.Id.HasValue && component.Id == model.Id))
                {
                    return component.GetViewModel;
                }
            }
            return null;
        }

        public DressViewModel? Insert(DressBindingModel model)
        {
            model.Id = 1;
            foreach (var component in _source.Dresses)
            {
                if (model.Id <= component.Id)
                {
                    model.Id = component.Id + 1;
                }
            }
            var newDress = Dress.Create(model);
            if (newDress == null)
            {
                return null;
            }
            _source.Dresses.Add(newDress);
            return newDress.GetViewModel;
        }

        public DressViewModel? Update(DressBindingModel model)
        {
            foreach (var component in _source.Dresses)
            {
                if (component.Id == model.Id)
                {
                    component.Update(model);
                    return component.GetViewModel;
                }
            }
            return null;
        }

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