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

namespace DatabaseImplement.Implements
{
    public class ProductStorage : IProductStorage
    {
        public ProductViewModel? Delete(ProductBindingModel model)
        {
            using var context = new Database();
            var element = context.Products
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Products.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public ProductViewModel? GetElement(ProductSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new Database();
            return context.Products
            .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }

        public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
        {
            if (!model.IsBeingSold.HasValue && !model.Price.HasValue && !model.Rate.HasValue && !model.Amount.HasValue && string.IsNullOrEmpty(model.Name))
            {
                return new();
            }
            using var context = new Database();
            if (model.Price.HasValue)
            {
                return context.Products
                .Where(x => x.Price <= model.Price)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
            if (model.Rate.HasValue)
            {
                return context.Products
                .Where(x => x.Rate <= model.Rate)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
            if (model.Amount.HasValue && model.IsBeingSold.HasValue)
            {
                return context.Products
                .Where(x => x.IsBeingSold == model.IsBeingSold && x.Amount >= model.Amount)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
            return context.Products
                .Where(x => x.Name == model.Name)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<ProductViewModel> GetFullList()
        {
            using var context = new Database();
            return context.Products
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }

        public ProductViewModel? Insert(ProductBindingModel model)
        {
            using var context = new Database();
            var newProduct = Product.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Products.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }

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