using Microsoft.EntityFrameworkCore;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrecastConcretePlantDatabaseImplement.Implements
{
    public class ShopStorage : IShopStorage
    {
        public List<ShopViewModel> GetFullList()
        {
            using var context = new PrecastConcretePlantDataBase();
            return context.Shops
                    .Include(x => x.Reinforcedies)
                    .ThenInclude(x => x.Reinforced)
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
        {
            if (string.IsNullOrEmpty(model.Name))
            {
                return new();
            }
            using var context = new PrecastConcretePlantDataBase();
            return context.Shops
                    .Include(x => x.Reinforcedies)
                    .ThenInclude(x => x.Reinforced)
                    .Where(x => x.Name.Contains(model.Name))
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

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

        public ShopViewModel? Insert(ShopBindingModel model)
        {
            using var context = new PrecastConcretePlantDataBase();
            var newShop = Shop.Create(context, model);
            if (newShop == null)
            {
                return null;
            }
            context.Shops.Add(newShop);
            context.SaveChanges();
            return newShop.GetViewModel;
        }

        public ShopViewModel? Update(ShopBindingModel model)
        {
            using var context = new PrecastConcretePlantDataBase();
            var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
            if (shop == null)
            {
                return null;
            }
            shop.Update(model);
            shop.UpdateReinforcedies(context, model);
            context.SaveChanges();
            return shop.GetViewModel;
        }

        public ShopViewModel? Delete(ShopBindingModel model)
        {
            using var context = new PrecastConcretePlantDataBase();
            var element = context.Shops
                .Include(x => x.Reinforcedies)
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Shops.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public bool SellReinforced(IReinforcedModel model, int count)
        {
            using var context = new PrecastConcretePlantDataBase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var shops = context.ShopReinforcedies
                   .Include(x => x.Shop)
                   .ToList()
                   .Where(rec => rec.ReinforcedId == model.Id);
                if (shops == null)
                {
                    return false;
                }
                foreach (var shop in shops)
                {
                    if (shop.Count < count)
                    {
                        shop.Count = 0;
                        count -= shop.Count;
                    }
                    else
                    {
                        shop.Count = shop.Count - count;
                        count -= count;
                    }
                    if (count == 0)
                    {

                        context.SaveChanges();
                        transaction.Commit();
                        return true;
                    }
                }
                transaction.Rollback();
                return false;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

    }
}
