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 DressStorage : IDressStorage
    {
        private readonly DataListSingleton _source;

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

        public List<DressViewModel> GetFullList()
        {
            var result = new List<DressViewModel>();
            foreach (var material in _source.Dresses)
            {
                result.Add(material.GetViewModel);
            }
            return result;
        }

        public List<DressViewModel> GetFilteredList(DressSearchModel model)
        {
            var result = new List<DressViewModel>();
            if (string.IsNullOrEmpty(model.DressName))
            {
                return result;
            }
            foreach (var dress in _source.Dresses)
            {
                if (dress.DressName.Contains(model.DressName))
                {
                    result.Add(dress.GetViewModel);
                }
            }
            return result;
        }

        public DressViewModel? GetElement(DressSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DressName) && !model.ID.HasValue)
            {
                return null;
            }

            foreach (var dress in _source.Dresses)
            {
                if ((!string.IsNullOrEmpty(model.DressName) && dress.DressName == model.DressName) || (model.ID.HasValue && dress.ID == model.ID))
                {
                    return dress.GetViewModel;
                }
            }
            return null;
        }

        public DressViewModel? Insert(DressBindingModel model)
        {
            model.ID = 1;
            foreach (var dress in _source.Dresses)
            {
                if (model.ID <= dress.ID)
                {
                    model.ID = dress.ID + 1;
                }
            }
            var newComponent = Dress.Create(model);
            if (newComponent == null)
            {
                return null;
            }
            _source.Dresses.Add(newComponent);
            return newComponent.GetViewModel;
        }

        public DressViewModel? Update(DressBindingModel model)
        {
            foreach (var dress in _source.Dresses)
            {
                if (dress.ID == model.ID)
                {
                    dress.Update(model);
                    return dress.GetViewModel;
                }
            }
            return null;
        }

        public DressViewModel? Delete(DressBindingModel model)
        {
            for (int i = 0; i < _source.Dresses.Count; ++i)
            {
                if (_source.Dresses[i].ID == model.ID)
                {
                    var element = _source.Dresses[i];
                    _source.Dresses.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
    }
}
