using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Models;
using FlowerShopFileImplement.Implements;
using FlowerShopFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopFileImplement.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.Name))
            {
                return new();
            }
            return _source.Shops
                .Where(x => x.ShopName.Contains(model.Name))
                .Select(x => x.GetViewModel)
                .ToList(); ;
        }
        public ShopViewModel? GetElement(ShopSearchModel model)
        {
            if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
            {
                return null;
            }
            return _source.Shops
                 .FirstOrDefault(x =>
                (!string.IsNullOrEmpty(model.Name) && x.ShopName == model.Name) ||
                 (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 component = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
            if (component == null)
            {
                return null;
            }
            component.Update(model);
            _source.SaveShops();
            return component.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 CheckAvailability(int flowerId, int count)
        {
            count -= _source.Shops.Select(x => x.ShopFlowers.Select(y => (y.Value.Item1.Id == flowerId ? y.Value.Item2 : 0)).Sum()).Sum();
            return count <= 0;
        }
        //логика продажи
        public bool SellFlowers(IFlowerModel model, int count)
        {
            var flower = _source.Flowers.FirstOrDefault(x => x.Id == model.Id);

            if (flower == null || !CheckAvailability(flower.Id, count))
            {
                return false;
            }

            for (int i = 0; i < _source.Shops.Count; i++)
            {
                var shop = _source.Shops[i];
                var flowers = shop.ShopFlowers;

                foreach (var flowerr in flowers.Where(x => x.Value.Item1.Id == flower.Id))
                {
                    var min = Math.Min(flowerr.Value.Item2, count);
                    flowers[flowerr.Value.Item1.Id] = (flowerr.Value.Item1, flowerr.Value.Item2 - min);
                    count -= min;

                    if (count <= 0)
                    {
                        break;
                    }
                }

                shop.Update(new ShopBindingModel
                {
                    Id = shop.Id,
                    ShopName = shop.ShopName,
                    Address = shop.Address,
                    DateOpen = shop.DateOpen,
                    MaxCapacity = shop.MaxCapacity,
                    ShopFlowers = flowers
                });
            }
            _source.SaveShops();
            return true;
        }
    }
}