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.LinearLayout;
import android.widget.TextView;

import cn.com.wasu.main.R;

import com.wasu.widget.util.UIUtil;

import org.w3c.dom.Text;

/**
 * 
 * @ClassName: TabLinearLayout
 * @Description: 首页顶部标签区（推荐， 点播...）
 * @author 海迪
 * @date 2015年7月25日 下午5:10:09
 * 
 */
public class TabLinearLayout extends LinearLayout {

	private Context context;

	private OnItemFocusedListener onItemFocusedListener;

	private int focusedItemIndex = -1;
	private MainTabItem focusItem = null;

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

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

	public TabLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	private void init(Context context) {
		this.context = context;
		setChildrenDrawingOrderEnabled(true);
		//测试代码，设置
		TextView textView = new TextView(context);
		textView.setText(context.getText(R.string.patch_info));
		addView(textView);
	}

	private int mFocusEdgeOffset = getResources().getDimensionPixelSize(
			R.dimen.d_0dp);
	Drawable focusShadowDrawable = null;

	/**
	 * 覆盖dispatchDraw，并绘制焦点样式
	 */
	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);

		if (focusItem != null) {
			if (focusShadowDrawable == null)
				focusShadowDrawable = context.getResources().getDrawable(
						R.drawable.main_tab_item_selected);

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

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

	/**
	 * 修改重绘子Item的次序
	 */
	@Override
	protected int getChildDrawingOrder(int childCount, int i) {
		if (focusedItemIndex < 0)
			return i;

		if (i < (childCount - 1)) {
			if (focusedItemIndex == i)
				i = childCount - 1;
		} else {
			if (focusedItemIndex < childCount)
				i = focusedItemIndex;
		}

		return i;
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: setItemFocused
	 * @Description: 设置指定项获取焦点
	 * @param @param index
	 * @return void
	 * @throws
	 * 
	 * @param index
	 */
	public void setItemFocused(int index) {
		if (getChildCount() > index) {
			getChildAt(index).requestFocus();
		}
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: setItemSelected
	 * @Description: 设置第index个子Item为选中，并且只有选中项可以获取焦点、单击
	 * @param @param index
	 * @return void
	 * @throws
	 * 
	 * @param index
	 */
	/*public void setItemSelected(int index) {

		if (isFoucsed)
			return;

		MainTabItem tabItem = null;
		for (int i = 0; i < getChildCount(); i++) {
			View view = getChildAt(i);

			if (view instanceof MainTabItem) {
				tabItem = (MainTabItem) view;
			}

			if (tabItem != null) {
				boolean tag = false;

				if (i == index)
					tag = true;

				tabItem.setFocusable(tag);
				tabItem.setSelected(tag);
				// tabItem.setSelectedStyle(tag);
			}

			tabItem = null;
		}
	}*/

	public interface OnItemFocusedListener {
		public void OnItemFocused(MainTabItem focusItem, int index);
	}

	public void setOnItemFocusedListener(
			OnItemFocusedListener onItemFocusedListener) {
		this.onItemFocusedListener = onItemFocusedListener;
	}
	
	private boolean hasFocused = false;

	public void setFocused(boolean focus) {
		hasFocused = focus;
	}

	public boolean getFocused() {
		return hasFocused;
	}

	public void setChildrenFocusable(int index) {

		for (int i = 0; i < getChildCount(); i++) {
			View view = getChildAt(i);

			if (view != null) {
				view.setFocusable(true);
			}
		}
	}

	public void setSelectedChildStyle(int index) {
		if (index < 0 || index >= getChildCount())
			return;

		MainTabItem tabItem = null;
		for (int i = 0; i < getChildCount(); i++) {
			View view = getChildAt(i);

			if (view instanceof MainTabItem) {
				tabItem = (MainTabItem) view;
			}

			if (tabItem != null) {
				boolean tag = (i == index) ? true : false;

				tabItem.setFocusable(tag);
				tabItem.setSelectedStyle(false, tag);
			}

			tabItem = null;
		}
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: setFocusedItemIndex
	 * @Description: 设置获取焦点项，并重新刷新UI、修改样式
	 * @param @param focusItem
	 * @param @param focusedItemIndex
	 * @param @param keyCode
	 * @return void
	 * @throws
	 * 
	 * @param focusItem
	 * @param focusedItemIndex
	 * @param keyCode
	 */
	public void setFocusedItemIndex(MainTabItem focusItem,
			int focusedItemIndex, int keyCode) {
		if (focusItem != null && onItemFocusedListener != null) {
			onItemFocusedListener.OnItemFocused(focusItem, focusedItemIndex);
		}

		this.focusItem = focusItem;
		this.focusedItemIndex = focusedItemIndex;
		this.postInvalidate();
		
		if(focusItem != null) {
			setChildrenFocusable(focusedItemIndex);
		}
		
		hasFocused = false;
		if(focusItem != null) {
			hasFocused = true;
			for (int i = 0; i < getChildCount(); i++) {
				View view = getChildAt(i);
				view.setFocusable(true);
			}
			focusItem.setSelectedStyle(true, false);
		} else if(keyCode == 20 || keyCode == 19){
			setSelectedChildStyle(focusedItemIndex);
		}

	}

}
