/// <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);
        }
        private static AutofillValue ApplyValue(Field field, string value, bool monthValue = false)
        {
            switch (field.AutofillType)
            {
            case AutofillType.Date:
                if (long.TryParse(value, out long dateValue))
                {
                    return(AutofillValue.ForDate(dateValue));
                }
                break;

            case AutofillType.List:
                if (field.AutofillOptions != null)
                {
                    if (monthValue && int.TryParse(value, out int monthIndex))
                    {
                        if (field.AutofillOptions.Count == 13)
                        {
                            return(AutofillValue.ForList(monthIndex));
                        }
                        else if (field.AutofillOptions.Count >= monthIndex)
                        {
                            return(AutofillValue.ForList(monthIndex - 1));
                        }
                    }

                    for (var i = 0; i < field.AutofillOptions.Count; i++)
                    {
                        if (field.AutofillOptions[i].Equals(value))
                        {
                            return(AutofillValue.ForList(i));
                        }
                    }
                }
                break;

            case AutofillType.Text:
                return(AutofillValue.ForText(value));

            case AutofillType.Toggle:
                if (bool.TryParse(value, out bool toggleValue))
                {
                    return(AutofillValue.ForToggle(toggleValue));
                }
                break;

            default:
                break;
            }

            return(null);
        }
        /// <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);
        }
        /// <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;
            List <string> allHints            = autofillFieldMetadataCollection.AllAutofillHints;

            for (int hintIndex = 0; hintIndex < allHints.Count; hintIndex++)
            {
                string hint = allHints[hintIndex];
                List <AutofillFieldMetadata> fillableAutofillFields = autofillFieldMetadataCollection.GetFieldsForHint(hint);
                if (fillableAutofillFields == null)
                {
                    continue;
                }
                for (int autofillFieldIndex = 0; autofillFieldIndex < fillableAutofillFields.Count; autofillFieldIndex++)
                {
                    FilledAutofillField filledAutofillField = HintMap[hint];
                    if (filledAutofillField == null)
                    {
                        continue;
                    }
                    AutofillFieldMetadata autofillFieldMetadata = fillableAutofillFields[autofillFieldIndex];
                    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);
        }
        void BindValueToNode(AssistStructure.ViewNode viewNode,
                             FilledAutofillField field, Dataset.Builder builder,
                             MutableBoolean setValueAtLeastOnce)
        {
            AutofillId autofillId = viewNode.AutofillId;

            if (autofillId == null)
            {
                Util.Logw("Autofill ID null for %s", viewNode.ToString());
                return;
            }
            int autofillType = (int)viewNode.AutofillType;

            switch (autofillType)
            {
            case (int)AutofillType.List:
                var options   = viewNode.GetAutofillOptions();
                int listValue = -1;
                if (options != null)
                {
                    listValue = Util.IndexOf(viewNode.GetAutofillOptions(), field.GetTextValue());
                }
                if (listValue != -1)
                {
                    builder.SetValue(autofillId, AutofillValue.ForList(listValue));
                    setValueAtLeastOnce.Value = true;
                }
                break;

            case (int)AutofillType.Date:
                var dateValue = field.GetDateValue();
                if (dateValue != null)
                {
                    builder.SetValue(autofillId, AutofillValue.ForDate(dateValue));
                    setValueAtLeastOnce.Value = true;
                }
                break;

            case (int)AutofillType.Text:
                string textValue = field.GetTextValue();
                if (textValue != null)
                {
                    builder.SetValue(autofillId, AutofillValue.ForText(textValue));
                    setValueAtLeastOnce.Value = true;
                }
                break;

            case (int)AutofillType.Toggle:
                var toggleValue = field.GetToggleValue();
                if (toggleValue != null)
                {
                    builder.SetValue(autofillId, AutofillValue.ForToggle(toggleValue));
                    setValueAtLeastOnce.Value = true;
                }
                break;

            case (int)AutofillType.None:
                break;

            default:
                Util.Logw("Invalid autofill type - %d", autofillType);
                break;
            }
        }
        /**
         * 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);
        }
        /**
         * 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)
        {
            bool          setValueAtLeastOnce = false;
            List <string> allHints            = autofillFieldMetadataCollection.GetAllHints();

            for (var hintIndex = 0; hintIndex < allHints.Count; hintIndex++)
            {
                string hint = allHints[hintIndex];
                var    fillableAutofillFields = autofillFieldMetadataCollection.GetFieldsForHint(hint);
                if (fillableAutofillFields == null)
                {
                    continue;
                }

                for (var autofillFieldIndex = 0;
                     autofillFieldIndex < fillableAutofillFields.Count;
                     autofillFieldIndex++)
                {
                    var filledAutofillField = mHintMap.FirstOrDefault(x => x.Key == hint);
                    if (filledAutofillField.Value == null)
                    {
                        continue;
                    }

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

                        break;

                    case AutofillType.Date:
                        var dateValue = filledAutofillField.Value.GetDateValue();
                        if (dateValue != null)
                        {
                            datasetBuilder.SetValue(autofillId, AutofillValue.ForDate(dateValue));
                            setValueAtLeastOnce = true;
                        }

                        break;

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

                        break;

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

                        break;

                    case  AutofillType.None:
                        break;

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

            return(setValueAtLeastOnce);
        }