using ComputersShopContracts.BindingModels;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopDatabaseImplement.Models;
using ComputersShopDataModels;
using Microsoft.EntityFrameworkCore;


namespace ComputersShopDatabaseImplement.Implements
{
    public class ShopStorage : IShopStorage
    {
        public ShopViewModel? Delete(ShopBindingModel model)
        {
            using var context = new ComputersShopDatabase();
            var element = context.Shops.FirstOrDefault(x => x.Id == model.Id);
            if (element != null)
            {
                context.Shops.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public ShopViewModel? GetElement(ShopSearchModel model)
        {
            if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new ComputersShopDatabase();
            return context.Shops
                .Include(x => x.Computers)
                .ThenInclude(x => x.Computer)
                .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
                ?.GetViewModel;
        }

        public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
        {
            if (string.IsNullOrEmpty(model.Name))
            {
                return new();
            }
            using var context = new ComputersShopDatabase();
            return context.Shops
                .Include(x => x.Computers)
                .ThenInclude(x => x.Computer)
                .Select(x => x.GetViewModel)
                .Where(x => x.ShopName.Contains(model.Name ?? string.Empty))
                .ToList();
        }

        public List<ShopViewModel> GetFullList()
        {
            using var context = new ComputersShopDatabase();
            return context.Shops
            .Include(x => x.Computers)
                .ThenInclude(x => x.Computer)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public ShopViewModel? Insert(ShopBindingModel model)
        {
            using var context = new ComputersShopDatabase();
            try
            {
                var newShop = Shop.Create(context, model);
                if (newShop == null)
                {
                    return null;
                }
                if (context.Shops.Any(x => x.ShopName == newShop.ShopName))
                {
                    throw new Exception("Не должно быть два магазина с одним названием");
                }
                context.Shops.Add(newShop);
                context.SaveChanges();
                return newShop.GetViewModel;
            }
            catch
            {
                throw;
            }
        }

        public ShopViewModel? Update(ShopBindingModel model)
        {
            using var context = new ComputersShopDatabase();
            var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
            if (shop == null)
            {
                return null;
            }
            try
            {
                if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id)))
                {
                    throw new Exception("Не должно быть два магазина с одним названием");
                }
                shop.Update(model);
                shop.UpdateComputers(context, model);
                context.SaveChanges();
                return shop.GetViewModel;
            }
            catch
            {
                throw;
            }
        }
        public bool SellComputers(IComputerModel model, int count)
        {
            if (model == null)
                return false;
            using var context = new ComputersShopDatabase();
            using var transaction = context.Database.BeginTransaction();
            List<ShopComputer> lst = new List<ShopComputer>();
            foreach (var el in context.ShopComputers.Where(x => x.ComputerId == model.Id))
            {
                int dif = count;
                if (el.Count < dif)
                    dif = el.Count;
                el.Count -= dif;
                count -= dif;
                if (el.Count == 0)
                {
                    lst.Add(el);
                }
                if (count == 0)
                    break;
            }
            if (count > 0)
            {
                transaction.Rollback();
                return false;
            }
            foreach (var el in lst)
            {
                context.ShopComputers.Remove(el);
            }
            context.SaveChanges();
            transaction.Commit();
            return true;
        }
    }
}
