/*
 * This file provided by Facebook is for non-commercial testing and evaluation
 * purposes only.  Facebook reserves all rights not expressly granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.facebook.fresco.samples.showcase.drawee;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.interfaces.DraweeHierarchy;
import com.facebook.drawee.span.DraweeSpan;
import com.facebook.drawee.span.DraweeSpanStringBuilder;
import com.facebook.drawee.span.SimpleDraweeSpanTextView;
import com.facebook.fresco.samples.showcase.BaseShowcaseFragment;
import com.facebook.fresco.samples.showcase.R;
import com.facebook.fresco.samples.showcase.common.SimpleScaleTypeAdapter;

/**
 * Simple fragment that displays text with inline images using {@link DraweeSpan}.
 */
public class DraweeSpanSimpleTextFragment extends BaseShowcaseFragment {

  private static final Uri INLINE_IMAGE_URI =
      Uri.parse("http://frescolib.org/static/sample-images/animal_a_m.jpg");

  private SimpleDraweeSpanTextView mDraweeSpanTextView;
  private ScalingUtils.ScaleType mScaleType;

  @Nullable
  @Override
  public View onCreateView(
      LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_drawee_span_simple, container, false);
  }

  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    mDraweeSpanTextView = (SimpleDraweeSpanTextView) view.findViewById(R.id.drawee_text_view);
    final Spinner scaleType = (Spinner) view.findViewById(R.id.scaleType);

    final SimpleScaleTypeAdapter scaleTypeAdapter = SimpleScaleTypeAdapter.createForAllScaleTypes();
    scaleType.setAdapter(scaleTypeAdapter);
    scaleType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        final SimpleScaleTypeAdapter.Entry spinnerEntry =
            (SimpleScaleTypeAdapter.Entry) scaleTypeAdapter.getItem(position);
        mScaleType = spinnerEntry.scaleType;
        updateText();
      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {
      }
    });

    updateText();
  }

  private void updateText() {
    // The # will be replaced with the image.
    String text = getString(R.string.drawee_span_simple_text);
    int imagePosition = text.indexOf('#');

    DraweeSpanStringBuilder draweeSpanStringBuilder = new DraweeSpanStringBuilder(text);

    DraweeHierarchy draweeHierarchy = GenericDraweeHierarchyBuilder.newInstance(getResources())
        .setPlaceholderImage(new ColorDrawable(Color.RED))
        .setActualImageScaleType(mScaleType)
        .build();
    DraweeController controller = Fresco.newDraweeControllerBuilder()
        .setUri(INLINE_IMAGE_URI)
        .build();

    draweeSpanStringBuilder.setImageSpan(
        getContext(), /* Context */
        draweeHierarchy, /* hierarchy to be used */
        controller, /* controller to be used to update the hierarchy */
        imagePosition, /* image index within the text */
        200, /* image width */
        200, /* image height */
        false, /* auto resize */
        DraweeSpan.ALIGN_CENTER); /* alignment */

    mDraweeSpanTextView.setDraweeSpanStringBuilder(draweeSpanStringBuilder);
  }

  @Override
  public int getTitleId() {
    return R.string.drawee_span_simple_title;
  }
}

@@ -839,48 +839,8 @@
 le;%0A
-import android.support.v4.app.Fragment;%0A
 impo
@@ -867,24 +867,24 @@
 utInflater;%0A
+
 import andro
@@ -1621,16 +1621,83 @@
 Adapter;
+%0Aimport com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
 %0A%0A/**%0A *
@@ -1859,132 +1859,8 @@
  %7B%0A%0A
-  private static final Uri INLINE_IMAGE_URI =%0A      Uri.parse(%22http://frescolib.org/static/sample-images/animal_a_m.jpg%22);%0A%0A
   pr
@@ -1955,16 +1955,47 @@
 aleType;
+%0A  private Uri mInlineImageUri;
 %0A%0A  @Nul
@@ -2317,24 +2317,201 @@
 nceState) %7B%0A
+    final ImageUriProvider imageUriProvider = ImageUriProvider.getInstance(getContext());%0A    mInlineImageUri = imageUriProvider.createSampleUri(ImageUriProvider.ImageSize.M);%0A%0A
     mDraweeS
@@ -3916,24 +3916,23 @@
 Uri(
-INLINE_IMAGE_URI
+mInlineImageUri
 )%0A  

Update DraweeSpanSimpleTextFragment