protected List <KeyValueDTO> MapRoleControlsToFields()
        {
            var resultCollection = new List <KeyValueDTO>();

            //get existing userDefinedFields
            var usedDefinedFields = Storage.CrateContentsOfType <KeyValueListCM>(x => x.Label == "DocuSignTemplateUserDefinedFields").FirstOrDefault();

            if (usedDefinedFields != null)
            {
                var tempFieldCollection = usedDefinedFields.Values;

                var mappingBehavior  = new TextSourceMappingBehavior(Storage, "RolesMapping", true);
                var textSourceValues = mappingBehavior.GetValues(Payload);
                foreach (var item in textSourceValues)
                {
                    var field = tempFieldCollection.FirstOrDefault(x => x.Key == item.Key);
                    if (field != null)
                    {
                        //field.Tags = "RecepientId:" + item.Value;
                        field.Value = item.Value;
                        resultCollection.Add(field);
                    }
                }
            }
            return(resultCollection);
        }
        protected List <KeyValueDTO> MapControlsToFields()
        {
            //todo: refactor the method
            var resultCollection = new List <KeyValueDTO>();

            //get existing userDefinedFields
            var usedDefinedFields = Storage.CrateContentsOfType <KeyValueListCM>(x => x.Label == "DocuSignTemplateUserDefinedFields").FirstOrDefault();

            if (usedDefinedFields != null)
            {
                var tempFieldCollection = usedDefinedFields.Values;

                //extract data from text source Controls
                var mappingBehavior  = new TextSourceMappingBehavior(Storage, "Mapping", true);
                var textSourceValues = mappingBehavior.GetValues(Payload);
                foreach (var item in textSourceValues)
                {
                    var field = tempFieldCollection.FirstOrDefault(x => x.Key == item.Key);
                    if (field != null)
                    {
                        field.Value = item.Value;
                        resultCollection.Add(field);
                    }
                }

                var radiopGroupMappingBehavior = new RadioButtonGroupMappingBehavior(Storage, "RadioGroupMapping");
                var radioButtonGroups          = radiopGroupMappingBehavior.GetValues(Payload);
                foreach (var item in radioButtonGroups)
                {
                    var field = tempFieldCollection.FirstOrDefault(x => x.Key == item.GroupName);
                    if (field != null)
                    {
                        //get index of selected value
                        var selectedItem = item.Radios.FirstOrDefault(x => x.Selected);
                        if (selectedItem != null)
                        {
                            field.Value = selectedItem.Value.ToString();
                            resultCollection.Add(field);
                        }
                    }
                }

                var checkBoxMappingBehavior = new CheckBoxMappingBehavior(Storage, "CheckBoxMapping");
                var checkboxes = checkBoxMappingBehavior.GetValues(Payload);
                foreach (var item in checkboxes)
                {
                    var field = tempFieldCollection.FirstOrDefault(x => x.Key == item.Name);
                    if (field != null)
                    {
                        field.Value = item.Selected.ToString().ToLower();
                        resultCollection.Add(field);
                    }
                }

                var dropdownListMappingBehavior = new DropDownListMappingBehavior(Storage, "DropDownMapping");
                var dropDownLists = dropdownListMappingBehavior.GetValues();
                foreach (var item in dropDownLists)
                {
                    var field = tempFieldCollection.FirstOrDefault(x => x.Key == item.Name);
                    if (field != null)
                    {
                        field.Value = item.selectedKey;
                        resultCollection.Add(field);
                    }
                }
            }

            return(resultCollection);
        }
        protected async Task HandleFollowUpConfiguration()
        {
            if (Storage.Count == 0)
            {
                return;
            }


            //update docusign templates list to get if new templates were provided by DS
            FillDocuSignTemplateSource("target_docusign_template");
            // Try to find DocuSignTemplate drop-down.

            var dropdownControlDTO = ConfigurationControls.FindByName("target_docusign_template");

            if (dropdownControlDTO == null)
            {
                return;
            }
            // Get DocuSign Template Id
            var docusignTemplateId = dropdownControlDTO.Value;

            //Abort configuration if templateId is the same that before
            if (!IsNewTemplateIdChoosen(Storage, docusignTemplateId))
            {
                return;
            }

            var conf          = DocuSignManager.SetUp(AuthorizationToken);
            var tabsandfields = DocuSignManager.GetTemplateRecipientsTabsAndDocuSignTabs(conf, docusignTemplateId);

            var roles = tabsandfields.Item1.Where(a => a.Tags.Contains(DocuSignConstants.DocuSignSignerTag));

            Storage.RemoveByLabel("DocuSignTemplateRolesFields");
            Storage.Add("DocuSignTemplateRolesFields", new KeyValueListCM(roles));


            var envelopeDataDTO = tabsandfields.Item2;

            var userDefinedFields = tabsandfields.Item1.Where(a => a.Tags.Contains(DocuSignConstants.DocuSignTabTag));

            //check for DocuSign default template names and add advisory json
            var hasDefaultNames = DocuSignManager.DocuSignTemplateDefaultNames(tabsandfields.Item2);

            if (hasDefaultNames)
            {
                var advisoryCrate          = Storage.CratesOfType <AdvisoryMessagesCM>().FirstOrDefault();
                var currentAdvisoryResults = advisoryCrate == null ? new AdvisoryMessagesCM() : advisoryCrate.Content;

                var advisory = currentAdvisoryResults.Advisories.FirstOrDefault(x => x.Name == advisoryName);

                if (advisory == null)
                {
                    currentAdvisoryResults.Advisories.Add(new AdvisoryMessageDTO {
                        Name = advisoryName, Content = advisoryContent
                    });
                }
                else
                {
                    advisory.Content = advisoryContent;
                }

                Storage.Add(Crate.FromContent("Advisories", currentAdvisoryResults));
            }

            Storage.RemoveByLabel("DocuSignTemplateUserDefinedFields");
            Storage.Add("DocuSignTemplateUserDefinedFields", new KeyValueListCM(userDefinedFields.Concat(roles)));

            //Create TextSource controls for ROLES
            var rolesMappingBehavior = new TextSourceMappingBehavior(Storage, "RolesMapping", true);

            rolesMappingBehavior.Clear();
            rolesMappingBehavior.Append(roles.Select(x => x.Key).ToList(), "Upstream Terminal-Provided Fields", AvailabilityType.RunTime);

            //Create Text Source controls for TABS
            var textSourceFields = new List <string>();

            textSourceFields = envelopeDataDTO.Where(x => x.Fr8DisplayType == ControlTypes.TextBox).Select(x => x.Name).ToList();
            var mappingBehavior = new TextSourceMappingBehavior(
                Storage,
                "Mapping",
                true
                );

            mappingBehavior.Clear();
            mappingBehavior.Append(textSourceFields, "Upstream Terminal-Provided Fields", AvailabilityType.RunTime);
            //Create TextSource controls for ROLES

            //Create radio Button Groups
            var radioButtonGroupBehavior = new RadioButtonGroupMappingBehavior(Storage, "RadioGroupMapping");

            radioButtonGroupBehavior.Clear();
            foreach (var item in envelopeDataDTO.Where(x => x.Fr8DisplayType == ControlTypes.RadioButtonGroup).ToList())
            {
                var radioButtonGroupDTO = item as DocuSignMultipleOptionsTabDTO;
                if (radioButtonGroupDTO == null)
                {
                    continue;
                }
                //todo: migrate the string format for label into template
                radioButtonGroupBehavior.Append(radioButtonGroupDTO.Name,
                                                $"For the <strong>{radioButtonGroupDTO.Name}</strong>, use:", radioButtonGroupDTO.Items.Select(x => new RadioButtonOption()
                {
                    Name     = x.Value,
                    Value    = x.Value,
                    Selected = x.Selected
                }).ToList());
            }

            //create checkbox controls
            var checkBoxMappingBehavior = new CheckBoxMappingBehavior(Storage, "CheckBoxMapping");

            checkBoxMappingBehavior.Clear();
            foreach (var item in envelopeDataDTO.Where(x => x.Fr8DisplayType == ControlTypes.CheckBox).ToList())
            {
                checkBoxMappingBehavior.Append(item.Name, item.Name);
            }

            //create dropdown controls
            var dropdownListMappingBehavior = new DropDownListMappingBehavior(Storage, "DropDownMapping");

            dropdownListMappingBehavior.Clear();
            foreach (var item in envelopeDataDTO.Where(x => x.Fr8DisplayType == ControlTypes.DropDownList).ToList())
            {
                var dropDownListDTO = item as DocuSignMultipleOptionsTabDTO;
                if (dropDownListDTO == null)
                {
                    continue;
                }

                dropdownListMappingBehavior.Append(dropDownListDTO.Name, $"For the <strong>{item.Name}</strong>, use:", dropDownListDTO.Items.Where(x => x.Text != string.Empty || x.Value != string.Empty).Select(x => new ListItem()
                {
                    Key      = string.IsNullOrEmpty(x.Value) ? x.Text : x.Value,
                    Value    = string.IsNullOrEmpty(x.Text) ? x.Value : x.Text,
                    Selected = x.Selected,
                }).ToList());
            }
        }