package com.paulvarry.intra42.ui;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.paulvarry.intra42.activities.user.UserProjectsFragment;
import com.paulvarry.intra42.adapters.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class CustomViewPager extends ViewPager {

    private boolean enabled = true;
    private List<String> disableSwiping;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return canSwipe() && super.onInterceptTouchEvent(event);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return canSwipe() && super.onTouchEvent(event);

    }

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

    public void disableSwiping(String tabName) {
        if (disableSwiping == null)
            disableSwiping = new ArrayList<>();
        disableSwiping.add(tabName);
    }

    boolean canSwipe() {
        ViewPagerAdapter adapter = (ViewPagerAdapter) getAdapter();
        int c = getCurrentItem();
        List<String> title = adapter.getPageTitle();

        if (title.size() <= c)
            return enabled;

        String item = title.get(c);

        Fragment fragment = adapter.getItem(getCurrentItem());
        if (fragment instanceof UserProjectsFragment) {
            return ((UserProjectsFragment) fragment).canSwipe();
        }

        if (disableSwiping != null)
            for (String d : disableSwiping) {
                if (d.contains(item))
                    return false;
            }
        return enabled;
    }
}
@@ -332,16 +332,77 @@
 Adapter;
+%0Aimport com.paulvarry.intra42.adapters.ViewStatePagerAdapter;
 %0A%0Aimport
@@ -1289,20 +1289,183 @@
         
-View
+Fragment fragment;%0A        String item;%0A        int currentItem = getCurrentItem();%0A%0A        if (getAdapter() instanceof ViewStatePagerAdapter) %7B%0A            ViewState
 PagerAda
@@ -1480,24 +1480,29 @@
 pter = (View
+State
 PagerAdapter
@@ -1521,42 +1521,13 @@
 ();%0A
+%0A
     
-    int c = getCurrentItem();%0A
     
@@ -1571,25 +1571,28 @@
 ageTitle();%0A
-%0A
+    
         if (
@@ -1592,67 +1592,194 @@
    i
-f (title.size() %3C= c)%0A            return enabled;%0A%0A
+tem = title.get(currentItem);%0A%0A            fragment = adapter.getItem(currentItem);%0A        %7D else %7B%0A            ViewPagerAdapter adapter = (ViewPagerAdapter) getAdapter();%0A%0A    
         
 Stri
@@ -1770,30 +1770,80 @@
             
+List%3C
 String
+%3E title = adapter.getPageTitle();%0A           
  item = titl
@@ -1849,16 +1849,26 @@
 le.get(c
+urrentItem
 );%0A%0A    
@@ -1867,32 +1867,27 @@
 );%0A%0A        
+  
-Fragment
+ 
  fragment = 
@@ -1902,20 +1902,17 @@
 getItem(
-getC
+c
 urrentIt
@@ -1917,12 +1917,21 @@
 Item
-()
 );
+%0A        %7D%0A
 %0A   

Update CustomViewPager for StateViewPager