package com.saki.ui.adapter;

import android.os.Handler;
import android.os.Message;

public class PromoteAppAdapterHandler extends Handler {
    class autoScroll implements Runnable {
        @Override
        public void run() {
            int index = PromoteAppAdapterHandler.this.adapter.getBanner()
                    .getPager().getCurrentItem();
            if (index == PromoteAppAdapterHandler.this.adapter.getCount() - 1) {
                PromoteAppAdapterHandler.this.adapter.getBanner().getPager()
                        .setCurrentItem(0);
            } else {
                PromoteAppAdapterHandler.this.adapter.getBanner().getPager()
                        .arrowScroll(2);
            }
            if (auto)
                PromoteAppAdapterHandler.this.postDelayed(this, 20000);
        }
    }

    private final PromoteAppAdapter adapter;
    private boolean auto = true;

    public PromoteAppAdapterHandler(PromoteAppAdapter adaper) {
        this.adapter = adaper;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 0)
            this.adapter.notifyDataSetChanged();
    }

    public void startAutoScroll() {
        auto = true;
        this.postDelayed(new autoScroll(), 20000);
    }

    public void stopAutoScroll() {
        auto = false;
    }
}
