package com.ddmeng.zhihudaily.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

import com.ddmeng.zhihudaily.R;

import java.lang.ref.WeakReference;

public class AutoScrollViewPager extends ViewPager {
    public static final int MESSAGE_SCROLL = 0;
    public static final int DEFAULT_SCROLL_INTERVAL_DURATION = 1000;

    private Handler scrollHandler;
    private int scrollIntervalDuration = DEFAULT_SCROLL_INTERVAL_DURATION;
    private boolean isAutoScrollEnabled = false;

    public AutoScrollViewPager(Context context) {
        super(context);
        init(context, null);
    }

    public AutoScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AutoScrollViewPager, 0, 0);
            scrollIntervalDuration = typedArray.getInteger(R.styleable.AutoScrollViewPager_scrollIntervalDuration,
                    DEFAULT_SCROLL_INTERVAL_DURATION);
            typedArray.recycle();
        }
        scrollHandler = new ScrollHandler(this);
    }

    public int getScrollIntervalDuration() {
        return scrollIntervalDuration;
    }

    public void setScrollIntervalDuration(int scrollIntervalDuration) {
        this.scrollIntervalDuration = scrollIntervalDuration;
    }

    public void startAutoScroll() {
        isAutoScrollEnabled = true;
        sendScrollMessage();
    }

    public void stopAutoScroll() {
        isAutoScrollEnabled = false;
        scrollHandler.removeMessages(MESSAGE_SCROLL);
    }

    private void sendScrollMessage() {
        if (isAutoScrollEnabled) {
            scrollHandler.removeMessages(MESSAGE_SCROLL);
            scrollHandler.sendEmptyMessageDelayed(MESSAGE_SCROLL, scrollIntervalDuration);
        }
    }

    private void scroll() {
        PagerAdapter adapter = getAdapter();
        int totalCount = adapter != null ? adapter.getCount() : 0;
        if (totalCount <= 1) {
            return;
        }
        int currentItem = getCurrentItem();
        setCurrentItem(currentItem == totalCount - 1 ? 0 : ++currentItem, true);
    }

    private static class ScrollHandler extends Handler {
        private WeakReference<AutoScrollViewPager> viewPagerWeakReference;

        public ScrollHandler(AutoScrollViewPager viewPager) {
            this.viewPagerWeakReference = new WeakReference<>(viewPager);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_SCROLL: {
                    AutoScrollViewPager autoScrollViewPager = viewPagerWeakReference.get();
                    if (autoScrollViewPager != null) {
                        autoScrollViewPager.scroll();
                        autoScrollViewPager.sendScrollMessage();
                    }
                    break;
                }
                default:
                    break;
            }
        }
    }
}

@@ -310,16 +310,61 @@
 daily.R;
+%0Aimport com.ddmeng.zhihudaily.utils.LogUtils;
 %0A%0Aimport
@@ -443,24 +443,84 @@
 ViewPager %7B%0A
+    public static final String TAG = %22AutoScrollViewPager%22;%0A
     public s
@@ -618,17 +618,17 @@
 ATION = 
-1
+2
 000;%0A%0A  
@@ -1502,24 +1502,988 @@
 dler(this);%0A
+        addOnPageChangeListener(new OnPageChangeListener() %7B%0A            @Override%0A            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) %7B%0A                LogUtils.d(TAG, %22onPageScrolled: %22 + position + %22, %22 + positionOffset);%0A            %7D%0A%0A            @Override%0A            public void onPageSelected(int position) %7B%0A                LogUtils.d(TAG, %22onPageSelected: %22 + position);%0A            %7D%0A%0A            @Override%0A            public void onPageScrollStateChanged(int state) %7B%0A                LogUtils.d(TAG, %22onPageScrollStateChanged: %22 + state);%0A                switch (state) %7B%0A                    case SCROLL_STATE_IDLE: %7B%0A                        startAutoScroll();%0A                        break;%0A                    %7D%0A                    case SCROLL_STATE_DRAGGING: %7B%0A                        stopAutoScroll();%0A                        break;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D);%0A
     %7D%0A%0A    p
@@ -4146,73 +4146,8 @@
 ();%0A
-                        autoScrollViewPager.sendScrollMessage();%0A
     

stop scroll when user swipe, resume scroll when idle