using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;
using BarListImplement.Models;


namespace BarListImplement.Implements
{
    public class ComponentStorage : IComponentStorage
    {
        private readonly DataListSingleton _source;

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

        public List<ComponentViewModel> GetFullList()
        {
            var Result = new List<ComponentViewModel>();

            foreach (var Component in _source.Components)
            {
                Result.Add(Component.GetViewModel);
            }

            return Result;
        }

        public List<ComponentViewModel> GetFilteredList(ComponentSearchModel Model)
        {
            var Result = new List<ComponentViewModel>();

            if (string.IsNullOrEmpty(Model.ComponentName))
                return Result;

            foreach (var Component in _source.Components)
            {
                if (Component.ComponentName.Contains(Model.ComponentName))
                {
                    Result.Add(Component.GetViewModel);
                }
            }

            return Result;
        }

        public ComponentViewModel? GetElement(ComponentSearchModel Model)
        {
            if (string.IsNullOrEmpty(Model.ComponentName) && !Model.Id.HasValue)
                return null;

            foreach (var Component in _source.Components)
            {
                if ((!string.IsNullOrEmpty(Model.ComponentName)
                    && Component.ComponentName == Model.ComponentName)
                    || (Model.Id.HasValue && Component.Id == Model.Id))
                {
                    return Component.GetViewModel;
                }
            }

            return null;
        }

        public ComponentViewModel? Insert(ComponentBindingModel Model)
        {
            Model.Id = 1;
            foreach (var Component in _source.Components)
            {
                if (Model.Id <= Component.Id)
                {
                    Model.Id = Component.Id + 1;
                }
            }

            var NewComponent = Component.Create(Model);

            if (NewComponent == null)
                return null;

            _source.Components.Add(NewComponent);
            return NewComponent.GetViewModel;
        }

        public ComponentViewModel? Update(ComponentBindingModel Model)
        {
            foreach (var Component in _source.Components)
            {
                if (Component.Id == Model.Id)
                {
                    Component.Update(Model);
                    return Component.GetViewModel;
                }
            }

            return null;
        }

        public ComponentViewModel? Delete(ComponentBindingModel Model)
        {
            for (int i = 0; i < _source.Components.Count; ++i)
            {
                if (_source.Components[i].Id == Model.Id)
                {
                    var Element = _source.Components[i];
                    _source.Components.RemoveAt(i);

                    return Element.GetViewModel;
                }
            }

            return null;
        }
    }
}
