using FishFactoryContracts.BindingModels;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FishFactoryListImplement.Implements
{
	public class MessageInfoStorage : IMessageInfoStorage
	{
		private readonly DataListSingleton _source;
		public MessageInfoStorage(DataListSingleton source)
		{
			_source = DataListSingleton.GetInstance();
		}

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

			foreach (var msg in _source.Messages)
			{
				if (msg.MessageId.Equals(model.MessageId))
				{
					return msg.GetViewModel;
				}
			}
			return null;
		}

		public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
		{
			var list = new List<MessageInfoViewModel>();
			foreach (var msg in _source.Messages)
			{
				if (msg.ClientId.HasValue && model.ClientId == msg.ClientId)
				{
					list.Add(msg.GetViewModel);
				}
			}
			return list;
		}

		public List<MessageInfoViewModel> GetFullList()
		{
			var list = new List<MessageInfoViewModel>();
			foreach (var msg in _source.Messages)
			{
				list.Add(msg.GetViewModel);
			}
			return list;
		}

		public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
		{
			var newMessage = MessageInfo.Create(model);
			if (newMessage == null)
			{
				return null;
			}
			_source.Messages.Add(newMessage);
			return newMessage.GetViewModel;
		}
	}
}
