package com.wasu.cs.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wasu.cs.model.SpecialColumnModel;
import com.wasu.util.StringUtils;

import cn.com.wasu.main.R;

/**
 * 
 * @ClassName: MainTabItem 
 * @Description: 标记区的Item，例如点播、推荐等等
 * @author 海迪
 * @date 2015年6月16日 下午1:15:12 
 *
 */
public class MainTabItem extends LinearLayout {
	
	private TabLinearLayout tabLinearLayout;
	private int index = -1;
	
	private TextView titleView;
	private ImageView lableView;
	private int keyCode;
	
	public MainTabItem(Context context) {
		super(context);
		init(context);
	}

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

	public MainTabItem(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}
	
	/**
	 * 
	 * @author 海迪
	 * @Title: init 
	 * @Description: 初始化Item UI, 并添加监听焦点改变事件
	 * @param @param context
	 * @return void
	 * @throws 
	 *
	 * @param context
	 */
	private void init(Context context) {
		
		LayoutInflater.from(context).inflate(R.layout.main_tab_item, this);
		int w = LayoutParams.WRAP_CONTENT;
		int h = getResources().getDimensionPixelSize(R.dimen.d_44dp);
		int padding = getResources().getDimensionPixelSize(R.dimen.d_10dp);
		setLayoutParams(new LayoutParams(w, h));
		setOrientation(LinearLayout.VERTICAL);
		setGravity(Gravity.CENTER);
		setPadding(padding, 0, padding, 0);
		
		setFocusable(true);
		setClickable(true);
		
		titleView = (TextView) findViewById(R.id.title);
		lableView = (ImageView) findViewById(R.id.lable);
		
		setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				int code = 0;
				MainTabItem item = null;
				if(hasFocus) {
					item = (MainTabItem) v;
					setSelectedStyle(true, true);
				} else {
					setSelectedStyle(false, true);
					if(keyCode == 20 || keyCode == 19) {
						code = keyCode;
					}
					
				}
				
				if(tabLinearLayout != null) {
					tabLinearLayout.setFocusedItemIndex(item, index, code);
				}
			}
		});
	}
	
	
	
	
	/**
	 * 
	 * @author 海迪
	 * @Title: setSelectedStyle 
	 * @Description: 焦点发送改变时，修改Item的Style
	 * @param @param isSelected
	 * @return void
	 * @throws 
	 *
	 * @param isSelected
	 */
	public void setSelectedStyle(boolean isFocused, boolean isSelected) {
		int color = -1;
		int resId = -1;
		if(isFocused) {
			color = getResources().getColor(R.color.yellow);
			resId = R.drawable.transparent;
		} else if(isSelected) {
			color = getResources().getColor(R.color.yellow);
			resId = R.color.yellow;
		} else {
			color = getResources().getColor(R.color.white);
			resId = R.drawable.transparent;
		}
		
		titleView.setTextColor(color);
		lableView.setImageResource(resId);
	}
	
	/**
	 * 
	 * @author 海迪
	 * @Title: initData 
	 * @Description: 初始化Item的标题
	 * @param @param scm
	 * @return void
	 * @throws 
	 *
	 * @param scm
	 */
	public void initData(SpecialColumnModel scm) {
		if(scm == null) return;
		
		String title = scm.getTitle();
		title = StringUtils.isBlank(title) ? "" : title.trim();
		
		titleView.setText(title);
		titleView.setContentDescription(title);
	}
	
	/**
	 * 复写onKeyDown，并在Item中记录keyCode
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		this.keyCode = keyCode;
		return super.onKeyDown(keyCode, event);
	}

	public void setTabLinearLayout(TabLinearLayout tabLinearLayout) {
		this.tabLinearLayout = tabLinearLayout;
	}

	public void setIndex(int index) {
		this.index = index;
	}

}
