public static PackageListViewModel Convert(ILocalPackageCache cache, RunningInstallationTaskList runningTasks, IInstalledPackageArchive installPackageArchive, CompletedInstallationTaskList completedTasks)
        {
            var model = new PackageListViewModel();

            foreach(var packageId in cache.AvailablePackages)
            {
                var package = new LocalPackageInformation
                                  {
                                      PackageId = packageId,
                                      LatestAvailableVersion = cache.GetLatestVersion(packageId).Version.ToString()
                                  };

                var installedPackage = installPackageArchive.GetCurrentInstalledVersion(packageId);
                package.InstalledVersion = installedPackage == null ? "0.0.0.0" : installedPackage.Version.ToString();

                package.LastInstallationTask =
                    completedTasks
                        .Where(t => t.PackageId == packageId).OrderByDescending(t => t.LogFileName).
                        FirstOrDefault();

                package.CurrentTask = runningTasks.Count > 0 ? runningTasks
                           .Where(t => t.PackageId == packageId)
                           .Select(t =>
                                       {
                                           var lastOrDefault = t.ProgressReports.LastOrDefault();
                                           return lastOrDefault != null ? new InstallTaskViewModel
                                                                               {
                                                                                   Messages = t.ProgressReports.Select(pr => pr.Message).ToArray(),
                                                                                   Status = Enum.GetName(typeof(TaskStatus), t.Task.Status),
                                                                                   PackageId = t.PackageId,
                                                                                   Version = t.Version,
                                                                                   LastMessage = t.ProgressReports.Count > 0 ? lastOrDefault.Message : ""
                                                                               } : null;
                                       }).FirstOrDefault()
                           : null;

                package.AvailableVersions = cache.AvailablePackageVersions(packageId).ToList();
                model.Packages.Add(package);
            }

            model.CurrentTasks = runningTasks
                .Select(t =>
                            {
                                var progressReport = t.ProgressReports.LastOrDefault();
                                return progressReport != null
                                           ? new InstallTaskViewModel
                                                 {
                                                     Messages = t.ProgressReports.Select(pr => pr.Message).ToArray(),
                                                     Status = Enum.GetName(typeof (TaskStatus), t.Task.Status),
                                                     PackageId = t.PackageId,
                                                     Version = t.Version,
                                                     LastMessage =
                                                         t.ProgressReports.Count > 0 ? progressReport.Message : ""
                                                 }
                                           : null;
                            }).ToList();

            model.AvailableVersions =
                cache.AllCachedPackages().Select(p => p.Version.ToString()).Distinct().OrderByDescending(s => s);

            return model;
        }
        public static PackageListViewModel Convert(ILocalPackageCache cache, RunningInstallationTaskList runningTasks, 
            IInstalledPackageArchive installPackageArchive, CompletedInstallationTaskList completedTasks,
            IAgentSettings agentSettings, IPackagesList allPackagesList)
        {
            var model = new PackageListViewModel();

            var packagesById = allPackagesList.GetWatched().GroupBy(p => p.Id);
            foreach (var package in packagesById)
            {
                var packageInfo = new LocalPackageInformation
                                  {
                                      PackageId = package.Key
                                  };
                var latestVersion = package.OrderByDescending(p=>p.Version).FirstOrDefault();
                if (latestVersion != null)
                    packageInfo.LatestAvailableVersion = latestVersion.Version.ToString();

                var installedPackage = installPackageArchive.GetCurrentInstalledVersion(package.Key);
                packageInfo.InstalledVersion = installedPackage == null ? "0.0.0.0" : installedPackage.Version.ToString();

                /*packageInfo.LastInstallationTask =
                    completedTasks
                        .Where(t => t.PackageId == package.Key).OrderByDescending(t => t.LogFileName)
                        .FirstOrDefault();*/

                packageInfo.CurrentTask = runningTasks.Count > 0 ? runningTasks
                           .Where(t => t.PackageId == package.Key)
                           .Select(t =>
                                       {
                                           var lastOrDefault = t.ProgressReports.LastOrDefault();
                                           return lastOrDefault != null ? new InstallTaskViewModel
                                                                               {
                                                                                   Messages = t.ProgressReports.Select(pr => pr.Message).ToArray(),
                                                                                   Status = Enum.GetName(typeof(TaskStatus), t.Task.Status),
                                                                                   PackageId = t.PackageId,
                                                                                   Version = t.Version,
                                                                                   LastMessage = t.ProgressReports.Count > 0 ? lastOrDefault.Message : ""
                                                                               } : null;
                                       }).FirstOrDefault()
                           : null;

                packageInfo.AvailableVersions = package.OrderByDescending(x => x.Version).Select(x => x.Version.ToString()).ToList();

                if (latestVersion != null)
                {

                    packageInfo.Tags = latestVersion.Tags.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var tag in packageInfo.Tags)
                    {
                        if (!model.Tags.Any(t => t.Equals(tag.ToLower())))
                        {
                            model.Tags.Add(tag.ToLower());
                        }
                    }
                }
                else
                {
                    packageInfo.Tags = new string[0];
                }
                model.Packages.Add(packageInfo);
            }

            model.CurrentTasks = runningTasks
                .Select(t =>
                            {
                                var progressReport = t.ProgressReports.LastOrDefault();
                                return progressReport != null
                                           ? new InstallTaskViewModel
                                                 {
                                                     Messages = t.ProgressReports.Select(pr => pr.Message).ToArray(),
                                                     Status = Enum.GetName(typeof (TaskStatus), t.Task.Status),
                                                     PackageId = t.PackageId,
                                                     Version = t.Version,
                                                     LastMessage =
                                                         t.ProgressReports.Count > 0 ? progressReport.Message : ""
                                                 }
                                           : null;
                            }).ToList();

            model.AvailableVersions =
                allPackagesList.Select(p => p.Version.ToString()).Distinct().OrderByDescending(s => s);

            model.NugetRepository = agentSettings.NuGetRepository;
            return model;
        }