using Microsoft.EntityFrameworkCore;
using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardDataBaseImplement.Models;
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace ShipyardDataBaseImplement.Implements
{
    public class ShopStorage : IShopStorage
    {
        public ShopViewModel? GetElement(ShopSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ShopName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new ShipyardDataBase();
            return context.Shops
             .Include(x => x.Ships)
             .ThenInclude(x => x.Ship)
             .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) &&
            x.ShopName == model.ShopName) ||
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }

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

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

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

        public ShopViewModel? Update(ShopBindingModel model)
        {
            using var context = new ShipyardDataBase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var shop = context.Shops.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (shop == null)
                {
                    return null;
                }
                shop.Update(model);
                context.SaveChanges();
                if (model.ShopShips != null) 
                {
                    shop.UpdateShips(context, model);
                }
                transaction.Commit();
                return shop.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public ShopViewModel? Delete(ShopBindingModel model)
        {
            using var context = new ShipyardDataBase();
            var element = context.Shops
            .Include(x => x.Ships)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Shops.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
        public bool SellShips(IShipModel model, int count)
        {
            using var context = new ShipyardDataBase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                List<Shop> shopsWithShip = context.Shops.Include(x => x.Ships).ThenInclude(x => x.Ship).Where(x => x.Ships.Any(x => x.ShipId == model.Id)).ToList();
                foreach (var shop in shopsWithShip)
                {
                    int carInShopCount = shop.ShopShips[model.Id].Item2;
                    if (count - carInShopCount >= 0)
                    {
                        count -= carInShopCount;
                        context.ShopShips.Remove(shop.Ships.FirstOrDefault(x => x.ShipId == model.Id)!);
                        shop.ShopShips.Remove(model.Id);
                    }
                    else
                    {
                        shop.ShopShips[model.Id] = (model, carInShopCount - count);
                        count = 0;
                        shop.UpdateShips(context, new()
                        {
                            Id = shop.Id,
                            ShopShips = shop.ShopShips
                        });
                    }
                    if (count == 0)
                    {
                        context.SaveChanges();
                        transaction.Commit();
                        return true;
                    }
                }
                transaction.Rollback();
                return false;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}
