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

namespace FishFactoryDatabaseImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        public List<OrderViewModel> GetFullList()
        {
            using var context = new FishFactoryDatabase();
			return context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel).ToList();
		}
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
			if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.ClientId.HasValue && !model.Status.HasValue)
			{
				return new();
			}
			using var context = new FishFactoryDatabase();
			if (model.Id.HasValue)
				return context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
			else if (model.ClientId.HasValue)
				return context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
			else if (model.DateFrom != null)
				return context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList();
			else
				return context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).Where(x => x.Status.Equals(model.Status)).Select(x => x.GetViewModel).ToList();
		}
        public OrderViewModel? GetElement(OrderSearchModel model)
        {
			if (!model.Id.HasValue && (!model.ImplementerId.HasValue || !model.Status.HasValue))
			{
				return null;
			}
			using var context = new FishFactoryDatabase();
			if (model.Id.HasValue)
				return context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
			return context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.ImplementerId == model.ImplementerId && x.Status == model.Status)?.GetViewModel;
		}
        public OrderViewModel? Insert(OrderBindingModel model)
        {
			using var context = new FishFactoryDatabase();
			var newOrder = Order.Create(model, context);
            if (newOrder == null)
            {
                return null;
            }
            context.Orders.Add(newOrder);
            context.SaveChanges();
            return AccessCannedStorage(newOrder.GetViewModel, context);
        }
        public OrderViewModel? Update(OrderBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            var order = context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id);
            if (order == null)
            {
                return null;
            }
            order.Update(model);
            context.SaveChanges();
            return AccessCannedStorage(order.GetViewModel, context);
        }
        public OrderViewModel? Delete(OrderBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            var element = context.Orders.Include(x => x.Canned).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(rec => rec.Id ==
           model.Id);
            if (element != null)
            {
                context.Orders.Remove(element);
                context.SaveChanges();
                return AccessCannedStorage(element.GetViewModel, context);
            }
            return null;
        }

        public static OrderViewModel AccessCannedStorage(OrderViewModel? model, FishFactoryDatabase context)
        {
            if (model == null)
            {
                return null;
            }
            var canned = context.Canneds.FirstOrDefault(x => x.Id == model.CannedId);
            if (canned == null)
            {
                return null;
            }
            var client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId);
            if (client == null)
            {
                return null;
            }
            model.CannedName = canned.CannedName;
            model.ClientId = client.Id;
            model.ClientFIO = client.ClientFIO;
            return model;
        }
    }
}
