using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationListImplement.Models;

namespace SoftwareInstallationListImplement.Implements
{
    public class PackageStorage : IPackageStorage
    {
        private readonly DataListSingleton _source;
        public PackageStorage()
        {
            _source = DataListSingleton.GetInstance();
        }

        public PackageViewModel? Delete(PackageBindingModel model)
        {
            for (int i = 0; i < _source.Packages.Count; ++i)
            {
                if (_source.Packages[i].Id == model.Id)
                {
                    var element = _source.Packages[i];
                    _source.Packages.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }

        public PackageViewModel? GetElement(PackageSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PackageName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var package in _source.Packages)
            {
                if ((!string.IsNullOrEmpty(model.PackageName) &&
                package.PackageName == model.PackageName) ||
                (model.Id.HasValue && package.Id == model.Id))
                {
                    return package.GetViewModel;
                }
            }
            return null;
        }

        public List<PackageViewModel> GetFilteredList(PackageSearchModel model)
        {
            var result = new List<PackageViewModel>();
            if (string.IsNullOrEmpty(model.PackageName))
            {
                return result;
            }
            foreach (var package in _source.Packages)
            {
                if (package.PackageName.Contains(model.PackageName ?? string.Empty))
                {
                    result.Add(package.GetViewModel);
                }
            }
            return result;
        }

        public List<PackageViewModel> GetFullList()
        {
            var result = new List<PackageViewModel>();
            foreach (var package in _source.Packages)
            {
                result.Add(package.GetViewModel);
            }
            return result;
        }

        public PackageViewModel? Insert(PackageBindingModel model)
        {
            model.Id = 1;
            foreach (var package in _source.Packages)
            {
                if (model.Id <= package.Id)
                {
                    model.Id = package.Id + 1;
                }
            }
            var newPackage = Package.Create(model);
            if (newPackage == null)
            {
                return null;
            }
            _source.Packages.Add(newPackage);
            return newPackage.GetViewModel;
        }

        public PackageViewModel? Update(PackageBindingModel model)
        {
            foreach (var package in _source.Packages)
            {
                if (package.Id == model.Id)
                {
                    package.Update(model);
                    return package.GetViewModel;
                }
            }
            return null;
        }
    }
}