/// <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>
 /// Adds a filledAutofillField to the collection, indexed by all of its hints.
 /// </summary>
 /// <returns>The add.</returns>
 /// <param name="filledAutofillField">Filled autofill field.</param>
 public void Add(FilledAutofillField filledAutofillField)
 {
     string[] autofillHints = filledAutofillField.AutofillHints;
     foreach (string hint in autofillHints)
     {
         HintMap.Add(hint, filledAutofillField);
     }
 }
 public FilledAutofillFieldCollection(Dictionary <string, FilledAutofillField> hintMap, string datasetName = "")
 {
     //recreate hint map making sure we compare case insensitive
     HintMap = BuildHintMap();
     foreach (var p in hintMap)
     {
         HintMap.Add(p.Key, p.Value);
     }
     DatasetName = datasetName;
 }
 /// <summary>
 /// Takes in a list of autofill hints (`autofillHints`), usually associated with a View or set of
 /// Views. Returns whether any of the filled fields on the page have at least 1 of these
 /// `autofillHint`s.
 /// </summary>
 /// <returns><c>true</c>, if with hints was helpsed, <c>false</c> otherwise.</returns>
 /// <param name="autofillHints">Autofill hints.</param>
 public bool HelpsWithHints(List <string> autofillHints)
 {
     for (int i = 0; i < autofillHints.Count; i++)
     {
         var autofillHint = autofillHints[i];
         if (HintMap.ContainsKey(autofillHint) && !HintMap[autofillHint].IsNull())
         {
             return(true);
         }
     }
     return(false);
 }
        /**
         * Takes in a list of autofill hints (`autofillHints`), usually associated with a View or set of
         * Views. Returns whether any of the filled fields on the page have at least 1 of these
         * `autofillHint`s.
         */
        public bool HelpsWithHints(List <String> autofillHints)
        {
            for (var i = 0; i < autofillHints.Count; i++)
            {
                if (HintMap.ContainsKey(autofillHints[i]) && !HintMap[autofillHints[i]].IsNull())
                {
                    return(true);
                }
            }

            return(false);
        }
 /// <summary>
 /// Adds a filledAutofillField to the collection, indexed by all of its hints.
 /// </summary>
 /// <returns>The add.</returns>
 /// <param name="filledAutofillField">Filled autofill field.</param>
 public void Add(FilledAutofillField filledAutofillField)
 {
     foreach (string hint in filledAutofillField.AutofillHints)
     {
         if (AutofillHintsHelper.IsSupportedHint(hint))
         {
             HintMap.Add(hint, filledAutofillField);
         }
         else
         {
             CommonUtil.loge($"Invalid hint: {hint}");
         }
     }
 }
        /**
         * Adds a {@code FilledAutofillField} to the collection, indexed by all of its hints.
         */
        public void Add(FilledAutofillField filledAutofillField)
        {
            if (filledAutofillField == null)
            {
                throw new ArgumentNullException(nameof(filledAutofillField));
            }

            var autofillHints = filledAutofillField.GetAutofillHints();

            foreach (var hint in autofillHints)
            {
                HintMap.Add(hint, filledAutofillField);
            }
        }
        /// <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);
        }
        /**
         * Populates a {@link Dataset.Builder} with appropriate values for each {@link AutofillId}
         * in a {@code AutofillFieldMetadataCollection}.
         *
         * In other words, it constructs an autofill
         * {@link Dataset.Builder} by applying saved values (from this {@code FilledAutofillFieldCollection})
         * to Views specified in a {@code AutofillFieldMetadataCollection}, which represents the current
         * page the user is on.
         */
        public bool ApplyToFields(AutofillFieldMetadataCollection autofillFieldMetadataCollection,
                                  Dataset.Builder datasetBuilder)
        {
            var setValueAtLeastOnce = false;
            var allHints            = autofillFieldMetadataCollection.AutofillHints;

            for (var hintIndex = 0; hintIndex < allHints.Count; hintIndex++)
            {
                var hint = allHints[hintIndex];
                if (!autofillFieldMetadataCollection.AutofillHintsToFieldsMap.ContainsKey(hint))
                {
                    continue;
                }

                var fillableAutofillFields = autofillFieldMetadataCollection.AutofillHintsToFieldsMap[hint];
                for (var autofillFieldIndex = 0; autofillFieldIndex < fillableAutofillFields.Count; autofillFieldIndex++)
                {
                    if (!HintMap.ContainsKey(hint))
                    {
                        continue;
                    }

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

                    case AutofillType.Date:
                        var dateValue = filledAutofillField.DateValue;
                        if (dateValue != null)
                        {
                            datasetBuilder.SetValue(autofillId, AutofillValue.ForDate(dateValue.Value));
                            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;

                    case AutofillType.None:
                    default:
                        break;
                    }
                }
            }

            return(setValueAtLeastOnce);
        }