package com.wasu.cs.widget;

import com.wasu.cs.model.FilmSpecialModel;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;
import com.wasu.util.StringUtils;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.com.wasu.main.IntentMap;
import cn.com.wasu.main.R;

/**
 * 
 * @ClassName: FilmSpecialItem 
 * @Description: 影视剧专题模板Item
 * @author 海迪
 * @date 2015年7月25日 下午5:34:44 
 *
 */
public class FilmSpecialItem extends RelativeLayout {
	
	private ImageView firstImgView;
	private TextView titleView;
	private TextView gradeIntegerView;
	private TextView gradeDecimalView;
	private int index = -1;
	private Context context;
	
	private FilmSpecialModel model;

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

	/**
	 * 
	 * @author 海迪
	 * @Title: init 
	 * @Description: 创建Item时，添加布局，并初始化事件
	 * @param @param c
	 * @return void
	 * @throws 
	 *
	 * @param c
	 */
	private void init(Context c) {
		context = c;
		LayoutInflater.from(c).inflate(R.layout.film_special_item, this);
		setFocusable(true);
		setClickable(true);
		
		firstImgView = (ImageView) findViewById(R.id.cont_first_img);
		titleView = (TextView) findViewById(R.id.title);
		gradeIntegerView = (TextView) findViewById(R.id.grade_integer);
		gradeDecimalView = (TextView) findViewById(R.id.grade_decimal);

		setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				resetTitleStyle(hasFocus);
				if (hasFocus) {
					startAnimation();
					horizontalListView.setFocusedItemIndex((FilmSpecialItem)v, index);
				} else {
					reverseAnimation();
					horizontalListView.setFocusedItemIndex(null, index);
				}
			}

		});
		
		/**
		 * 添加单击处理
		 */
		setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				IntentMap.startIntent(context, null, model.getLayout(),
						model.getJsonUrl(), null);
			}
		});
	}
	
	/**
	 * 
	 * @author 海迪
	 * @Title: resetTitleStyle 
	 * @Description: 焦点发送变化时，修改显示样式
	 * @param @param hasFocus
	 * @return void
	 * @throws 
	 *
	 * @param hasFocus
	 */
	private void resetTitleStyle(boolean hasFocus) {
		if(titleView == null) return;
		
		if(hasFocus) {
			titleView.setMaxLines(2);
			titleView.setText(model.getSummary());
			titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.d_15dp));
			titleView.setTextColor(context.getResources().getColorStateList(R.color.white));
		} else {
			titleView.setMaxLines(1);
			titleView.setText(model.getTitle());
			titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.d_20dp));
			titleView.setTextColor(context.getResources().getColorStateList(R.color.white_alpha_08));
		}
	}

	private void startAnimation() {

		this.setScaleX(1.15f);
		this.setScaleY(1.15f);
	}

	private void reverseAnimation() {

		this.setScaleX(1.0f);
		this.setScaleY(1.0f);
	}
	
	/**
	 * 
	 * @author 海迪
	 * @Title: initData 
	 * @Description: 初始化Item数据（图片，描述， 评分）
	 * @param @param model
	 * @return void
	 * @throws 
	 *
	 * @param model
	 */
	public void initData(FilmSpecialModel model) {
		if(model == null) return;
		
		this.model = model;
		
		if (firstImgView != null) {
			FrescoImageFetcherModule.getInstance().attachImage(model.getPicUrl(), firstImgView,
					getResources().getDimensionPixelSize(R.dimen.d_8dp));
		}
		
		titleView.setVisibility(View.GONE);
		if(titleView != null && !StringUtils.isBlank(model.getTitle())) {
			titleView.setText(model.getTitle());
			titleView.setVisibility(View.VISIBLE);
		}
		
		String points = model.getPoints();
//		points = "8.6";
		String[] pArr = null;
		if (!StringUtils.isBlank(points)) {
			pArr = points.split("\\.");
		}

		String integer = null;
		if (pArr != null && pArr.length > 0) {
			integer = pArr[0];
		}

		String decimal = null;
		if (pArr != null && pArr.length > 1) {
			decimal = "." + pArr[1];
		}
		
		gradeIntegerView.setVisibility(View.INVISIBLE);
		gradeDecimalView.setVisibility(View.INVISIBLE);
		
		if (gradeIntegerView != null && !StringUtils.isBlank(integer)) {
			gradeIntegerView.setText(integer);
			gradeIntegerView.setVisibility(View.VISIBLE);
			if (gradeDecimalView != null && !StringUtils.isBlank(decimal)) {
				gradeDecimalView.setText(decimal);
				gradeDecimalView.setVisibility(View.VISIBLE);
			}
		}
		
	}
	
	private HorizontalListView horizontalListView;
	
	public void setHorizontalListView(HorizontalListView horizontalListView) {
		this.horizontalListView = horizontalListView;
	}
	
	public void setIndex(int index) {
		this.index = index;
	}

}
