public SelectPartsOfPrinterToImport(string settingsFilePath, PrinterSettingsLayer destinationLayer, string sectionName = null) :
            base(unlocalizedTextForTitle: "Import Wizard")
        {
            this.isMergeIntoUserLayer = destinationLayer == ActiveSliceSettings.Instance.UserLayer;
            this.destinationLayer     = destinationLayer;
            this.sectionName          = sectionName;

            // TODO: Need to handle load failures for import attempts
            settingsToImport = PrinterSettings.LoadFile(settingsFilePath);

            this.headerLabel.Text = "Select What to Import".Localize();

            this.settingsFilePath = settingsFilePath;

            var scrollWindow = new ScrollableWidget()
            {
                AutoScroll = true,
                HAnchor    = HAnchor.ParentLeftRight,
                VAnchor    = VAnchor.ParentBottomTop,
            };

            scrollWindow.ScrollArea.HAnchor = HAnchor.ParentLeftRight;
            contentRow.AddChild(scrollWindow);

            var container = new FlowLayoutWidget(FlowDirection.TopToBottom)
            {
                HAnchor = HAnchor.ParentLeftRight,
            };

            scrollWindow.AddChild(container);

            if (isMergeIntoUserLayer)
            {
                container.AddChild(new WrappedTextWidget(importMessage, textColor: ActiveTheme.Instance.PrimaryTextColor));
            }

            // add in the check boxes to select what to import
            container.AddChild(new TextWidget("Main Settings:")
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                Margin    = new BorderDouble(0, 3, 0, isMergeIntoUserLayer ? 10 : 0),
            });

            var mainProfileRadioButton = new RadioButton("Printer Profile")
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                Margin    = new BorderDouble(5, 0),
                HAnchor   = HAnchor.ParentLeft,
                Checked   = true,
            };

            container.AddChild(mainProfileRadioButton);

            if (settingsToImport.QualityLayers.Count > 0)
            {
                container.AddChild(new TextWidget("Quality Presets:")
                {
                    TextColor = ActiveTheme.Instance.PrimaryTextColor,
                    Margin    = new BorderDouble(0, 3, 0, 15),
                });

                int buttonIndex = 0;
                foreach (var qualitySetting in settingsToImport.QualityLayers)
                {
                    RadioButton qualityButton = new RadioButton(qualitySetting.Name)
                    {
                        TextColor = ActiveTheme.Instance.PrimaryTextColor,
                        Margin    = new BorderDouble(5, 0, 0, 0),
                        HAnchor   = HAnchor.ParentLeft,
                    };
                    container.AddChild(qualityButton);

                    int localButtonIndex = buttonIndex;
                    qualityButton.CheckedStateChanged += (s, e) =>
                    {
                        if (qualityButton.Checked)
                        {
                            selectedQuality = localButtonIndex;
                        }
                        else
                        {
                            selectedQuality = -1;
                        }
                    };

                    buttonIndex++;
                }
            }

            if (settingsToImport.MaterialLayers.Count > 0)
            {
                container.AddChild(new TextWidget("Material Presets:")
                {
                    TextColor = ActiveTheme.Instance.PrimaryTextColor,
                    Margin    = new BorderDouble(0, 3, 0, 15),
                });

                int buttonIndex = 0;
                foreach (var materialSetting in settingsToImport.MaterialLayers)
                {
                    RadioButton materialButton = new RadioButton(materialSetting.Name)
                    {
                        TextColor = ActiveTheme.Instance.PrimaryTextColor,
                        Margin    = new BorderDouble(5, 0),
                        HAnchor   = HAnchor.ParentLeft,
                    };

                    container.AddChild(materialButton);

                    int localButtonIndex = buttonIndex;
                    materialButton.CheckedStateChanged += (s, e) =>
                    {
                        if (materialButton.Checked)
                        {
                            selectedMaterial = localButtonIndex;
                        }
                        else
                        {
                            selectedMaterial = -1;
                        }
                    };

                    buttonIndex++;
                }
            }

            var mergeButtonTitle = this.isMergeIntoUserLayer ? "Merge".Localize() : "Import".Localize();
            var mergeButton      = textImageButtonFactory.Generate(mergeButtonTitle);

            mergeButton.Name   = "Merge Profile";
            mergeButton.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                bool copyName = false;
                PrinterSettingsLayer sourceLayer = null;
                if (selectedMaterial > -1)
                {
                    sourceLayer = settingsToImport.MaterialLayers[selectedMaterial];
                    copyName    = true;
                }
                else if (selectedQuality > -1)
                {
                    sourceLayer = settingsToImport.QualityLayers[selectedQuality];
                    copyName    = true;
                }

                List <PrinterSettingsLayer> sourceFilter;

                if (selectedQuality == -1 && selectedMaterial == -1)
                {
                    sourceFilter = new List <PrinterSettingsLayer>()
                    {
                        settingsToImport.OemLayer,
                        settingsToImport.UserLayer
                    };
                }
                else
                {
                    sourceFilter = new List <PrinterSettingsLayer>()
                    {
                        sourceLayer
                    };
                }

                ActiveSliceSettings.Instance.Merge(destinationLayer, settingsToImport, sourceFilter, copyName);

                this.Parents <SystemWindow>().FirstOrDefault()?.CloseOnIdle();
            });

            footerRow.AddChild(mergeButton);

            footerRow.AddChild(new HorizontalSpacer());
            footerRow.AddChild(cancelButton);

            if (settingsToImport.QualityLayers.Count == 0 && settingsToImport.MaterialLayers.Count == 0)
            {
                // Only main setting so don't ask what to merge just do it.
                UiThread.RunOnIdle(() =>
                {
                    var sourceFilter = new List <PrinterSettingsLayer>()
                    {
                        settingsToImport.OemLayer ?? new PrinterSettingsLayer(),
                        settingsToImport.UserLayer ?? new PrinterSettingsLayer()
                    };

                    ActiveSliceSettings.Instance.Merge(destinationLayer, settingsToImport, sourceFilter, false);
                    UiThread.RunOnIdle(ApplicationController.Instance.ReloadAdvancedControlsPanel);

                    string successMessage = importPrinterSuccessMessage.FormatWith(Path.GetFileNameWithoutExtension(settingsFilePath));
                    if (!isMergeIntoUserLayer)
                    {
                        string sourceName = isMergeIntoUserLayer ? Path.GetFileNameWithoutExtension(settingsFilePath) : destinationLayer[SettingsKey.layer_name];
                        string importSettingSuccessMessage = "You have successfully imported a new {1} setting. You can find '{0}' in your list of {1} settings.".Localize();
                        successMessage = importSettingSuccessMessage.FormatWith(sourceName, sectionName);
                    }

                    WizardWindow.ChangeToPage(new ImportSucceeded(successMessage)
                    {
                        WizardWindow = this.WizardWindow,
                    });
                });
            }
        }
        public ImportSettingsPage(string settingsFilePath, PrinterConfig printer)
        {
            this.WindowTitle = "Import Wizard";
            this.HeaderText  = "Select What to Import".Localize();

            // TODO: Need to handle load failures for import attempts
            var settingsToImport = PrinterSettings.LoadFile(settingsFilePath);

            // if there are no settings to import
            if (settingsToImport.QualityLayers.Count == 0 && settingsToImport.MaterialLayers.Count == 0)
            {
                // Only main setting so don't ask what to merge just do it.
                UiThread.RunOnIdle(() =>
                {
                    DisplayFailedToImportMessage(settingsFilePath);
                    this.Parents <SystemWindow>().First().Close();
                });
            }

            this.settingsFilePath = settingsFilePath;

            var scrollWindow = new ScrollableWidget()
            {
                AutoScroll = true,
                HAnchor    = HAnchor.Stretch,
                VAnchor    = VAnchor.Stretch,
            };

            scrollWindow.ScrollArea.HAnchor = HAnchor.Stretch;
            contentRow.AddChild(scrollWindow);

            var container = new FlowLayoutWidget(FlowDirection.TopToBottom)
            {
                HAnchor = HAnchor.Stretch,
            };

            scrollWindow.AddChild(container);

            if (settingsToImport.QualityLayers.Count > 0)
            {
                container.AddChild(new TextWidget("Quality Presets:")
                {
                    TextColor = theme.TextColor,
                    Margin    = new BorderDouble(0, 3),
                });

                int buttonIndex = 0;
                foreach (var qualitySetting in settingsToImport.QualityLayers)
                {
                    var qualityButton = new RadioButton(string.IsNullOrEmpty(qualitySetting.Name) ? "no name" : qualitySetting.Name)
                    {
                        TextColor = theme.TextColor,
                        Margin    = new BorderDouble(5, 0, 0, 0),
                        HAnchor   = HAnchor.Left,
                    };
                    container.AddChild(qualityButton);

                    int localButtonIndex = buttonIndex;
                    qualityButton.CheckedStateChanged += (s, e) =>
                    {
                        if (qualityButton.Checked)
                        {
                            selectedQuality = localButtonIndex;
                        }
                        else
                        {
                            selectedQuality = -1;
                        }
                    };

                    buttonIndex++;
                }
            }

            if (settingsToImport.MaterialLayers.Count > 0)
            {
                container.AddChild(new TextWidget("Material Presets:")
                {
                    TextColor = theme.TextColor,
                    Margin    = new BorderDouble(0, 3, 0, 15),
                });

                int buttonIndex = 0;
                foreach (var materialSetting in settingsToImport.MaterialLayers)
                {
                    var materialButton = new RadioButton(string.IsNullOrEmpty(materialSetting.Name) ? "no name" : materialSetting.Name)
                    {
                        TextColor = theme.TextColor,
                        Margin    = new BorderDouble(5, 0),
                        HAnchor   = HAnchor.Left,
                    };

                    container.AddChild(materialButton);

                    int localButtonIndex = buttonIndex;
                    materialButton.CheckedStateChanged += (s, e) =>
                    {
                        if (materialButton.Checked)
                        {
                            selectedMaterial = localButtonIndex;
                        }
                        else
                        {
                            selectedMaterial = -1;
                        }
                    };

                    buttonIndex++;
                }
            }

            var mergeButton = theme.CreateDialogButton("Import".Localize());

            mergeButton.Name   = "Merge Profile";
            mergeButton.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                bool copyName = false;
                PrinterSettingsLayer sourceLayer = null;
                bool destIsMaterial = true;
                if (selectedMaterial > -1)
                {
                    sourceLayer = settingsToImport.MaterialLayers[selectedMaterial];
                    copyName    = true;
                }
                else if (selectedQuality > -1)
                {
                    destIsMaterial = false;
                    sourceLayer    = settingsToImport.QualityLayers[selectedQuality];
                    copyName       = true;
                }

                List <PrinterSettingsLayer> sourceFilter;

                if (selectedQuality == -1 && selectedMaterial == -1)
                {
                    sourceFilter = new List <PrinterSettingsLayer>()
                    {
                        settingsToImport.OemLayer,
                        settingsToImport.UserLayer
                    };
                }
                else
                {
                    sourceFilter = new List <PrinterSettingsLayer>()
                    {
                        sourceLayer
                    };
                }

                if (File.Exists(settingsFilePath))
                {
                    if (Path.GetExtension(settingsFilePath).ToLower() == ProfileManager.ProfileExtension)
                    {
                        var printerSettingsLayer = new PrinterSettingsLayer();
                        printer.Settings.Merge(printerSettingsLayer, settingsToImport, sourceFilter, copyName);

                        var layerName = (printerSettingsLayer.ContainsKey(SettingsKey.layer_name)) ? printerSettingsLayer[SettingsKey.layer_name] : "none";

                        string sectionName = destIsMaterial ? "Material".Localize() : "Quality".Localize();

                        string importSettingSuccessMessage = string.Format("You have successfully imported a new {0} setting. You can find '{1}' in your list of {0} settings.".Localize(), sectionName, layerName);

                        DialogWindow.ChangeToPage(
                            new ImportSucceeded(importSettingSuccessMessage)
                        {
                            DialogWindow = this.DialogWindow,
                        });

                        if (destIsMaterial)
                        {
                            printer.Settings.MaterialLayers.Add(printerSettingsLayer);
                        }
                        else
                        {
                            printer.Settings.QualityLayers.Add(printerSettingsLayer);
                        }
                    }
                    else
                    {
                        // Inform of unexpected extension type
                        StyledMessageBox.ShowMessageBox(
                            "Oops! Unable to recognize settings file '{0}'.".Localize().FormatWith(Path.GetFileName(settingsFilePath)),
                            "Unable to Import".Localize());
                    }
                }
            });

            this.AddPageAction(mergeButton);
        }
        public SelectPartsOfPrinterToImport(string settingsFilePath, PrinterSettingsLayer destinationLayer, string sectionName = null) :
            base(unlocalizedTextForTitle: "Import Wizard")
        {
            this.isMergeIntoUserLayer = destinationLayer == ActiveSliceSettings.Instance.UserLayer;
            this.destinationLayer     = destinationLayer;
            this.sectionName          = sectionName;

            // TODO: Need to handle load failures for import attempts
            settingsToImport = PrinterSettings.LoadFile(settingsFilePath);

            this.headerLabel.Text = "Select What to Import".Localize();

            this.settingsFilePath = settingsFilePath;

            var scrollWindow = new ScrollableWidget()
            {
                AutoScroll = true,
                HAnchor    = HAnchor.ParentLeftRight,
                VAnchor    = VAnchor.ParentBottomTop,
            };

            scrollWindow.ScrollArea.HAnchor = HAnchor.ParentLeftRight;
            contentRow.AddChild(scrollWindow);

            var container = new FlowLayoutWidget(FlowDirection.TopToBottom)
            {
                HAnchor = HAnchor.ParentLeftRight,
            };

            scrollWindow.AddChild(container);

            if (isMergeIntoUserLayer)
            {
                container.AddChild(new WrappedTextWidget(importMessage, textColor: ActiveTheme.Instance.PrimaryTextColor));
            }

            // add in the check boxes to select what to import
            container.AddChild(new TextWidget("Main Settings:")
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                Margin    = new BorderDouble(0, 3, 0, isMergeIntoUserLayer ? 10 : 0),
            });

            var mainProfileRadioButton = new RadioButton("Printer Profile")
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                Margin    = new BorderDouble(5, 0),
                HAnchor   = HAnchor.ParentLeft,
                Checked   = true,
            };

            container.AddChild(mainProfileRadioButton);

            if (settingsToImport.QualityLayers.Count > 0)
            {
                container.AddChild(new TextWidget("Quality Presets:")
                {
                    TextColor = ActiveTheme.Instance.PrimaryTextColor,
                    Margin    = new BorderDouble(0, 3, 0, 15),
                });

                int buttonIndex = 0;
                foreach (var qualitySetting in settingsToImport.QualityLayers)
                {
                    RadioButton qualityButton = new RadioButton(qualitySetting.Name)
                    {
                        TextColor = ActiveTheme.Instance.PrimaryTextColor,
                        Margin    = new BorderDouble(5, 0, 0, 0),
                        HAnchor   = HAnchor.ParentLeft,
                    };
                    container.AddChild(qualityButton);

                    int localButtonIndex = buttonIndex;
                    qualityButton.CheckedStateChanged += (s, e) =>
                    {
                        if (qualityButton.Checked)
                        {
                            selectedQuality = localButtonIndex;
                        }
                        else
                        {
                            selectedQuality = -1;
                        }
                    };

                    buttonIndex++;
                }
            }

            if (settingsToImport.MaterialLayers.Count > 0)
            {
                container.AddChild(new TextWidget("Material Presets:")
                {
                    TextColor = ActiveTheme.Instance.PrimaryTextColor,
                    Margin    = new BorderDouble(0, 3, 0, 15),
                });

                int buttonIndex = 0;
                foreach (var materialSetting in settingsToImport.MaterialLayers)
                {
                    RadioButton materialButton = new RadioButton(materialSetting.Name)
                    {
                        TextColor = ActiveTheme.Instance.PrimaryTextColor,
                        Margin    = new BorderDouble(5, 0),
                        HAnchor   = HAnchor.ParentLeft,
                    };

                    container.AddChild(materialButton);

                    int localButtonIndex = buttonIndex;
                    materialButton.CheckedStateChanged += (s, e) =>
                    {
                        if (materialButton.Checked)
                        {
                            selectedMaterial = localButtonIndex;
                        }
                        else
                        {
                            selectedMaterial = -1;
                        }
                    };

                    buttonIndex++;
                }
            }

            var mergeButtonTitle = this.isMergeIntoUserLayer ? "Merge".Localize() : "Import".Localize();
            var mergeButton      = textImageButtonFactory.Generate(mergeButtonTitle);

            mergeButton.Name   = "Merge Profile";
            mergeButton.Click += (s, e) => UiThread.RunOnIdle(Merge);
            footerRow.AddChild(mergeButton);

            footerRow.AddChild(new HorizontalSpacer());
            footerRow.AddChild(cancelButton);

            if (settingsToImport.QualityLayers.Count == 0 && settingsToImport.MaterialLayers.Count == 0)
            {
                // Only main setting so don't ask what to merge just do it.
                UiThread.RunOnIdle(Merge);
            }
        }