private void PopulateCertificateList(
            IEnumerable <CertificateSelector.CertificateSummary> certsToInstall,
            IEnumerable <string> certsToUninstall,
            List <CertificatesDataContract.CertificateInfo> desiredList)
        {
            if (desiredList == null)
            {
                return;
            }

            if (certsToInstall != null)
            {
                foreach (CertificateSelector.CertificateSummary certificateSummary in certsToInstall)
                {
                    CertificatesDataContract.CertificateInfo certificateInfo = new CertificatesDataContract.CertificateInfo();
                    certificateInfo.Hash            = certificateSummary.Hash;
                    certificateInfo.StorageFileName = certificateSummary.StorageFileName;
                    certificateInfo.State           = CertificatesDataContract.JsonStateInstalled;
                    desiredList.Add(certificateInfo);
                }
            }

            if (certsToUninstall != null)
            {
                foreach (string hash in certsToUninstall)
                {
                    CertificatesDataContract.CertificateInfo certificateInfo = new CertificatesDataContract.CertificateInfo();
                    certificateInfo.Hash            = hash;
                    certificateInfo.StorageFileName = "";
                    certificateInfo.State           = CertificatesDataContract.JsonStateUninstalled;
                    desiredList.Add(certificateInfo);
                }
            }
        }
        /// <summary>
        /// Uninstall a certificate via device twin.
        /// </summary>
        private async void UninstallCertButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            if (CertHashUninstallInput.Text.Length == 0)
            {
                _mainPage.ShowDialogAsync("Invaid Input", "Please enter all fields to uninstall certificate");
                return;
            }

            CertificatesDataContract.CertificateInfo certificateInfo = new CertificatesDataContract.CertificateInfo();
            certificateInfo.Hash  = CertHashUninstallInput.Text;
            certificateInfo.State = CertificatesDataContract.JsonStateUninstalled;

            CertificatesDataContract.DesiredProperties certDesiredProperties = new CertificatesDataContract.DesiredProperties();
            switch (CertPathUninstallCombobox.SelectedValue)
            {
            case CertificateStore.rootCATrustedCertificates_Root:
                certDesiredProperties.rootCATrustedCertificates_Root.Add(certificateInfo);
                break;

            case CertificateStore.rootCATrustedCertificates_CA:
                certDesiredProperties.rootCATrustedCertificates_CA.Add(certificateInfo);
                break;

            case CertificateStore.rootCATrustedCertificates_TrustedPublisher:
                certDesiredProperties.rootCATrustedCertificates_TrustedPublisher.Add(certificateInfo);
                break;

            case CertificateStore.rootCATrustedCertificates_TrustedPeople:
                certDesiredProperties.rootCATrustedCertificates_TrustedPeople.Add(certificateInfo);
                break;

            case CertificateStore.certificateStore_CA_System:
                certDesiredProperties.certificateStore_CA_System.Add(certificateInfo);
                break;

            case CertificateStore.certificateStore_Root_System:
                certDesiredProperties.certificateStore_Root_System.Add(certificateInfo);
                break;

            case CertificateStore.certificateStore_My_User:
                certDesiredProperties.certificateStore_My_User.Add(certificateInfo);
                break;

            case CertificateStore.certificateStore_My_System:
                certDesiredProperties.certificateStore_My_System.Add(certificateInfo);
                break;

            default:
                break;
            }

            string refreshingValue = "\"refreshing\"";
            string finalValue      = "{" + certDesiredProperties.ToJsonString() + "}";
            await _mainPage.UpdateTwinData(refreshingValue, finalValue);
        }
        /// <summary>
        /// Install a certificate via device twin.
        /// </summary>
        private async void InstallCertButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            if (App.STORAGECONNSTRING.Length == 0)
            {
                _mainPage.ShowDialogAsync("Missing Connection String", "Please enter the Storage Connection String in Settings");
                return;
            }

            string certHashInstall = CertHashInstallInput.Text;
            string certFileName    = CertFileNameInput.Text;

            if (certHashInstall.Length == 0 || certFileName.Length == 0)
            {
                _mainPage.ShowDialogAsync("Invaid Input", "Please enter all fields to Install certificate");
                return;
            }

            ExternalStorageDataContract.DesiredProperties desiredProperties = new ExternalStorageDataContract.DesiredProperties();
            desiredProperties.connectionString = App.STORAGECONNSTRING;

            CertificatesDataContract.CertificateInfo certificateInfo = new CertificatesDataContract.CertificateInfo();
            certificateInfo.Hash            = certHashInstall;
            certificateInfo.StorageFileName = certFileName;
            certificateInfo.State           = CertificatesDataContract.JsonStateInstalled;

            CertificatesDataContract.DesiredProperties certDesiredProperties = new CertificatesDataContract.DesiredProperties();
            switch (CertPathInstallCombobox.SelectedValue)
            {
            case CertificateStore.rootCATrustedCertificates_Root:
                certDesiredProperties.rootCATrustedCertificates_Root.Add(certificateInfo);
                break;

            case CertificateStore.rootCATrustedCertificates_CA:
                certDesiredProperties.rootCATrustedCertificates_CA.Add(certificateInfo);
                break;

            case CertificateStore.rootCATrustedCertificates_TrustedPublisher:
                certDesiredProperties.rootCATrustedCertificates_TrustedPublisher.Add(certificateInfo);
                break;

            case CertificateStore.rootCATrustedCertificates_TrustedPeople:
                certDesiredProperties.rootCATrustedCertificates_TrustedPeople.Add(certificateInfo);
                break;

            case CertificateStore.certificateStore_CA_System:
                certDesiredProperties.certificateStore_CA_System.Add(certificateInfo);
                break;

            case CertificateStore.certificateStore_Root_System:
                certDesiredProperties.certificateStore_Root_System.Add(certificateInfo);
                break;

            case CertificateStore.certificateStore_My_User:
                certDesiredProperties.certificateStore_My_User.Add(certificateInfo);
                break;

            case CertificateStore.certificateStore_My_System:
                certDesiredProperties.certificateStore_My_System.Add(certificateInfo);
                break;

            default:
                break;
            }

            string refreshingValue = "\"refreshing\"";
            string finalValue      = "{" + desiredProperties.ToJsonString() + ", " + certDesiredProperties.ToJsonString() + "}";
            await _mainPage.UpdateTwinData(refreshingValue, finalValue);
        }