using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseImplement.Implements
{
    public class SupplyStorage : ISupplyStorage
    {
        public SupplyViewModel? Delete(SupplyBindingModel model)
        {
            using var context = new Database();
            var element = context.Supplies
            .Include(x => x.Products)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Supplies.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public SupplyViewModel? GetElement(SupplySearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new Database();
            return context.Supplies
            .Include(x => x.Supplier)
            .Include(x => x.Products)
            .ThenInclude(x => x.Product)
            .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }

        public List<SupplyViewModel> GetFilteredList(SupplySearchModel model)
        {
            if (!model.DateStart.HasValue && !model.DateEnd.HasValue && model.Status == null) 
            {
                return new();
            }
            using var context = new Database();
            if (model.DateStart.HasValue && model.DateEnd.HasValue)
            {
                return context.Supplies
                .Include(x => x.Supplier)
                .Include(x => x.Products)
                .ThenInclude(x => x.Product)
                .Where(x => x.Id == model.Id || model.DateStart <= x.Date && x.Date <= model.DateEnd)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
            if (model.DateEnd.HasValue)
            {
                return context.Supplies
                .Include(x => x.Supplier)
                .Include(x => x.Products)
                .ThenInclude(x => x.Product)
                .Where(x => x.Id == model.Id || x.Date <= model.DateEnd)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
            if (model.DateStart.HasValue)
            {
                return context.Supplies
                .Include(x => x.Supplier)
                .Include(x => x.Products)
                .ThenInclude(x => x.Product)
                .Where(x => x.Id == model.Id || model.DateStart <= x.Date)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
            return context.Supplies
                .Include(x => x.Supplier)
                .Include(x => x.Products)
                .ThenInclude(x => x.Product)
                .Where(x => model.Status.Equals(x.Status))
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<SupplyViewModel> GetFullList()
        {
            using var context = new Database();
            return context.Supplies
            .Include(x => x.Supplier)
            .Include(x => x.Products)
            .ThenInclude(x => x.Product)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }

        public bool? Insert(SupplyBindingModel model)
        {
            using var context = new Database();
            var newProduct = Supply.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Supplies.Add(newProduct);
            try
            {
                context.SaveChanges();
            }
            catch (Exception ex) 
            {
                Debug.WriteLine(ex);
            }
            return true;
        }

        public bool? Update(SupplyBindingModel model)
        {
            using var context = new Database();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Supplies.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (product == null)
                {
                    return false;
                }
                product.Update(model);
                context.SaveChanges();
                transaction.Commit();
                return true;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}
