//public static List<FieldImportanceMapping> AllFieldImportanceMappings(this ImportTaskConfiguration importTaskConfiguration)
        //{
        //    var rc = new List<FieldImportanceMapping>();
        //    if (importTaskConfiguration != null)
        //    {
        //        importTaskConfiguration.ImportTaskEmails.ToList<ImportTaskEmail>().ForEach(x => rc.AddRange(x.FieldImportanceMappings.Cast<FieldImportanceMapping>().ToList()));
        //    }
        //    return rc;
        //}
        public static void ApplyDefaults(this ImportTaskConfiguration importTaskConfiguration)
        {
            importTaskConfiguration.DaysBetweenEmails = 15;
            importTaskConfiguration.DaysUntilEmailReminder = 10;

            // step 1
            ImportTaskEmail email1 = new ImportTaskEmail
            {
                Step = 1
            };

            email1.SetFieldNames(new Dictionary<string, string>()
            {
                {"FirstName", null}, {"LastName", null}, {"Initials", null}, {"PhoneNumber", null}
            });

            // step 2
            ImportTaskEmail email2 = new ImportTaskEmail
            {
                Step = 2
            };

            email2.SetFieldNames(new Dictionary<string, string>()
            {
                {"Address", null}, {"City", null}, {"Country", null}, {"PostCode", null}
            });

            // step 3
            ImportTaskEmail email3 = new ImportTaskEmail
            {
                Step = 3
            };

            email3.SetFieldNames(new Dictionary<string, string>()
            {
                {"Company", null}, {"AltPhoneNumber", null}
            });

            importTaskConfiguration.ImportTaskEmails.Add(email1);
            importTaskConfiguration.ImportTaskEmails.Add(email2);
            importTaskConfiguration.ImportTaskEmails.Add(email3);
        }
        protected void SaveImportTaskConfiguration()
        {
            //string json = Convert.ToString(ImportanceSettingsValuesTextBox.Value);
            //Dictionary<int, List<string>> emailPools = JsonConvert.DeserializeObject<Dictionary<int, List<string>>>(json);
            //List<FieldImportanceMapping> mappingsFromClient = new List<FieldImportanceMapping>();

            //foreach (var pool in emailPools)
            //{
            //    int importance = pool.Key;

            //    foreach (string fieldName in pool.Value)
            //    {
            //        mappingsFromClient.Add(new FieldImportanceMapping
            //        {
            //            FieldName = fieldName,
            //            Importance = importance
            //        });
            //    }
            //}

            //if (importTaskConfiguration.IsNew())
            //{
            //    importTaskConfiguration = new ImportTaskConfiguration();
            //}

            importTaskConfiguration.BindValuesFromFormLayout<ImportTaskConfiguration>(ImportTaskConfigurationFormLayout, true);
            ValidationResult validation = new ImportTaskConfigurationValidator().Validate(importTaskConfiguration);

            if (validation.IsValid)
            {
                // remove all existing
                if (importTaskConfiguration.IsNew() == false)
                {
                    importTaskConfiguration.ImportTaskEmails.ToList<ImportTaskEmail>().ForEach(x => x.Delete());

                }

                // add new
                int emailStep = 1;

                for (int i = 1; i <= 3; i++)
                {
                    string subject = Request.Form[String.Format("subject_Step{0}", i)];
                    string emailText = Request.Form[String.Format("emailText_Step{0}", i)];

                    ImportTaskEmail email = new ImportTaskEmail { Subject = subject, EmailText = emailText};
                    var fieldNames = new Dictionary<string, string>();

                    foreach (string propertyName in EmployeeUtilities.PropertiesAvailableForSelfUpdating())
                    {
                        var isChecked = Request.Form[String.Format("propertyName_Step{0}_{1}", i, propertyName)] != null;
                        var description = Request.Form[String.Format("propertyName_Step{0}_{1}_Description", i, propertyName)];

                        if (isChecked == true && email != null)
                        {
                            fieldNames[propertyName] = description;
                        }
                    }

                    if (fieldNames.Count > 0)
                    {
                        email.Step = emailStep;
                        emailStep++;
                        email.SetFieldNames(fieldNames);
                        email.Save();
                        importTaskConfiguration.ImportTaskEmails.Add(email);
                    }
                }

                if (importTaskConfiguration.Oid == Guid.Empty)
                {
                    Customer customer = CustomerUtilities.CurrentCustomerFromXPODefaultSession();
                    customer.ImportTaskConfigurations.Add(importTaskConfiguration);
                    customer.Save();
                }
                else
                {
                    importTaskConfiguration.Save();
                }

                Dictionary<string, string> newValues = new Dictionary<string, string>()
                {
                    { UrlManager.QueryString_ImportTaskConfiguration_Oid, Convert.ToString(importTaskConfiguration.Oid) },
                    { queryString_StatusLabelMessageKey, String.Format("Updated successfully at {0}", DateTime.Now) }
                };

                string redirectUrl = Utilities.UpdateUrlByChangingQueryStringValues(newValues);
                Response.Redirect(redirectUrl);
            }
            else
            {
                ShowErrorsForValidationResultInFormLayout(ImportTaskConfigurationFormLayout, validation);
            }
        }
        protected void SaveImportTaskConfiguration()
        {
            importTaskConfiguration.BindValuesFromFormLayout<ImportTaskConfiguration>(ImportTaskConfigurationFormLayout, true);
            ValidationResult validation = new ImportTaskConfigurationValidator().Validate(importTaskConfiguration);

            if (validation.IsValid)
            {
                // remove all existing
                if (importTaskConfiguration.IsNew() == false)
                {
                    importTaskConfiguration.ImportTaskEmails.ToList<ImportTaskEmail>().ForEach(x => x.Delete());

                }

                // add new
                int emailStep = 1;

                for (int i = 1; i <= 3; i++)
                {
                    string subject = Request.Form[String.Format("subject_Step{0}", i)];
                    string emailText = Request.Form[String.Format("emailText_Step{0}", i)];

                    ImportTaskEmail email = new ImportTaskEmail { Subject = subject, EmailText = emailText};
                    var fieldNames = new Dictionary<string, string>();

                    foreach (string propertyName in EmployeeUtilities.PropertiesAvailableForSelfUpdating())
                    {
                        var isChecked = Request.Form[String.Format("propertyName_Step{0}_{1}", i, propertyName)] != null;
                        var description = Request.Form[String.Format("propertyName_Step{0}_{1}_Description", i, propertyName)];

                        if (isChecked == true && email != null)
                        {
                            fieldNames[propertyName] = description;
                        }
                    }

                    if (fieldNames.Count > 0)
                    {
                        email.Step = emailStep;
                        emailStep++;
                        email.SetFieldNames(fieldNames);
                        email.Save();
                        importTaskConfiguration.ImportTaskEmails.Add(email);
                    }
                }

                if (importTaskConfiguration.Oid == Guid.Empty)
                {
                    Customer customer = CustomerUtilities.CurrentCustomerFromXPODefaultSession();
                    customer.ImportTaskConfigurations.Add(importTaskConfiguration);
                    customer.Save();
                }
                else
                {
                    importTaskConfiguration.Save();
                }

                Dictionary<string, string> newValues = new Dictionary<string, string>()
                {
                    { UrlManager.QueryString_ImportTaskConfiguration_Oid, Convert.ToString(importTaskConfiguration.Oid) },
                    { queryString_StatusLabelMessageKey, String.Format("Updated successfully at {0}", DateTime.Now) }
                };

                string redirectUrl = Utilities.UpdateUrlByChangingQueryStringValues(newValues);
                Response.Redirect(redirectUrl);
            }
            else
            {
                ShowErrorsForValidationResultInFormLayout(ImportTaskConfigurationFormLayout, validation);
                StatusMessage.ShowMessageWithHeader("Not saved", "", Views.Controls.MessageControlType.Negative);

                var listItems = new List<string>();
                validation.Errors.ToList().Where(x => String.IsNullOrEmpty(x.PropertyName)).ToList().ForEach(x => listItems.Add(x.ErrorMessage));
                StatusMessage.ShowListItems(listItems);
            }
        }