using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using TypographyContracts.BindingModels;
using TypographyContracts.SearchModels;
using TypographyContracts.StoragesContracts;
using TypographyContracts.ViewModels;
using TypographyListImplement.Models;

namespace TypographyListImplement.Implements
{
    public class PrintedStorage : IPrintedStorage
    {
        private readonly DataListSingleton _source;

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

        public List<PrintedViewModel> GetFullList()
        {
            var result = new List<PrintedViewModel>();
            foreach (var printed in _source.Printeds)
            {
                result.Add(printed.GetViewModel);
            }
            return result;
        }

        public List<PrintedViewModel> GetFilteredList(PrintedSearchModel model)
        {
            var result = new List<PrintedViewModel>();
            if (string.IsNullOrEmpty(model.PrintedName))
            {
                return result;
            }
            foreach (var printed in _source.Printeds)
            {
                if (printed.PrintedName.Contains(model.PrintedName))
                {
                    result.Add(printed.GetViewModel);
                }
            }
            return result;
        }
        public PrintedViewModel? GetElement(PrintedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PrintedName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var printed in _source.Printeds)
            {
                if ((!string.IsNullOrEmpty(model.PrintedName) && printed.PrintedName == model.PrintedName) || (model.Id.HasValue && printed.Id == model.Id))
                {
                    return printed.GetViewModel;
                }
            }
            return null;
        }

        public PrintedViewModel? Insert(PrintedBindingModel model)
        {
            model.Id = 1;
            foreach (var printed in _source.Printeds)
            {
                if (model.Id <= printed.Id)
                {
                    model.Id = printed.Id + 1;
                }
            }
            var newPrinted = Printed.Create(model);
            if (newPrinted == null)
            {
                return null;
            }
            _source.Printeds.Add(newPrinted);
            return newPrinted.GetViewModel;
        }

        public PrintedViewModel? Update(PrintedBindingModel model)
        {
            foreach (var printed in _source.Printeds)
            {
                if (printed.Id == model.Id)
                {
                    printed.Update(model);
                    return printed.GetViewModel;
                }
            }
            return null;
        }
        public PrintedViewModel? Delete(PrintedBindingModel model)
        {
            for (int i = 0; i < _source.Printeds.Count; ++i)
            {
                if (_source.Printeds[i].Id == model.Id)
                {
                    var element = _source.Printeds[i];
                    _source.Printeds.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
    }
}
