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 ImplementerStorage:IImplementerStorage
	{
		public List<ImplementerViewModel> GetFullList()
		{
			using var context = new AbstractFoodOrdersDatabase();
			return context.Implementers
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
		{
			if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue)
			{
				return new();
			}
			if (!string.IsNullOrEmpty(model.ImplementerFIO) && !string.IsNullOrEmpty(model.Password))
			{
				using var context = new AbstractFoodOrdersDatabase();
				return context.Implementers
				.Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO))
				.Select(x => x.GetViewModel)
				.ToList();
			}
			else
			{
				using var context = new AbstractFoodOrdersDatabase();
				return context.Implementers
				.Where(x => x.Id == model.Id)
				.Select(x => x.GetViewModel)
				.ToList();
			}
		}
		public ImplementerViewModel? GetElement(ImplementerSearchModel model)
		{
			using var context = new AbstractFoodOrdersDatabase();
			if (model.Id.HasValue)
				return context.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
			if (model.ImplementerFIO != null && model.Password != null)
				return context.Implementers
					.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO)
									  && x.Password.Equals(model.Password))
					?.GetViewModel;
			if (model.ImplementerFIO != null)
				return context.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel;
			return null;
		}
		public ImplementerViewModel? Insert(ImplementerBindingModel model)
		{
			var newImplementer = Implementer.Create(model);
			if (newImplementer == null)
			{
				return null;
			}
			using var context = new AbstractFoodOrdersDatabase();
			context.Implementers.Add(newImplementer);
			context.SaveChanges();
			return newImplementer.GetViewModel;
		}
		public ImplementerViewModel? Update(ImplementerBindingModel model)
		{
			using var context = new AbstractFoodOrdersDatabase();
			var component = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
			if (component == null)
			{
				return null;
			}
			component.Update(model);
			context.SaveChanges();
			return component.GetViewModel;
		}
		public ImplementerViewModel? Delete(ImplementerBindingModel model)
		{
			using var context = new AbstractFoodOrdersDatabase();
			var element = context.Implementers.FirstOrDefault(rec => rec.Id == model.Id);
			if (element != null)
			{
				context.Implementers.Remove(element);
				context.SaveChanges();
				return element.GetViewModel;
			}
			return null;
		}
	}
}
