/// <summary>
        /// Drops an appliance from the database and removes it from the displayed list in the UI
        /// </summary>
        /// <param name="appliance"></param>
        private async void DropAppliance(Appliance appliance)
        {
            // Check if the appliance is used in any packaged solutions
            using (var ctx = new AssistantContext())
            {
                var conflictingSolutions = ctx.PackagedSolutions.Include(a => a.ApplianceInstances)
                                           .Where(s => s.ApplianceInstances.Any(a => a.Appliance.Id == appliance.Id))
                                           .ToList();
                if (conflictingSolutions.Count > 0)
                {
                    var formattedSolutionString = string.Join("\n", conflictingSolutions.Select(x => $"- {x.Name}"));
                    await _dialogCoordinator.ShowMessageAsync(this, "Fejl",
                                                              $"Komponentet kan ikke slettes, da det findes i følgende pakkeløsninger:\n{formattedSolutionString}");

                    return;
                }
            }

            // Remove from current solution
            foreach (var existingAppliance in AppliancesInPackagedSolution.Where(a => a.Appliance == appliance))
            {
                AppliancesInPackagedSolution.Remove(existingAppliance);
            }

            // Remove from visual list of appliances
            Appliances.Remove(appliance);

            // Remove from database
            using (var ctx = new AssistantContext())
            {
                ctx.Entry(appliance).State = EntityState.Deleted;

                ctx.SaveChanges();
            }
        }
        /// <summary>
        /// Prompt the user to confirm action
        /// Remove the current packaged solution from the view
        /// Initialize new packaged solution
        /// </summary>
        private async void CreateNewPackagedSolution()
        {
            // Run message dialog and await response
            var result = await _dialogCoordinator.ShowMessageAsync(this,
                                                                   "Ny pakkeløsning",
                                                                   "Hvis du opretter en ny pakkeløsning mister du arbejdet på din nuværende pakkeløsning. Vil du fortsætte?",
                                                                   MessageDialogStyle.AffirmativeAndNegative,
                                                                   new MetroDialogSettings()
            {
                NegativeButtonText = "Afbryd"
            });

            // If negative button was pressed => return
            if (result == MessageDialogResult.Negative)
            {
                return;
            }

            // Clear the appliance list for the current packaged solution
            AppliancesInPackagedSolution.Clear();

            // Reset collections and results
            PackagedSolution       = new PackagedSolution();
            EeiResultsRoomHeating  = new EEICalculationResult();
            EeiResultsWaterHeating = new EEICalculationResult();

            // Update the EEI (=> 0)
            UpdateEei();
        }
        /// <summary>
        /// Adds an appliance to the viewed packaged solution
        /// </summary>
        /// <param name="appliance"></param>
        private void AddApplianceToPackagedSolution(ApplianceInstance appliance)
        {
            AppliancesInPackagedSolution.Add(appliance);

            // Set save state to false
            IsDataSaved = false;

            // Notify the print function of changes
            PrintEnergyLabelCmd.NotifyCanExecuteChanged();
        }
        /// <summary>
        /// Save the viewed packaged solution to the database
        /// </summary>
        private void SaveCurrentPackagedSolution()
        {
            /* IMPORTANT
             * A packaged solution should not be saved if there exists
             *  a solar collector without a container tied to it. */
            using (var ctx = new AssistantContext())
            {
                // Copy visible list of appliance instances to the list in the model
                var newApplianceInstanceList = AppliancesInPackagedSolution.ToList();

                // Mark packaged solution as added if new or modified if old
                ctx.Entry(PackagedSolution).State = PackagedSolution.Id == 0 ? EntityState.Added : EntityState.Modified;

                // Mark removed appliance instances as deleted
                if (PackagedSolution.Id > 0)
                {
                    var previousPackagedSolution = ctx.PackagedSolutions.Include(a => a.ApplianceInstances).FirstOrDefault(p => p.Id == PackagedSolution.Id);
                    previousPackagedSolution?.ApplianceInstances.ToList().ForEach(instance =>
                    {
                        if (newApplianceInstanceList.All(a => a.Id != instance.Id))
                        {
                            ctx.Entry(instance).State = EntityState.Deleted;
                        }
                    });
                }

                // Attach appliances and appliance instances to avoid duplicates
                foreach (var appInstance in newApplianceInstanceList)
                {
                    ctx.Entry(appInstance.Appliance.DataSheet).State = EntityState.Unchanged;
                    ctx.Entry(appInstance.Appliance).State           = EntityState.Unchanged;

                    ctx.Entry(appInstance).State = appInstance.Id == 0 ? EntityState.Added : EntityState.Unchanged;
                }

                // Set new appliance instance list
                PackagedSolution.ApplianceInstances = newApplianceInstanceList;

                // Set the creation date to now
                if (PackagedSolution.CreationDate == default(DateTime))
                {
                    PackagedSolution.CreationDate = DateTime.Now;
                }

                // Save database changes
                ctx.SaveChanges();
            }

            // Set save state to true and notify print function of change
            IsDataSaved = true;
            PrintEnergyLabelCmd.NotifyCanExecuteChanged();
        }
        public void LoadExistingPackagedSolution(int packagedSolutionId)
        {
            using (var ctx = new AssistantContext())
            {
                var existingPackagedSolution = ctx.PackagedSolutions.Where(p => p.Id == packagedSolutionId)
                                               .Include(s => s.ApplianceInstances.Select(i => i.Appliance.DataSheet))
                                               .FirstOrDefault();

                if (existingPackagedSolution == null)
                {
                    return;
                }

                // Copy appliances
                foreach (var appliance in existingPackagedSolution.ApplianceInstances)
                {
                    AppliancesInPackagedSolution.Add(appliance.MakeCopy());
                }
            }
        }
 private void UpdateEei()
 {
     PackagedSolution.ApplianceInstances = AppliancesInPackagedSolution.ToList();
     PackagedSolution.EnergyLabel.Clear();
     PackagedSolution.UpdateEei();
     if (PackagedSolution.EnergyLabel != null && PackagedSolution.EnergyLabel.Count > 1)
     {
         EeiResultsRoomHeating  = PackagedSolution.EnergyLabel[0];
         EeiResultsWaterHeating = PackagedSolution.EnergyLabel[1];
     }
     else if (PackagedSolution.EnergyLabel != null && PackagedSolution.EnergyLabel.Count == 1)
     {
         EeiResultsRoomHeating  = PackagedSolution.EnergyLabel?[0];
         EeiResultsWaterHeating = null;
     }
     else
     {
         EeiResultsRoomHeating  = null;
         EeiResultsWaterHeating = null;
     }
 }