package com.wasu.cs.widget;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

import cn.com.wasu.main.R;

/**
 * 
 * @ClassName: MainHPageItem22 
 * @Description: 内容区2*2尺寸的Item
 * @author 海迪
 * @date 2015年7月8日 下午6:26:55 
 *
 */
public class MainHPageItem22 extends MainHPageItemBase {
	
	private Context context;

	private String layout;
	private String jsonUrl;
	
	private String summary;
	private String title;

	private ImageView contFirstImgView;
	private ImageView cornerImgView;
	private LinearLayout textBox;
	private TextView titleView;
	private TextView gradeIntegerView;
	private TextView gradeDecimalView;

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

	private void init(Context context) {
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.main_hpage_item_22, this);
		int w = getResources().getDimensionPixelSize(R.dimen.d_422dp);
		int h = getResources().getDimensionPixelSize(R.dimen.d_270dp);
		setLayoutParams(new LayoutParams(w, h));

		contFirstImgView = (ImageView) findViewById(R.id.cont_first_img);
		cornerImgView = (ImageView) findViewById(R.id.corner_img);
		textBox = (LinearLayout) findViewById(R.id.text_box);
		titleView = (TextView) findViewById(R.id.title);
		gradeIntegerView = (TextView) findViewById(R.id.grade_integer);
		gradeDecimalView = (TextView) findViewById(R.id.grade_decimal);
	}
	
	/**
	 * 根据是否获取焦点，修改Item样式，修改放大倍数
	 */
	protected void resetTitleStyle(boolean hasFocus) {
		if(titleView == null) return;
		setPoint(hasFocus);
		if(hasFocus) {
			titleView.setMaxLines(2);
			titleView.setText(summary);
			titleView.setGravity(Gravity.CENTER_VERTICAL);
			titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.d_18dp));
			titleView.setTextColor(context.getResources().getColorStateList(R.color.white));
		} else {
			titleView.setMaxLines(1);
			titleView.setText(title);
			titleView.setGravity(Gravity.CENTER);
			titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.d_32dp));
			titleView.setTextColor(context.getResources().getColorStateList(R.color.white_alpha_08));
		}
	}

	@Override
	public String getLayout() {
		return layout;
	}

	@Override
	public String getJsonUrl() {
		return jsonUrl;
	}

	@Override
	public int getRowspan() { // 行
		return 2;
	}

	@Override
	public int getColspan() { // 列
		return 2;
	}

	@Override
	public boolean isFrontItem() {
		return false;
	}

	@Override
	public boolean isVideoItem() {
		return false;
	}
	
	@Override
	protected void sendStatistical() {
		if(cm == null) return;
		
		/*if(TextUtils.isEmpty(cm.getTraceid())){
			WasuStatistics.getInstance().itemClick("", false, "", null, null, index+"");
		}else{
			WasuStatistics.getInstance().itemClick(cm.getEid() + "", false, cm.getTraceid(), null, null, index+"");
		}*/
	}
	
	private HomeColumnModel cm;

	@Override
	public HomeColumnModel getData() {
		return cm;
	}

	@Override
	public void initData(HomeColumnModel cm) {
		this.cm = cm;
		
		if (cm == null) return;
		
		layout = cm.getLayout();
		jsonUrl = cm.getJsonUrl();

		String firstImgUrl = null;
		if (cm.getPicUrlList().size() > 0) {
			firstImgUrl = cm.getPicUrlList().get(0).getBottomPic();
		}

		if (contFirstImgView != null && !StringUtils.isBlank(firstImgUrl)) {
//			ImageFetcherModule.getInstance().attachImage(firstImgUrl,
//					contFirstImgView, getResources().getDimensionPixelSize(R.dimen.d_8dp));
			FrescoImageFetcherModule.getInstance().attachImage(firstImgUrl,
					contFirstImgView);
		}
		
		String cornerImgUrl = cm.getCornerIcon();
		if (cornerImgView != null && !StringUtils.isBlank(cornerImgUrl)) {
			FrescoImageFetcherModule.getInstance().attachImage(cornerImgUrl,
					cornerImgView);
			cornerImgView.setVisibility(ImageView.VISIBLE);
		}
		if (cm.getParentLayout().equals("Home_Rec"))  {
		summary = cm.getSummary();
		title = cm.getTitle();
		
		String points = cm.getPoints();
		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];
		}
		
		if(!StringUtils.isBlank(summary) || !StringUtils.isBlank(title) || !StringUtils.isBlank(integer)) {
			textBox.setVisibility(RelativeLayout.VISIBLE);
		} else {
			titleView = null;
			gradeIntegerView = null;
			gradeDecimalView = null;
		}
		
		if (titleView != null && (!StringUtils.isBlank(summary) || !StringUtils.isBlank(title))) {
			titleView.setText(title);
			titleView.setIncludeFontPadding(false);
			titleView.setVisibility(TextView.VISIBLE);
		} else {
			titleView = null;
		}
		
		if (gradeIntegerView != null && !StringUtils.isBlank(integer)) {
			gradeIntegerView.setText(integer);

			if (gradeDecimalView != null && !StringUtils.isBlank(decimal)) {
				gradeDecimalView.setText(decimal);
			}
		}
		setPoint(false);
		}
	}
	private void setPoint(boolean hasfocus){
		if (hasfocus) {
			if (gradeIntegerView != null)
				gradeIntegerView.setVisibility(TextView.VISIBLE);
			if (gradeDecimalView != null)
                gradeDecimalView.setVisibility(TextView.VISIBLE);
				
		}else{
			if (gradeIntegerView != null)
			   gradeIntegerView.setVisibility(TextView.GONE);
			if (gradeDecimalView != null)
               gradeDecimalView.setVisibility(TextView.GONE);
		}

	}

	@Override
	public boolean isAliRecItem() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void notifyPlay(boolean isToPlay) {
		// TODO Auto-generated method stub
		
	}
}
