using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantListImplement.Models;

namespace PrecastConcretePlantListImplement.Implements
{
    public class ReinforcedStorage : IReinforcedStorage
    {
        private readonly DataListSingleton _source;

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

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

            return null;
        }

        public ReinforcedViewModel? GetElement(ReinforcedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ReinforcedName) && !model.Id.HasValue)
            {
                return null;
            }

            foreach (var reinforced in _source.Reinforcedies)
            {
                if ((!string.IsNullOrEmpty(model.ReinforcedName) && reinforced.ReinforcedName == model.ReinforcedName) || (model.Id.HasValue && reinforced.Id == model.Id))
                {
                    return reinforced.GetViewModel;
                }
            }

            return null;
        }

        public List<ReinforcedViewModel> GetFilteredList(ReinforcedSearchModel model)
        {
            var result = new List<ReinforcedViewModel>();

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

            foreach (var reinforced in _source.Reinforcedies)
            {
                if (reinforced.ReinforcedName.Contains(model.ReinforcedName))
                {
                    result.Add(reinforced.GetViewModel);
                }
            }

            return result;
        }

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

            foreach (var reinforced in _source.Reinforcedies)
            {
                result.Add(reinforced.GetViewModel);
            }

            return result;
        }

        public ReinforcedViewModel? Insert(ReinforcedBindingModel model)
        {
            model.Id = 1;

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

            var newReinforced = Reinforced.Create(model);

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

            _source.Reinforcedies.Add(newReinforced);

            return newReinforced.GetViewModel;
        }

        public ReinforcedViewModel? Update(ReinforcedBindingModel model)
        {
            foreach (var reinforced in _source.Reinforcedies)
            {
                if (reinforced.Id == model.Id)
                {
                    reinforced.Update(model);
                    return reinforced.GetViewModel;
                }
            }

            return null;
        }
    }
}
