package com.cocosw.accessory.views.complex;

import android.annotation.TargetApi;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;

/**
 * Add 2 addtional function to ViewPager
 * 1 enable/disable swipe
 * 2 set scrollview as root view, viewpager can place into a scroll view
 *
 * @author kaliao
 */
public class CocoPager extends ViewPager {

    private boolean enabled = true;

    private ViewGroup root;

    public CocoPager(Context context) {
        super(context);
    }

    public CocoPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOffscreenPageLimit(3);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        try {
            if (root != null)
                root.requestDisallowInterceptTouchEvent(true);
            if (enabled)
                return super.onTouchEvent(event);
        } catch (IllegalArgumentException e) {
            Log.e("CocoPager", "onInterceptTouchEvent in IllegalArgumentException");
            return false;
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        try {
            if (root != null)
                root.requestDisallowInterceptTouchEvent(true);
            if (enabled)
                return super.onInterceptTouchEvent(event);
        } catch (IllegalArgumentException e) {
            Log.e("CocoPager", "onInterceptTouchEvent in IllegalArgumentException");
            return false;
        }
        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean getPagingEnabled() {
        return enabled;
    }

    @TargetApi(14)
    @Override
    protected boolean canScroll(final View v, final boolean checkV,
                                final int dx, final int x, final int y) {
        if (SDK_INT < ICE_CREAM_SANDWICH && v instanceof WebView)
            return ((WebView) v).canScrollHorizontally(-dx);
        else
            return super.canScroll(v, checkV, dx, x, y);
    }

    public void setRootView(ViewGroup view) {
        this.root = view;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (root != null)
            root.requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }
}

@@ -2493,24 +2493,42 @@
 Event ev) %7B%0A
+        try %7B%0A    
         if (
@@ -2533,32 +2533,36 @@
  (root != null)%0A
+    
             root
@@ -2604,32 +2604,36 @@
 (true);%0A        
+   
+ 
 return super.dis
@@ -2653,16 +2653,184 @@
 nt(ev);%0A
+        %7D catch (IllegalArgumentException e) %7B%0A            Log.e(%22CocoPager%22, %22onInterceptTouchEvent in IllegalArgumentException%22);%0A            return false;%0A        %7D%0A
     %7D%0A%7D%0A

fix a possible exception