/// <summary>
        /// Populates a Dataset.Builder with appropriate values for each AutofillId
        /// in a AutofillFieldMetadataCollection.
        ///
        /// In other words, it constructs an autofill Dataset.Builder
        /// by applying saved values (from this FilledAutofillFieldCollection)
        /// to Views specified in a AutofillFieldMetadataCollection, which represents the current
        /// page the user is on.
        /// </summary>
        /// <returns><c>true</c>, if to fields was applyed, <c>false</c> otherwise.</returns>
        /// <param name="autofillFieldMetadataCollection">Autofill field metadata collection.</param>
        /// <param name="datasetBuilder">Dataset builder.</param>
        public bool ApplyToFields(AutofillFieldMetadataCollection autofillFieldMetadataCollection, Dataset.Builder datasetBuilder)
        {
            bool setValueAtLeastOnce = false;

            foreach (string hint in autofillFieldMetadataCollection.AllAutofillCanonicalHints)
            {
                foreach (AutofillFieldMetadata autofillFieldMetadata in autofillFieldMetadataCollection.GetFieldsForHint(hint))
                {
                    FilledAutofillField filledAutofillField;
                    if (!HintMap.TryGetValue(hint, out filledAutofillField) || (filledAutofillField == null))
                    {
                        continue;
                    }

                    var autofillId   = autofillFieldMetadata.AutofillId;
                    var autofillType = autofillFieldMetadata.AutofillType;
                    switch (autofillType)
                    {
                    case AutofillType.List:
                        var listValue = autofillFieldMetadata.GetAutofillOptionIndex(filledAutofillField.TextValue);
                        if (listValue != -1)
                        {
                            datasetBuilder.SetValue(autofillId, AutofillValue.ForList(listValue));
                            setValueAtLeastOnce = true;
                        }
                        break;

                    case AutofillType.Date:
                        var dateValue = filledAutofillField.DateValue;
                        datasetBuilder.SetValue(autofillId, AutofillValue.ForDate((long)dateValue));
                        setValueAtLeastOnce = true;
                        break;

                    case AutofillType.Text:
                        var textValue = filledAutofillField.TextValue;
                        if (textValue != null)
                        {
                            datasetBuilder.SetValue(autofillId, AutofillValue.ForText(textValue));
                            setValueAtLeastOnce = true;
                        }
                        break;

                    case AutofillType.Toggle:
                        var toggleValue = filledAutofillField.ToggleValue;
                        if (toggleValue != null)
                        {
                            datasetBuilder.SetValue(autofillId, AutofillValue.ForToggle(toggleValue.Value));
                            setValueAtLeastOnce = true;
                        }
                        break;

                    default:
                        Log.Warn(CommonUtil.Tag, "Invalid autofill type - " + autofillType);
                        break;
                    }
                }
            }
            return(setValueAtLeastOnce);
        }
        /// <summary>
        /// Populates a Dataset.Builder with appropriate values for each AutofillId
        /// in a AutofillFieldMetadataCollection.
        ///
        /// In other words, it constructs an autofill Dataset.Builder
        /// by applying saved values (from this FilledAutofillFieldCollection)
        /// to Views specified in a AutofillFieldMetadataCollection, which represents the current
        /// page the user is on.
        /// </summary>
        /// <returns><c>true</c>, if to fields was applyed, <c>false</c> otherwise.</returns>
        /// <param name="autofillFieldMetadataCollection">Autofill field metadata collection.</param>
        /// <param name="datasetBuilder">Dataset builder.</param>
        public bool ApplyToFields(AutofillFieldMetadataCollection autofillFieldMetadataCollection, Dataset.Builder datasetBuilder)
        {
            bool setValueAtLeastOnce = false;

            foreach (string hint in autofillFieldMetadataCollection.AllAutofillCanonicalHints)
            {
                foreach (AutofillFieldMetadata autofillFieldMetadata in autofillFieldMetadataCollection.GetFieldsForHint(hint))
                {
                    FilledAutofillField filledAutofillField;
                    if (!HintMap.TryGetValue(hint, out filledAutofillField) || (filledAutofillField == null))
                    {
                        continue;
                    }

                    var autofillId   = autofillFieldMetadata.AutofillId;
                    var autofillType = autofillFieldMetadata.AutofillType;
                    switch (autofillType)
                    {
                    case AutofillType.List:
                        var listValue = autofillFieldMetadata.GetAutofillOptionIndex(filledAutofillField.TextValue);
                        if (listValue != -1)
                        {
                            datasetBuilder.SetValue(autofillId, AutofillValue.ForList(listValue));
                            setValueAtLeastOnce = true;
                        }
                        break;

                    case AutofillType.Date:
                        var dateValue = filledAutofillField.DateValue;
                        datasetBuilder.SetValue(autofillId, AutofillValue.ForDate((long)dateValue));
                        setValueAtLeastOnce = true;
                        break;

                    case AutofillType.Text:
                        var textValue = filledAutofillField.TextValue;
                        if (textValue != null)
                        {
                            datasetBuilder.SetValue(autofillId, AutofillValue.ForText(textValue));
                            setValueAtLeastOnce = true;
                        }
                        break;

                    case AutofillType.Toggle:
                        var toggleValue = filledAutofillField.ToggleValue;
                        if (toggleValue != null)
                        {
                            datasetBuilder.SetValue(autofillId, AutofillValue.ForToggle(toggleValue.Value));
                            setValueAtLeastOnce = true;
                        }
                        break;

                    default:
                        Log.Warn(CommonUtil.Tag, "Invalid autofill type - " + autofillType);
                        break;
                    }
                }
            }

            /*
             * if (!setValueAtLeastOnce)
             * {
             * Kp2aLog.Log("No value set. Hint keys : " + string.Join(",", HintMap.Keys));
             *      foreach (string hint in autofillFieldMetadataCollection.AllAutofillCanonicalHints)
             * {
             * Kp2aLog.Log("No value set. Hint = " + hint);
             * foreach (AutofillFieldMetadata autofillFieldMetadata in autofillFieldMetadataCollection
             * .GetFieldsForHint(hint))
             * {
             * Kp2aLog.Log("No value set. fieldForHint = " + autofillFieldMetadata.AutofillId.ToString());
             * FilledAutofillField filledAutofillField;
             * if (!HintMap.TryGetValue(hint, out filledAutofillField) || (filledAutofillField == null))
             * {
             *  Kp2aLog.Log("No value set. Hint map does not contain value, " +
             *              (filledAutofillField == null));
             *  continue;
             * }
             *
             * Kp2aLog.Log("autofill type=" + autofillFieldMetadata.AutofillType);
             * }
             * }
             * }*/

            return(setValueAtLeastOnce);
        }