package com.prolific.parallaxview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

public class ParallaxFactory implements LayoutInflater.Factory {
  //private static final String[] sClassPrefixList = {
  //    "android.widget.",
  //    "android.webkit."
  //};
  //private static final Map<Class<? extends TextView>, Integer> sStyles
  //    = new HashMap<Class<? extends TextView>, Integer>() {
  //  {
  //    put(TextView.class, android.R.attr.textViewStyle);
  //    put(Button.class, android.R.attr.buttonStyle);
  //    put(EditText.class, android.R.attr.editTextStyle);
  //    put(AutoCompleteTextView.class, android.R.attr.autoCompleteTextViewStyle);
  //    put(MultiAutoCompleteTextView.class, android.R.attr.autoCompleteTextViewStyle);
  //    put(CheckBox.class, android.R.attr.checkboxStyle);
  //    put(RadioButton.class, android.R.attr.radioButtonStyle);
  //    put(ToggleButton.class, android.R.attr.buttonStyleToggle);
  //  }
  //};


  private final LayoutInflater.Factory factory;
  private final int mXInAttributeId;
  private final int mXOutAttributeId;
  private final int mYInAttributeId;
  private final int mYOutAttributeId;
  private final int mFadeInAttributeId;
  private final int mFadeOutAttributeId;

  public ParallaxFactory(LayoutInflater.Factory factory, int xIn, int xOut,
      int yIn, int yOut, int fadeIn, int fadeOut) {

    this.factory = factory;
    mXInAttributeId = xIn;
    mXOutAttributeId = xOut;
    mYInAttributeId = yIn;
    mYOutAttributeId = yOut;
    mFadeInAttributeId = fadeIn;
    mFadeOutAttributeId = fadeOut;
  }

  @Override
  public View onCreateView(String name, Context context, AttributeSet attrs) {
    View view = null;

    if (context instanceof LayoutInflater.Factory) {
      view = ((LayoutInflater.Factory) context).onCreateView(name, context, attrs);
    }

    if (factory != null && view == null) {
      view = factory.onCreateView(name, context, attrs);
    }

    if (view == null) {
      view = createViewOrFailQuietly(name, context, attrs);
    }

    if (view != null) {
      onViewCreated(view, name, context, attrs);
    }

    return view;
  }

  protected View createViewOrFailQuietly(String name, Context context, AttributeSet attrs) {
    if (name.contains(".")) {
      return createViewOrFailQuietly(name, null, context, attrs);
    }

    //for (final String prefix : sClassPrefixList) {
    //  final View view = createViewOrFailQuietly(name, prefix, context, attrs);
    //
    //  if (view != null) {
    //    return view;
    //  }
    //}

    return null;
  }

  protected View createViewOrFailQuietly(String name, String prefix, Context context, AttributeSet attrs) {
    try {
      return LayoutInflater.from(context).createView(name, prefix, attrs);
    } catch (Exception ignore) {
      return null;
    }
  }

  protected void onViewCreated(View view, String name, Context context, AttributeSet attrs) {

    int[] attrIds = {mXInAttributeId, mXOutAttributeId, mYInAttributeId, mYOutAttributeId, mFadeInAttributeId, mFadeOutAttributeId};

    TypedArray a = context.obtainStyledAttributes(attrs, attrIds);

    ParallaxViewTag parallaxViewTag = new ParallaxViewTag();

    parallaxViewTag.xIn = a.getFloat(android.R.styleable.x_in, 0.0f);
    parallaxViewTag.xOut = a.getFloat(android.R.styleable.x_out, 0.0f);
    parallaxViewTag.yIn = a.getFloat(android.R.styleable.y_in, 0.0f);
    parallaxViewTag.yOut = a.getFloat(android.R.styleable.y_out, 0.0f);
    parallaxViewTag.fadeIn = a.getBoolean(android.R.styleable.fade_in, false);
    parallaxViewTag.fadeOut = a.getBoolean(android.R.styleable.fade_out, false);

    a.recycle();

    view.setTag(ParallaxViewTag.TAG, parallaxViewTag);
  }
}
@@ -266,817 +266,8 @@
 y %7B%0A
-  //private static final String%5B%5D sClassPrefixList = %7B%0A  //    %22android.widget.%22,%0A  //    %22android.webkit.%22%0A  //%7D;%0A  //private static final Map%3CClass%3C? extends TextView%3E, Integer%3E sStyles%0A  //    = new HashMap%3CClass%3C? extends TextView%3E, Integer%3E() %7B%0A  //  %7B%0A  //    put(TextView.class, android.R.attr.textViewStyle);%0A  //    put(Button.class, android.R.attr.buttonStyle);%0A  //    put(EditText.class, android.R.attr.editTextStyle);%0A  //    put(AutoCompleteTextView.class, android.R.attr.autoCompleteTextViewStyle);%0A  //    put(MultiAutoCompleteTextView.class, android.R.attr.autoCompleteTextViewStyle);%0A  //    put(CheckBox.class, android.R.attr.checkboxStyle);%0A  //    put(RadioButton.class, android.R.attr.radioButtonStyle);%0A  //    put(ToggleButton.class, android.R.attr.buttonStyleToggle);%0A  //  %7D%0A  //%7D;%0A%0A
 %0A  p
@@ -1643,219 +1643,8 @@
  %7D%0A%0A
-    //for (final String prefix : sClassPrefixList) %7B%0A    //  final View view = createViewOrFailQuietly(name, prefix, context, attrs);%0A    //%0A    //  if (view != null) %7B%0A    //    return view;%0A    //  %7D%0A    //%7D%0A%0A
     
@@ -2309,32 +2309,24 @@
  a.getFloat(
-android.
 R.styleable.
@@ -2372,32 +2372,24 @@
  a.getFloat(
-android.
 R.styleable.
@@ -2435,32 +2435,24 @@
  a.getFloat(
-android.
 R.styleable.
@@ -2502,24 +2502,16 @@
 etFloat(
-android.
 R.stylea
@@ -2566,32 +2566,24 @@
 .getBoolean(
-android.
 R.styleable.
@@ -2642,24 +2642,16 @@
 Boolean(
-android.
 R.stylea

remove pesky android.R