package com.selluv.android.common;

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

/**
 * Created by wdkim on 2016. 11. 2..
 */

public class BBViewPager extends ViewPager {
    private boolean scrollEnabled;
    private static final int OFF_SET = 100;
    private float preX;

    public BBViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPageEnabled(false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (this.scrollEnabled) return super.onTouchEvent(ev);
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (this.scrollEnabled) {
            final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    preX = ev.getX();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float x = ev.getX();
                    if ((x - OFF_SET <= preX && preX <= x+ OFF_SET)) {
                        return false;
                    } else {
                        return true;
                    }
            }
            return super.onInterceptTouchEvent(ev);
        }
        return false;
    }

    /**
     * viewpager 스크롤 활성화 여부 설정
     * @param enabled
     */
    public void setPageEnabled(boolean enabled) {
        this.scrollEnabled = enabled;
    }
}
