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

namespace PrecastConcretePlantListImplement.Implements
{
    public class ClientStorage : IClientStorage
    {
        private readonly DataListSingleton _source;
        public ClientStorage()
        {
            _source = DataListSingleton.GetInstance();
        }

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

        public ClientViewModel? GetElement(ClientSearchModel model)
        {
            foreach (var client in _source.Clients)
            {
                if (model.Id.HasValue && model.Id == client.Id)
                    return client.GetViewModel;
                if (model.Email != null && model.Password != null &&
                    client.Email.Equals(model.Email) && client.Password.Equals(model.Password))
                    return client.GetViewModel;
                if (model.Email != null && client.Email.Equals(model.Email))
                    return client.GetViewModel;
            }
            return null;
        }

        public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
        {
            if (model == null)
            {
                return new();
            }
            // У нас нет каких либо специфических условий для выборки списка клиентов,
            // поэтому получаем лишь один элемент по модели
            var res = GetElement(model);
            return res != null ? new() { res } : new();
        }

        public List<ClientViewModel> GetFullList()
        {
            var result = new List<ClientViewModel>();
            foreach (var client in _source.Clients)
            {
                result.Add(client.GetViewModel);
            }
            return result;
        }

        public ClientViewModel? Insert(ClientBindingModel model)
        {
            model.Id = 1;
            foreach (var client in _source.Clients)
            {
                if (model.Id <= client.Id)
                {
                    model.Id = client.Id + 1;
                }
            }
            var res = Client.Create(model);
            if (res != null)
            {
                _source.Clients.Add(res);
            }
            return res?.GetViewModel;
        }

        public ClientViewModel? Update(ClientBindingModel model)
        {
            foreach (var client in _source.Clients)
            {
                if (client.Id == model.Id)
                {
                    client.Update(model);
                    return client.GetViewModel;
                }
            }
            return null;
        }
    }
}
