package com.wasu.cs.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
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: MainHPageItemFront
 * @Description: 内容区异形图Item
 * @author 海迪
 * @date 2015年7月8日 下午7:23:34
 * 
 */
public class MainHPageItemFront extends MainHPageItemBase {

	private Context context;

	private String layout;
	private String jsonUrl;

	private String summary;
	private String title;

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

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

	private void init(Context context) {
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.main_hpage_item_front, this);
		setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		/*
		 * // int w = getResources().getInteger(R.integer.i_512); int w =
		 * getResources().getDimensionPixelSize(R.dimen.d_512dp); // int h =
		 * getResources().getInteger(R.integer.i_360); int h =
		 * getResources().getDimensionPixelSize(R.dimen.d_360dp);
		 * setLayoutParams(new LayoutParams(w, h));
		 */

		contFirstImgView = (ImageView) findViewById(R.id.cont_first_img);

		cornerImgView = (ImageView) findViewById(R.id.corner_img);
		focusImgView = (ImageView) findViewById(R.id.focus_img);
		frontImgView = (ImageView) findViewById(R.id.front_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);
	}

	@Override
	public void focusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			startAnimation();
		} else {
			reverseAnimation();
		}
	}

	protected void resetTitleStyle(boolean hasFocus) {
		if (titleView == null)
			return;
		setPoint(hasFocus);
		if (hasFocus) {
			titleView.setMaxLines(2);
			titleView.setText(summary);
			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.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.d_32dp));
			titleView.setTextColor(context.getResources().getColorStateList(R.color.white_alpha_08));
		}
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: startAnimation
	 * @Description: 异形图往上开始做动画
	 * @param
	 * @return void
	 * @throws
	 * 
	 */
	public void startAnimation() {
		focusImgView.setVisibility(ImageView.VISIBLE);
		this.setScaleX(1.1f);
		this.setScaleY(1.1f);

		if (animSet == null) {
			bindAnimations();
		} else {
			animSet.cancel();
		}

		animSet.start();
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: reverseAnimation
	 * @Description: 反转动画
	 * @param
	 * @return void
	 * @throws
	 * 
	 */
	public void reverseAnimation() {
		focusImgView.setVisibility(ImageView.GONE);
		this.setScaleX(1.0f);
		this.setScaleY(1.0f);

		if (animSet == null) {
			return;
		}

		animSet.cancel();
		for (Animator animator : animSet.getChildAnimations()) {
			ValueAnimator valueAnimator = (ValueAnimator) animator;
			valueAnimator.reverse();
		}
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: bindAnimations
	 * @Description: 绑定动画
	 * @param
	 * @return void
	 * @throws
	 * 
	 */
	public void bindAnimations() {
		animSet = new AnimatorSet();

		if (frontImgView != null) {
			ObjectAnimator translateY = ObjectAnimator.ofFloat(frontImgView, "y", 25);

			translateY.setStartDelay(50);
			animSet.play(translateY);
		}

		animSet.setDuration(300);
		// 头尾速率改变比较慢，中间加速
		animSet.setInterpolator(new AccelerateDecelerateInterpolator());

		// // 开始速率改变比较慢，然后逐渐减速
		// animSet.setInterpolator(new AccelerateInterpolator());
	}

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

	@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;
		String frontImgUrl = null;
		if (cm.getPicUrlList().size() > 0) {
			firstImgUrl = cm.getPicUrlList().get(0).getBottomPic();
			frontImgUrl = cm.getPicUrlList().get(0).getTopPic();
		}

		if (contFirstImgView != null && !StringUtils.isBlank(firstImgUrl)) {
			Log.d("echo", "firstImgUrl" + 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 (frontImgView != null && !StringUtils.isBlank(frontImgUrl)) {
//			ImageFetcherModule.getInstance().attachImage(frontImgUrl, frontImgView, getResources().getDimensionPixelSize(R.dimen.d_8dp));
			FrescoImageFetcherModule.getInstance().attachImage(frontImgUrl, frontImgView);
			Log.d("echo", "frontImgView" + frontImgUrl);
			frontImgView.setVisibility(ImageView.VISIBLE);

		} else {
			frontImgView = null;
		}

		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

	}
}
