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

namespace JewelryStoreDatabaseImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        public List<OrderViewModel> GetFullList()
        {
            using var context = new JewelryStoreDatabase();
            return context.Orders.Include(x => x.Jewel).Include(x => x.Client).Select(x => x.GetViewModel).ToList();
        }
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue)
            {
                return new();
            }
            using var context = new JewelryStoreDatabase();
            if (model.DateFrom.HasValue)
            {
                return context.Orders.Include(x => x.Jewel).Include(x => x.Client).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
                    .Select(x => x.GetViewModel).ToList();
            }
            else if (model.ClientId.HasValue)
            {
                return context.Orders.Include(x => x.Jewel).Include(x => x.Client).Where(x => x.ClientId == model.ClientId)
                    .Select(x => x.GetViewModel).ToList();
            } 
            return context.Orders.Include(x => x.Jewel).Include(x => x.Client).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
        }
        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return new();
            }
            using var context = new JewelryStoreDatabase();
            return AccessJewelStorage(context.Orders.Include(x => x.Client).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel, context);
        }
        public OrderViewModel? Insert(OrderBindingModel model)
        {
            var newOrder = Order.Create(model);
            if (newOrder == null)
            {
                return null;
            }
            using var context = new JewelryStoreDatabase();
            context.Orders.Add(newOrder);
            context.SaveChanges();
            return AccessJewelStorage(context.Orders.Include(x => x.Jewel).Include(x => x.Client).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel, context);
        }
        public OrderViewModel? Update(OrderBindingModel model)
        {
            using var context = new JewelryStoreDatabase();
            var order = context.Orders.Include(x => x.Jewel).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id);
            if (order == null)
            {
                return null;
            }
            order.Update(model);
            context.SaveChanges();
            return AccessJewelStorage(order.GetViewModel, context);
        }
        public OrderViewModel? Delete(OrderBindingModel model)
        {
            using var context = new JewelryStoreDatabase();
            var element = context.Orders.Include(x => x.Jewel).Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Orders.Remove(element);
                context.SaveChanges();
                return AccessJewelStorage(element.GetViewModel, context);
            }
            return null;
        }
        static OrderViewModel AccessJewelStorage(OrderViewModel model, JewelryStoreDatabase context)
        {
            if (model == null) return model;
            string? jewelName = context.Jewels.FirstOrDefault(x => x.Id == model.JewelId)?.JewelName;
            if (jewelName != null) model.JewelName = jewelName;
            return model;
        }
    }
}
