using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDataModels.Models;
using IceCreamShopFileImplement.Models;
using System.Collections.Generic;

namespace IceCreamShopFileImplement.Implements
{
    public class ShopStorage : IShopStorage
    {
        private readonly DataFileSingleton source;

        public ShopStorage()
        {
            source = DataFileSingleton.GetInstance();
        }

        public List<ShopViewModel> GetFullList()
        {
            return source.Shops
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ShopName))
            {
                return new();
            }
            return source.Shops
                    .Where(x => x.ShopName.Contains(model.ShopName))
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public ShopViewModel? GetElement(ShopSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
            {
                return null;
            }
            return source.Shops
                    .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
                                        (model.Id.HasValue && x.Id == model.Id))
                    ?.GetViewModel;
        }

        public ShopViewModel? Insert(ShopBindingModel model)
        {
            model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1;
            var newShop = Shop.Create(model);
            if (newShop == null)
            {
                return null;
            }
            source.Shops.Add(newShop);
            source.SaveShops();
            return newShop.GetViewModel;
        }

        public ShopViewModel? Update(ShopBindingModel model)
        {
            var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
            if (shop == null)
            {
                return null;
            }
            shop.Update(model);
            source.SaveShops();
            return shop.GetViewModel;
        }

        public ShopViewModel? Delete(ShopBindingModel model)
        {
            var element = source.Shops.FirstOrDefault(x => x.Id == model.Id);
            if (element != null)
            {
                source.Shops.Remove(element);
                source.SaveShops();
                return element.GetViewModel;
            }
            return null;
        }

        public bool MakeSale(IIceCreamModel model, int count)
        {
            var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id);
            int countInShops = source.Shops.SelectMany(x => x.ShopIceCreams).Sum(y => y.Key == model.Id ? y.Value.Item2 : 0);

            if (iceCream == null || countInShops < count)
            {
                return false;
            }

            foreach (var shop in source.Shops)
            {
                var shopIceCreams = shop.ShopIceCreams.Where(x => x.Key == model.Id);
                if (shopIceCreams.Any())
                {
                    var shopIceCream = shopIceCreams.First();
                    int min = Math.Min(shopIceCream.Value.Item2, count);
                    if (min == shopIceCream.Value.Item2) 
                    {
                        shop.ShopIceCreams.Remove(shopIceCream.Key);
                    } 
                    else
                    {
                        shop.ShopIceCreams[shopIceCream.Key] = (shopIceCream.Value.Item1, shopIceCream.Value.Item2 - min);
                    }
                    shop.Update(new ShopBindingModel
                    {
                        Id = shop.Id,
                        ShopName = shop.ShopName,
                        Address = shop.Address,
                        DateOpening = shop.DateOpening,
                        ShopIceCreams = shop.ShopIceCreams,
                        IceCreamsMaximum = shop.IceCreamsMaximum
                    });
                    count -= min;
                    if (count <= 0)
                    {
                        break;
                    }
                }
            }
            source.SaveShops();
            return true;
        }
    }
}
