package com.saki.ui;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

public class Banner extends RelativeLayout {

    public class Indicator extends View implements OnPageChangeListener {
        private int mPosition = 0;
        private float offset = 0;
        private Paint paint;
        Random random = new Random();

        public Indicator(Context context) {
            super(context);
            LayoutParams params = new LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 4);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            this.setLayoutParams(params);
            this.paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.RED);
        }

        public int getPosition() {
            return this.mPosition;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (pager.getAdapter() != null) {
                int count = pager.getAdapter().getCount();
                float width = this.getWidth() / count;
                canvas.drawRect((mPosition + offset) * width, 0,
                        (mPosition + 1 + offset) * width, this.getHeight(),
                        paint);
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            setPostion(arg0, arg1);
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageSelected(int arg0) {
        }

        private void setPostion(int position, float offset) {
            mPosition = position;
            this.offset = offset;
            this.invalidate();
        }
    }

    private Indicator indicator;

    private ViewPager pager;

    public Banner(Context context) {
        super(context);
        this.addView(pager = new ViewPager(context));
        pager.setBackgroundColor(Color.BLUE);
        this.addView(indicator = new Indicator(context));
        LayoutParams p = new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        pager.setLayoutParams(p);
        pager.setOnPageChangeListener(indicator);
    }

    public Banner(Context context, AttributeSet attr) {
        super(context, attr);
        this.addView(pager = new ViewPager(context));
        pager.setBackgroundColor(Color.BLUE);
        this.addView(indicator = new Indicator(context));
        LayoutParams p = new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        pager.setLayoutParams(p);
        pager.setOnPageChangeListener(indicator);
    }

    public Indicator getIndicator() {
        return this.indicator;
    }

    public ViewPager getPager() {
        return this.pager;
    }
}
