package com.wasu.cs.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;
import cn.com.wasu.main.R;

import com.wasu.widget.util.UIUtil;

public class ChannelListView extends ListView {

	private Context context;
	private View focusItem = null;

	public ChannelListView(Context context) {
		super(context);
		init(context);
	}

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

	private void init(Context context) {
		this.context = context;
	}

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        isDraw = gainFocus;
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

    private boolean isDraw = false;
	/**去掉按键监听，避免listview左边没有控件时按左键焦点框丢失**/
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
//			isDraw = false;
//		}
//		return super.onKeyDown(keyCode, event);
//	}

	public void setFocusItem(View view) {
		this.focusItem = view;
		this.postInvalidate();
	}

    public void setShadowDrawable(int resId) {
        this.shadowDrawable = context.getResources().getDrawable(resId);
    }

    public void setFocusEdgeOffset(int offset) {
        this.mFocusEdgeOffset = offset;
    }

	private Drawable shadowDrawable;
	private int mFocusEdgeOffset = getResources().getDimensionPixelSize(R.dimen.d_2dp);

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		if (focusItem != null && isDraw) {
			if (shadowDrawable == null)
				shadowDrawable = context.getResources().getDrawable(
						R.drawable.tv_select_focus);

			Rect toFocusedViewRect = UIUtil.createViewRect(this, focusItem, mFocusEdgeOffset);

			UIUtil.drawDrawableAt(canvas, toFocusedViewRect, shadowDrawable,true);
		}
	}

}
