using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftwareInstallationFileImplement.Implements
{
	public class MessageInfoStorage : IMessageInfoStorage
	{
		private readonly DataFileSingleton source;
		public MessageInfoStorage()
		{
			source = DataFileSingleton.GetInstance();
		}
		public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
		{
			if (model.MessageId == null)
				return null;
			return source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel;
		}

		public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
		{
			if (!model.ClientId.HasValue)
				return new();
			return source.Messages
				.Where(x => x.ClientId == model.ClientId)
				.Select(x => x.GetViewModel)
				.ToList();
		}

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

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