package com.wasu.cs.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
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: MainHPageItem21
 * @Description: 内容区2*1尺寸的Item
 * @author 海迪
 * @date 2015年7月8日 下午7:23:56
 * 
 */
public class MainHPageItem21 extends MainHPageItemBase {

	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 summaryView;

	private int aliRecommend;

	private boolean isAliRecommend;

	public boolean isAliRecommend() {
		return isAliRecommend;
	}

	public void setAliRecommend(boolean isAliRecommend) {
		this.isAliRecommend = isAliRecommend;
	}

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

	private void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.main_hpage_item_21, this);
		// int w = getResources().getInteger(R.integer.i_206);
		int w = getResources().getDimensionPixelSize(R.dimen.d_206dp);
		// int h = getResources().getInteger(R.integer.i_270);
		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);
		summaryView = (TextView) findViewById(R.id.summary);

	}

	/**
	 * 根据是否获取焦点，修改Item样式，修改放大倍数
	 */
	protected void resetTitleStyle(boolean hasFocus) {
		if (summaryView == null) {
			return;
		}
		if (hasFocus) {
		
			summaryView.setVisibility(View.VISIBLE);

		} else {
			summaryView.setVisibility(View.GONE);
		}
	}

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

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

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

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

	@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;
	}

	public void notifyData(HomeColumnModel cm) {
		this.cm = cm;
		if (cm == null)
			return;
		layout = cm.getLayout();
		jsonUrl = cm.getJsonUrl();
		aliRecommend = cm.getAliRecommend();
		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);
		}

		summary = cm.getSummary();
		title = cm.getTitle();

		if (!StringUtils.isBlank(summary) || !StringUtils.isBlank(title)) {
			textBox.setVisibility(RelativeLayout.VISIBLE);
		} else {
			titleView = null;
			summaryView = null;
		}

		if (titleView != null && (!StringUtils.isBlank(title))) {
			titleView.setText(title);
		} else {
			titleView = null;
		}

		if (summaryView != null && !StringUtils.isBlank(summary)) {
			summaryView.setText(summary);

		} else {
			summaryView = null;
		}
		invalidate();

	}

	@Override
	public void initData(HomeColumnModel cm) {

		this.cm = cm;

		if (cm == null)
			return;

		layout = cm.getLayout();
		jsonUrl = cm.getJsonUrl();
		aliRecommend = cm.getAliRecommend();

		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();

			if (!StringUtils.isBlank(summary) || !StringUtils.isBlank(title)) {
				textBox.setVisibility(RelativeLayout.VISIBLE);
			} else {
				titleView = null;
				summaryView = null;
			}

			if (titleView != null && (!StringUtils.isBlank(title))) {
				titleView.setText(title);
			} else {
				titleView = null;
			}

			if (summaryView != null && !StringUtils.isBlank(summary)) {
				summaryView.setText(summary);

			} else {
				summaryView = null;
			}

		}

		if (aliRecommend == 1) {
			setAliRecommend(true);
		} else {
			setAliRecommend(false);
		}
	}

	@Override
	public boolean isAliRecItem() {

		return isAliRecommend;
		// return true;
	}

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

	}

}
