package com.wasu.widgets.focuswidget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

import cn.com.wasu.main.R;

/**
 * 当你使用滚动窗口焦点错乱的时候，就可以使用这个控件.
 * <p>
 * 使用方法和滚动窗口是一样的，具体查看DEMO吧.
 * <p>
 * 如果想改变滚动的系数，R.dimen.fading_edge
 * <p>
 * 
 * @author danxingxi
 *
 */
public class SmoothHorizontalScrollView extends HorizontalScrollView {
	final String TAG = "SmoothHorizontalScrollView";

	public SmoothHorizontalScrollView(Context context) {
		this(context, null, 0);
		init();
	}

	public SmoothHorizontalScrollView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		init();
	}

	public SmoothHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init(){
		setSmoothScrollingEnabled(true);
	}

	@Override
	public void fling(int velocityX) {
		super.fling(velocityX*6);
	}

	@Override
	protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
		if (getChildCount() == 0)
			return 0;

		int width = getWidth();
		int screenLeft = getScrollX();
		int screenRight = screenLeft + width;

		int fadingEdge = this.getResources().getDimensionPixelSize(R.dimen.d_230dp);

		// leave room for left fading edge as long as rect isn't at very left
		if (rect.left > 0) {
			screenLeft += fadingEdge;
		}

		// leave room for right fading edge as long as rect isn't at very right
		if (rect.right < getChildAt(0).getWidth()) {
			screenRight -= fadingEdge;
		}

		int scrollXDelta = 0;

		if (rect.right > screenRight && rect.left > screenLeft) {
			// need to move right to get it in view: move right just enough so
			// that the entire rectangle is in view (or at least the first
			// screen size chunk).

			if (rect.width() > width) {
				// just enough to get screen size chunk on
				scrollXDelta += (rect.left - screenLeft);
			} else {
				// get entire rect at right of screen
				scrollXDelta += (rect.right - screenRight);
			}

			// make sure we aren't scrolling beyond the end of our content
			int right = getChildAt(0).getRight();
			int distanceToRight = right - screenRight;
			scrollXDelta = Math.min(scrollXDelta, distanceToRight);

		} else if (rect.left < screenLeft && rect.right < screenRight) {
			// need to move right to get it in view: move right just enough so
			// that
			// entire rectangle is in view (or at least the first screen
			// size chunk of it).

			if (rect.width() > width) {
				// screen size chunk
				scrollXDelta -= (screenRight - rect.right);
			} else {
				// entire rect at left
				scrollXDelta -= (screenLeft - rect.left);
			}

			// make sure we aren't scrolling any further than the left our
			// content
			scrollXDelta = Math.max(scrollXDelta, -getScrollX());
		}
		return scrollXDelta;
	}

}
