private void HandleHikCameraPostBack(NameValueCollection parts, string form)
        {
            if (form == NameToIdWithPrefix(HikDeleteCamera))
            {
                pluginConfig.RemoveHikvisionIsapiCamera(parts[RecordId]);
                pluginConfig.FireConfigChanged();
                divToUpdate.Add(SaveErrorDivId, RedirectPageJS(Invariant($"/{pageUrl}?{TabId}=1")));
            }
            else if (form == NameToIdWithPrefix(HikCancelCamera))
            {
                divToUpdate.Add(SaveErrorDivId, RedirectPageJS(Invariant($"/{pageUrl}?{TabId}=1")));
            }
            else if (form == NameToIdWithPrefix(HikSaveCamera))
            {
                StringBuilder results    = new StringBuilder();
                string        cameraName = parts[nameof(HikCameraSetting.Name)];
                if (string.IsNullOrWhiteSpace(cameraName))
                {
                    results.AppendLine("Camera Name is empty.<br>");
                }

                string userId   = parts[nameof(HikCameraSetting.Login)];
                string password = parts[nameof(HikCameraSetting.Password)];

                string cameraHostString = parts[nameof(HikCameraSetting.CameraHost)];
                if (!Uri.TryCreate(cameraHostString, UriKind.Absolute, out var cameraHost))
                {
                    results.AppendLine("Camera address is not valid.<br>");
                }

                var alarmCancelIntervalString = parts[nameof(HikCameraSetting.AlarmCancelInterval)];
                if (!long.TryParse(alarmCancelIntervalString, NumberStyles.Any, CultureInfo.InvariantCulture, out var alarmCancelInterval) ||
                    alarmCancelInterval < 0)
                {
                    results.AppendLine("Alarm cancel interval is not valid.<br>");
                }

                var propertiesRefreshIntervalString = parts[nameof(HikCameraSetting.CameraPropertiesRefreshInterval)];
                if (!long.TryParse(propertiesRefreshIntervalString, NumberStyles.Any, CultureInfo.InvariantCulture, out var propertiesRefreshInterval) ||
                    alarmCancelInterval < 0)
                {
                    results.AppendLine("Properties refresh interval is not valid.<br>");
                }

                var snapshotDirectory = parts[nameof(HikCameraSetting.SnapshotDownloadDirectory)];
                if (!Directory.Exists(snapshotDirectory))
                {
                    results.AppendLine("Snapshot directory is not valid.<br>");
                }

                var videoDownloadDirectory = parts[nameof(HikCameraSetting.VideoDownloadDirectory)];
                if (!Directory.Exists(videoDownloadDirectory))
                {
                    results.AppendLine("Video download directory is not valid.<br>");
                }

                if (results.Length > 0)
                {
                    divToUpdate.Add(SaveErrorDivId, results.ToString());
                }
                else
                {
                    string id = parts[RecordId];

                    if (string.IsNullOrWhiteSpace(id))
                    {
                        id = System.Guid.NewGuid().ToString();
                    }

                    var data = new HikCameraSetting(id,
                                                    cameraName,
                                                    cameraHost.AbsoluteUri.ToString(CultureInfo.InvariantCulture),
                                                    userId,
                                                    password,
                                                    TimeSpan.FromSeconds(alarmCancelInterval),
                                                    pluginConfig.HikvisionIsapiCameraProperties,
                                                    TimeSpan.FromSeconds(propertiesRefreshInterval),
                                                    snapshotDirectory,
                                                    videoDownloadDirectory);

                    pluginConfig.AddHikvisionIsapiCamera(data);
                    pluginConfig.FireConfigChanged();
                    divToUpdate.Add(SaveErrorDivId, RedirectPageJS(Invariant($"/{pageUrl}?{TabId}=1")));
                }
            }
        }
        private string BuildAddNewHikvisionIsapiCameraWebPageBody([AllowNull] HikCameraSetting cameraSettings)
        {
            TimeSpan DefaultAlarmCancelInterval             = TimeSpan.FromSeconds(30);
            TimeSpan DefaultCameraPropertiesRefreshInterval = TimeSpan.FromSeconds(60);

            string id                        = cameraSettings?.Id ?? Guid.NewGuid().ToString();
            string name                      = cameraSettings?.Name ?? string.Empty;
            string hostName                  = cameraSettings?.CameraHost ?? @"http://";
            string userId                    = cameraSettings?.Login ?? string.Empty;
            string password                  = cameraSettings?.Password ?? string.Empty;
            string alarmCancelInterval       = (cameraSettings?.AlarmCancelInterval ?? DefaultAlarmCancelInterval).TotalSeconds.ToString(CultureInfo.InvariantCulture);
            string propertiesRefreshInterval = (cameraSettings?.CameraPropertiesRefreshInterval ?? DefaultCameraPropertiesRefreshInterval).TotalSeconds.ToString(CultureInfo.InvariantCulture);
            string snapshotDownloadDirectory = cameraSettings?.SnapshotDownloadDirectory ?? string.Empty;
            string videoDownloadDirectory    = cameraSettings?.VideoDownloadDirectory ?? string.Empty;

            string buttonLabel = cameraSettings != null ? "Save" : "Add";
            string header      = cameraSettings != null ? "Edit Camera" : "Add New Camera";

            StringBuilder stb = new StringBuilder();

            stb.Append(PageBuilderAndMenu.clsPageBuilder.FormStart("ftmCameraChange", "IdChange", "Post"));

            stb.Append(@"<div>");
            stb.Append(@"<table class='full_width_table'>");
            stb.Append("<tr height='5'><td></td><td></td></tr>");
            stb.Append(Invariant($"<tr><td class='tableheader' colspan=2>{header}</td></tr>"));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Name:</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(nameof(HikCameraSetting.Name), name));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Camera:</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(nameof(HikCameraSetting.CameraHost), hostName, size: 75));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>User:</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(nameof(HikCameraSetting.Login), userId));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Password:</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(nameof(HikCameraSetting.Password), password, type: "password"));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Alarm Cancel Interval(seconds):</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(nameof(HikCameraSetting.AlarmCancelInterval), alarmCancelInterval));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Properties Refresh Interval(seconds):</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(nameof(HikCameraSetting.CameraPropertiesRefreshInterval), propertiesRefreshInterval));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Snapshot download directory:</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(nameof(HikCameraSetting.SnapshotDownloadDirectory), snapshotDownloadDirectory));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Video download directory:</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(nameof(HikCameraSetting.VideoDownloadDirectory), videoDownloadDirectory));
            stb.Append("</td></tr>");

            stb.Append(Invariant($"<tr><td colspan=2>{HtmlTextBox(RecordId, id, type: "hidden")}<div id='{SaveErrorDivId}' style='color:Red'></div></td><td></td></tr>"));
            stb.Append(Invariant($"<tr><td colspan=2>{FormPageButton(HikSaveCamera, buttonLabel)}"));

            if (cameraSettings != null)
            {
                stb.Append(FormPageButton(HikDeleteCamera, "Delete"));
            }

            stb.Append(FormPageButton(HikCancelCamera, "Cancel"));
            stb.Append(Invariant($"</td></tr>"));
            stb.Append("<tr height='5'><td colspan=2></td></tr>");
            stb.Append(@"</table>");
            stb.Append(@"</div>");
            stb.Append(PageBuilderAndMenu.clsPageBuilder.FormEnd());

            return(stb.ToString());
        }