using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.SearchModels;
using AbstractFoodOrdersContracts.StoragesContracts;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFoodOrdersFileImplement.Implements
{
	public class MessageInfoStorage:IMessageInfoStorage
	{
		private readonly DataFileSingleton source;
		public MessageInfoStorage()
		{
			source = DataFileSingleton.GetInstance();
		}
		public List<MessageInfoViewModel> GetFullList()
		{
			return source.MessageInfos
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
		{
			if (string.IsNullOrEmpty(model.MessageId) && !model.ClientId.HasValue)
			{
				return new();
			}
			return source.MessageInfos
				.Where(x => x.ClientId == model.ClientId)
				.Select(x => x.GetViewModel)
				.ToList();
		}
		public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
		{
			if (string.IsNullOrEmpty(model.MessageId) && !model.ClientId.HasValue)
			{
				return new();
			}
			return source.MessageInfos
			.FirstOrDefault(x => x.MessageId.Equals(model.MessageId) && x.ClientId == model.ClientId)
			?.GetViewModel;
		}

		public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
		{
			var newMessageInfo = MessageInfo.Create(model);
			if (newMessageInfo == null)
			{
				return null;
			}
			source.MessageInfos.Add(newMessageInfo);
			source.SaveMessageInfos();
			return newMessageInfo.GetViewModel;
		}
	}
}
