using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;
using BarListImplement.Models;

namespace BarListImplement.Implements
{
    public class CocktailStorage : ICocktailStorage
    {
        private readonly DataListSingleton _source;

        public CocktailStorage()
        {
            _source = DataListSingleton.GetInstance();
        }

        public List<CocktailViewModel> GetFullList()
        {
            var Result = new List<CocktailViewModel>();

            foreach (var Cocktail in _source.Cocktails)
            {
                Result.Add(Cocktail.GetViewModel);
            }

            return Result;
        }

        public List<CocktailViewModel> GetFilteredList(CocktailSearchModel Model)
        {
            var Result = new List<CocktailViewModel>();

            if (string.IsNullOrEmpty(Model.CocktailName))
                return Result;

            foreach (var Cocktail in _source.Cocktails)
            {
                if (Cocktail.CocktailName.Contains(Model.CocktailName))
                {
                    Result.Add(Cocktail.GetViewModel);
                }
            }

            return Result;
        }

        public CocktailViewModel? GetElement(CocktailSearchModel Model)
        {
            if (string.IsNullOrEmpty(Model.CocktailName) && !Model.Id.HasValue)
                return null;

            foreach (var Cocktail in _source.Cocktails)
            {
                if ((!string.IsNullOrEmpty(Model.CocktailName)
                    && Cocktail.CocktailName == Model.CocktailName)
                    || (Model.Id.HasValue && Cocktail.Id == Model.Id))
                {
                    return Cocktail.GetViewModel;
                }
            }

            return null;
        }

        public CocktailViewModel? Insert(CocktailBindingModel Model)
        {
            Model.Id = 1;
            foreach (var Cocktail in _source.Cocktails)
            {
                if (Model.Id <= Cocktail.Id)
                {
                    Model.Id = Cocktail.Id + 1;
                }
            }

            var NewCocktail = Cocktail.Create(Model);

            if (NewCocktail == null)
                return null;

            _source.Cocktails.Add(NewCocktail);
            return NewCocktail.GetViewModel;
        }

        public CocktailViewModel? Update(CocktailBindingModel Model)
        {
            foreach (var Cocktail in _source.Cocktails)
            {
                if (Cocktail.Id == Model.Id)
                {
                    Cocktail.Update(Model);
                    return Cocktail.GetViewModel;
                }
            }

            return null;
        }

        public CocktailViewModel? Delete(CocktailBindingModel Model)
        {
            for (int i = 0; i < _source.Cocktails.Count; ++i)
            {
                if (_source.Cocktails[i].Id == Model.Id)
                {
                    var Element = _source.Cocktails[i];
                    _source.Cocktails.RemoveAt(i);

                    return Element.GetViewModel;
                }
            }

            return null;
        }
    }
}
