/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.flipper.plugins.inspector;

import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperValue;

public class InspectorValue<T> implements FlipperValue {

  /**
   * Describe the type of data this value contains. This will influence how values are parsed and
   * displayed by the Flipper desktop app. For example colors will be parse as integers and
   * displayed using hex values and be editable using a color picker.
   *
   * <p>Do not extends this list of types without adding support for the type in the desktop
   * Inspector.
   */
  public static class Type<T> {

    public static final Type Auto = new Type<>("auto");
    public static final Type<String> Text = new Type<>("text");
    public static final Type<Number> Number = new Type<>("number");
    public static final Type<Boolean> Boolean = new Type<>("boolean");
    public static final Type<String> Enum = new Type<>("enum");
    public static final Type<Integer> Color = new Type<>("color");

    private final String mName;

    Type(String name) {
      mName = name;
    }

    @Override
    public String toString() {
      return mName;
    }
  }

  final Type<T> mType;
  final T mValue;
  final boolean mMutable;

  private InspectorValue(Type<T> type, T value, boolean mutable) {
    mType = type;
    mValue = value;
    mMutable = mutable;
  }

  public static <T> InspectorValue<T> mutable(Type<T> type, T value) {
    return new InspectorValue<>(type, value, true);
  }

  public static <T> InspectorValue<T> immutable(Type<T> type, T value) {
    return new InspectorValue<>(type, value, false);
  }

  public static InspectorValue mutable(Object value) {
    return new InspectorValue<>(Type.Auto, value, true);
  }

  public static InspectorValue immutable(Object value) {
    return new InspectorValue<>(Type.Auto, value, false);
  }

  @Override
  public FlipperObject toFlipperObject() {
    return new FlipperObject.Builder()
        .put("__type__", mType)
        .put("__mutable__", mMutable)
        .put("value", mValue)
        .build();
  }
}

@@ -325,16 +325,38 @@
 erValue;
+%0Aimport java.util.Set;
 %0A%0Apublic
@@ -1215,16 +1215,84 @@
 color%22);
+%0A    public static final Type%3CPicker%3E Picker = new Type%3C%3E(%22picker%22);
 %0A%0A    pr
@@ -2337,16 +2337,16 @@
 mValue)%0A
-
         
@@ -2358,11 +2358,789 @@
 d();%0A  %7D
+%0A%0A  public static final class Picker %7B%0A    public final Set%3CString%3E values;%0A    public final String selected;%0A%0A    public Picker(Set%3CString%3E values, String selected) %7B%0A      this.values = values;%0A      this.selected = selected;%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A      // FIXME(festevezga) - Manually rolled json, #noragrets%0A      StringBuilder b = new StringBuilder();%0A      b.append(%22%7B %5C%22values%5C%22: %22);%0A      b.append(%22%5B%22);%0A      int i = values.size();%0A      for (String value : values) %7B%0A        b.append('%22').append(value).append('%22');%0A        i--;%0A        if (i != 0) %7B%0A          b.append(%22,%22);%0A        %7D%0A      %7D%0A      b.append(%22%5D%22);%0A      b.append(%22, %5C%22selected%5C%22: %5C%22%22);%0A      b.append(selected);%0A      b.append(%22%5C%22%7D%22);%0A      return b.toString();%0A    %7D%0A  %7D
 %0A%7D%0A

Add support for the Picker widget in Android