package com.wasu.cs.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.HorizontalScrollView;

import com.wasu.cs.adapter.ContAdapter;
import com.wasu.cs.widget.TabLinearLayout.OnItemFocusedListener;

import cn.com.wasu.main.AppUtil;
import cn.com.wasu.main.R;

/**
 * 
 * @ClassName: MainTabBar 
 * @Description: 首页标签区TabBar控件
 * @author 海迪
 * @date 2015年6月16日 下午12:28:16 
 *
 */
public class MainTabBar extends HorizontalScrollView {
	
	private TabLinearLayout tabBox;
	
	private boolean isFirstLoad = false;
	private ContAdapter contAdapter;

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

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

	public MainTabBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}
	
	/**
	 * 
	 * @author 海迪
	 * @Title: init 
	 * @Description: 初始化， 引入布局文件，并添加监听事件
	 * @param @param context 
	 * @return void 
	 * @throws 
	 *
	 * @param context
	 */
	private void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.main_tab_bar, this);
		
		setHorizontalScrollBarEnabled(false);
		setVerticalScrollBarEnabled(false);
		
		tabBox = (TabLinearLayout) findViewById(R.id.tab_box);
		tabBox.setOnItemFocusedListener(new OnItemFocusedListener() {
			
			@Override
			public void OnItemFocused(MainTabItem focusItem, int index) {
				
				if(onItemSelectedListener != null) {
					onItemSelectedListener.onItemSelected(focusItem, index);
				}
			}
		});
		
		ViewTreeObserver vto = this.getViewTreeObserver();
		/**
		 * 监听Draw之前，设置tabBox的宽度
		 */
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
            	
            	if(!isFirstLoad) {
            		tabBox.setMinimumWidth(getMeasuredWidth());
            		isFirstLoad = true;
            	}
   
                return true;
            }
        });
	}
	
	public void addView(View child) {
		tabBox.addView(child);
	}
	
	public void addView(View child, int index) {
		tabBox.addView(child, index);
	}
	
	public void removeView(View child) {
		tabBox.removeView(child);
	}
	
	public void removeViewAt(int index) {
		tabBox.removeViewAt(index);
	}
	
	public void setAdapter(ContAdapter contAdapter) {
		this.contAdapter = contAdapter;
		initChildControls();
	}
	
	/**
	 * 
	 * @author 海迪
	 * @Title: initChildControls 
	 * @Description: 生成并布局Item的位置
	 * @param 
	 * @return void 
	 * @throws 
	 *
	 */
	private void initChildControls() {
		int count = contAdapter.getCount();
		View view = null;
		MainTabItem tabItem = null;
		int index = 0;
		MainTabItem firstItem = null;	
		MainTabItem lastItem = null;
		for(int i=0; i<count; i++) {
			view = contAdapter.getView(i, null, null);
			
			if(view != null && view instanceof MainTabItem) {
				tabItem = (MainTabItem) view;
				tabItem.setTag(i);
				tabItem.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if(onItemClickListener != null) {
							int position = (Integer) v.getTag();
							onItemClickListener.onItemClick(position, v);
							tabBox.setFocusedItemIndex(null, position, 20);
						}
					}
				});
				
				tabItem.setId(AppUtil.createId());
				tabItem.setTabLinearLayout(tabBox);
				tabItem.setIndex(index++);
				
				tabBox.addView(tabItem);
				
				if(i == 0) {
					firstItem = tabItem;
				}
				
				if(i == count-1) {
					lastItem = tabItem;
				}
			}
		}
		
		if(firstItem != null && lastItem != null && firstItem != lastItem) {
			//取消左右循环
			lastItem.setNextFocusRightId(firstItem.getId());
			firstItem.setNextFocusLeftId(lastItem.getId());
//			lastItem.setNextFocusRightId(lastItem.getId());
//			firstItem.setNextFocusLeftId(firstItem.getId());
		}
	}
	
	private OnItemSelectedListener onItemSelectedListener;
	private OnItemClickListener onItemClickListener;

	public void setOnItemSelectedListener(
			OnItemSelectedListener onItemSelectedListener) {
		this.onItemSelectedListener = onItemSelectedListener;
	}
	
	public void setOnItemClickListener(
			OnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}
	
	/**
	 * 
	 * @ClassName: OnItemSelectedListener 
	 * @Description: MainTabBar Item选中时监听对象
	 * @author 海迪
	 * @date 2015年7月25日 下午5:17:50 
	 *
	 */
	public interface OnItemSelectedListener {
		public void onItemSelected(View view, int position);
	}
	
	/**
	 * 
	 * @ClassName: OnItemClickListener 
	 * @Description: MainTabBar Item单击时监听对象
	 * @author 海迪
	 * @date 2015年7月25日 下午5:18:57 
	 *
	 */
	public interface OnItemClickListener {
		public void onItemClick(int position, View view);
	}
	
	/**
	 * 
	 * @author 海迪
	 * @Title: setItemFocused 
	 * @Description: 设置第index个子Item获取焦点
	 * @param @param index
	 * @return void
	 * @throws 
	 *
	 * @param index
	 */
	public void setItemFocused(int index) {
		tabBox.setItemFocused(index);
	}
	public MainTabItem getFocusItem(){
		return (MainTabItem) tabBox.getFocusedChild();
	}
	/**
	 * 
	 * @author 海迪
	 * @Title: setItemSelected 
	 * @Description: 设置第index个子Item为选中
	 * @param @param index
	 * @return void
	 * @throws 
	 *
	 * @param index
	 */
	public void setItemSelected(int index) {
		if(!tabBox.getFocused()) {
			tabBox.setSelectedChildStyle(index);
		}
	}
	public MainTabItem getItem(int position){
		return (MainTabItem) tabBox.getChildAt(position);
	}
	/**
	 * 
	 * @author 海迪
	 * @Title: releaseResources 
	 * @Description: 释放占用资源
	 * @param 
	 * @return void 
	 * @throws 
	 *
	 */
	public void releaseResources() {
		if(tabBox != null) {
			tabBox.removeAllViews();
			tabBox = null;
		}
		
		if(contAdapter != null) {
			contAdapter.clearData();
			contAdapter = null;
		}
	}

}
