using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DressAtelierContracts.BindingModels;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.StorageContracts;
using DressAtelierContracts.ViewModels;
using DressAtelierListImplement.Models;

namespace DressAtelierListImplement.Implements
{
    public class MaterialStorage : IMaterialStorage
    {
        private readonly DataListSingleton _source;

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

        public List<MaterialViewModel> GetFullList()
        {
            var result = new List<MaterialViewModel>();
            foreach (var material in _source.Components)
            {
                result.Add(material.GetViewModel);
            }
            return result;
        }
        public List<MaterialViewModel> GetFilteredList(MaterialSearchModel model)
        {
            var result = new List<MaterialViewModel>();
            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 MaterialViewModel? GetElement(MaterialSearchModel 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 MaterialViewModel? Insert(MaterialBindingModel model)
        {
            model.ID = 1;
            foreach (var component in _source.Components)
            {
                if (model.ID <= component.ID)
                {
                    model.ID = component.ID + 1;
                }
            }
            var newComponent = Material.Create(model);
            if (newComponent == null)
            {
                return null;
            }
            _source.Components.Add(newComponent);
            return newComponent.GetViewModel;
        }

        public MaterialViewModel? Update(MaterialBindingModel model)
        {
            foreach (var component in _source.Components)
            {
                if (component.ID == model.ID)
                {
                    component.Update(model);
                    return component.GetViewModel;
                }
            }
            return null;
        }

        public MaterialViewModel? Delete(MaterialBindingModel 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;
        }

    }
}
