package com.fragmentmaster.internal;

import android.annotation.SuppressLint;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.view.ViewPagerCompat;
import android.view.MotionEvent;
import android.view.View;

/**
 * Real container of fragments.
 */
class FragmentMasterPager extends ViewPagerCompat {

	private FragmentMasterImpl mFragmentMasterImpl;

	private static final int ANIMATION_NONE = 0;
	private static final int ANIMATION_ENTER = 1;
	private static final int ANIMATION_EXIT = 2;

	private int mAnimationState = ANIMATION_NONE;

	// The position of primary item in the latest SCROLL_STATE_IDLE state.
	private int mLatestIdleItem = 0;
	private ViewPager.PageTransformer mPageTransformer = new ViewPager.PageTransformer() {
		@Override
		public void transformPage(View page, float position) {
			if (mFragmentMasterImpl.hasPageAnimator()) {
				mFragmentMasterImpl.getPageAnimator().transformPage(page,
						position, mAnimationState == ANIMATION_ENTER);
			}
		}
	};

	// Internal listener
	private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {
		@Override
		public void onPageScrollStateChanged(int state) {
			if (mWrappedOnPageChangeListener != null) {
				mWrappedOnPageChangeListener.onPageScrollStateChanged(state);
			}

			if (state == ViewPager.SCROLL_STATE_IDLE) {
				mLatestIdleItem = getCurrentItem();
				mAnimationState = ANIMATION_NONE;
			}
		}
		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			if (mWrappedOnPageChangeListener != null) {
				mWrappedOnPageChangeListener.onPageScrolled(position,
						positionOffset, positionOffsetPixels);
			}
		}

		@Override
		public void onPageSelected(int position) {
			if (mWrappedOnPageChangeListener != null) {
				mWrappedOnPageChangeListener.onPageSelected(position);
			}
		}
	};

	private OnPageChangeListener mWrappedOnPageChangeListener;

	public FragmentMasterPager(FragmentMasterImpl fragmentMaster) {
		super(fragmentMaster.getActivity());
		mFragmentMasterImpl = fragmentMaster;
		super.setOnPageChangeListener(mOnPageChangeListener);
		setPageTransformer(false, mPageTransformer);
	}

	@Override
	@SuppressLint("ClickableViewAccessibility")
	public boolean onTouchEvent(MotionEvent ev) {
		if (mFragmentMasterImpl.isSlideable()
				&& !mFragmentMasterImpl.isScrolling()) {
			return super.onTouchEvent(ev);
		}
		return false;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (mFragmentMasterImpl.isSlideable()
				&& !mFragmentMasterImpl.isScrolling()) {
			return super.onInterceptTouchEvent(ev);
		}
		return false;
	}

	@Override
	public void setOnPageChangeListener(OnPageChangeListener listener) {
		mWrappedOnPageChangeListener = listener;
	}

	@Override
	protected void onPageScrolled(int position, float offset, int offsetPixels) {
		if (mLatestIdleItem > position) {
			// The ViewPager is performing exiting.
			mAnimationState = ANIMATION_EXIT;
		} else if (mLatestIdleItem == position) {
			// The ViewPager is performing entering.
			mAnimationState = ANIMATION_ENTER;
		}
		super.onPageScrolled(position, offset, offsetPixels);
	}

	@Override
	public void onRestoreInstanceState(Parcelable state) {
		super.onRestoreInstanceState(state);
		mLatestIdleItem = getCurrentItem();
	}

}

@@ -747,24 +747,183 @@
 leItem = 0;%0A
+%09private Runnable mIdleRunnable = new Runnable() %7B%0A%09%09public void run() %7B%0A%09%09%09mLatestIdleItem = getCurrentItem();%0A%09%09%09setAnimationState(ANIMATION_NONE);%0A%09%09%7D%0A%09%7D;%0A%0A
 %09private Vie
@@ -1587,80 +1587,27 @@
 %09%09%09%09
-mLatestIdleItem = getCurrentItem();%0A%09%09%09%09mAnimationState = ANIMATION_NONE
+post(mIdleRunnable)
 ;%0A%09%09
@@ -3128,33 +3128,35 @@
 ing exiting.%0A%09%09%09
-m
+set
 AnimationState =
@@ -3145,35 +3145,33 @@
 etAnimationState
- = 
+(
 ANIMATION_EXIT;%0A
@@ -3168,16 +3168,17 @@
 ION_EXIT
+)
 ;%0A%09%09%7D el
@@ -3200,17 +3200,17 @@
 dleItem 
-=
+%3C
 = positi
@@ -3262,17 +3262,19 @@
 ing.%0A%09%09%09
-m
+set
 Animatio
@@ -3271,35 +3271,33 @@
 etAnimationState
- = 
+(
 ANIMATION_ENTER;
@@ -3295,16 +3295,17 @@
 ON_ENTER
+)
 ;%0A%09%09%7D%0A%09%09
@@ -3468,16 +3468,16 @@
 state);%0A
-
 %09%09mLates
@@ -3510,10 +3510,86 @@
 ();%0A%09%7D%0A%0A
+%09private void setAnimationState(int state) %7B%0A%09%09mAnimationState = state;%0A%09%7D%0A%0A
 %7D%0A

Fix animation state of FragmentMasterPager.