using MotorPlantContracts.BindingModels;
using MotorPlantContracts.SearchModels;
using MotorPlantContracts.StoragesContracts;
using MotorPlantContracts.ViewModels;
using MotorPlantListImplement.Models;

namespace MotorPlantListImplement.Implements
{
    public class EngineStorage : IEngineStorage
    {
        private readonly DataListSingleton _source;
        public EngineStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public EngineViewModel? Delete(EngineBindingModel model)
        {
            for (int i = 0; i < _source.Engines.Count; ++i)
            {
                if (_source.Engines[i].Id == model.Id)
                {
                    var element = _source.Engines[i];
                    _source.Engines.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
        public EngineViewModel? GetElement(EngineSearchModel model)
        {
            if (string.IsNullOrEmpty(model.EngineName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var Engine in _source.Engines)
            {
                if ((!string.IsNullOrEmpty(model.EngineName) && Engine.EngineName == model.EngineName) ||
                (model.Id.HasValue && Engine.Id == model.Id))
                {
                    return Engine.GetViewModel;
                }
            }
            return null;
        }
        public List<EngineViewModel> GetFilteredList(EngineSearchModel model)
        {
            var result = new List<EngineViewModel>();
            if (string.IsNullOrEmpty(model.EngineName))
            {
                return result;
            }
            foreach (var Engine in _source.Engines)
            {
                if (Engine.EngineName.Contains(model.EngineName))
                {
                    result.Add(Engine.GetViewModel);
                }
            }
            return result;
        }
        public List<EngineViewModel> GetFullList()
        {
            var result = new List<EngineViewModel>();
            foreach (var Engine in _source.Engines)
            {
                result.Add(Engine.GetViewModel);
            }
            return result;
        }
        public EngineViewModel? Insert(EngineBindingModel model)
        {
            model.Id = 1;
            foreach (var Engine in _source.Engines)
            {
                if (model.Id <= Engine.Id)
                {
                    model.Id = Engine.Id + 1;
                }
            }
            var newEngine = Engine.Create(model);
            if (newEngine == null)
            {
                return null;
            }
            _source.Engines.Add(newEngine);
            return newEngine.GetViewModel;
        }
        public EngineViewModel? Update(EngineBindingModel model)
        {
            foreach (var Engine in _source.Engines)
            {
                if (Engine.Id == model.Id)
                {
                    Engine.Update(model);
                    return Engine.GetViewModel;
                }
            }
            return null;
        }
    }
}
