using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.SearchModels;
using GarmentFactoryContracts.StoragesContracts;
using GarmentFactoryContracts.ViewModels;
using GarmentFactoryListImplement.Models;

namespace GarmentFactoryListImplement.Implements
{
    public class TextileStorage : ITextileStorage
    {
        private readonly DataListSingleton _source;

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

        public List<TextileViewModel> GetFullList()
        {
            var result = new List<TextileViewModel>();
            foreach (var Textile in _source.Textiles)
            {
                result.Add(Textile.GetViewModel);
            }
            return result;
        }

        public List<TextileViewModel> GetFilteredList(TextileSearchModel model)
        {
            var result = new List<TextileViewModel>();
            if (string.IsNullOrEmpty(model.TextileName))
            {
                return result;
            }
            foreach (var textile in _source.Textiles)
            {
                if (textile.TextileName.Contains(model.TextileName))
                {
                    result.Add(textile.GetViewModel);
                }
            }
            return result;
        }

        public TextileViewModel? GetElement(TextileSearchModel model)
        {
            if (string.IsNullOrEmpty(model.TextileName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var textile in _source.Textiles)
            {
                if ((!string.IsNullOrEmpty(model.TextileName) &&
                textile.TextileName == model.TextileName) ||
                (model.Id.HasValue && textile.Id == model.Id))
                {
                    return textile.GetViewModel;
                }
            }
            return null;
        }

        public TextileViewModel? Insert(TextileBindingModel model)
        {
            model.Id = 1;
            foreach (var textile in _source.Textiles)
            {
                if (model.Id <= textile.Id)
                {
                    model.Id = textile.Id + 1;
                }
            }
            var newTextile = Textile.Create(model);
            if (newTextile == null)
            {
                return null;
            }
            _source.Textiles.Add(newTextile);
            return newTextile.GetViewModel;
        }

        public TextileViewModel? Update(TextileBindingModel model)
        {
            foreach (var textile in _source.Textiles)
            {
                if (textile.Id == model.Id)
                {
                    textile.Update(model);
                    return textile.GetViewModel;
                }
            }
            return null;
        }

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