using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;
using BarListImplement.Models;

namespace BarListImplement.Implements
{
	public class MessageInfoStorage : IMessageInfoStorage
	{
		private readonly DataListSingleton _source;

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

		public List<MessageInfoViewModel> GetFullList()
		{
			return _source.Messages
				.Select(x => x.GetViewModel)
				.ToList();
		}

		public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel Model)
		{
			return _source.Messages
				.Where(x => x.ClientId.HasValue && x.ClientId == Model.ClientId)
				.Select(x => x.GetViewModel)
				.ToList();
		}

		public MessageInfoViewModel? GetElement(MessageInfoSearchModel Model)
		{
			if (string.IsNullOrEmpty(Model.MessageId))
				return null;

			return _source.Messages
				.FirstOrDefault(x => x.MessageId == Model.MessageId)?
				.GetViewModel;
		}

		public MessageInfoViewModel? Insert(MessageInfoBindingModel Model)
		{
			var NewMessageInfo = MessageInfo.Create(Model);

			if (NewMessageInfo == null)
				return null;

			_source.Messages.Add(NewMessageInfo);
			return NewMessageInfo.GetViewModel;
		}
	}
}
