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

namespace AbstractFoodOrdersDatabaseImplement.Implements
{
	public class MessageInfoStorage:IMessageInfoStorage
	{
		public List<MessageInfoViewModel> GetFullList()
		{
			using var context = new AbstractFoodOrdersDatabase();
			return context.MessageInfos
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
		{
			if (string.IsNullOrEmpty(model.MessageId) && !model.ClientId.HasValue)
			{
				return new();
			}
			using var context = new AbstractFoodOrdersDatabase();
			return context.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();
			}
			using var context = new AbstractFoodOrdersDatabase();
			return context.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;
			}
			using var context = new AbstractFoodOrdersDatabase();
			context.MessageInfos.Add(newMessageInfo);
			context.SaveChanges();
			return newMessageInfo.GetViewModel;
		}
	}
}
