package com.wasu.cs.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

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

import cn.com.wasu.main.R;

/**
 * 
 * @ClassName: MainHPageItem11
 * @Description: 内容区1*1尺寸的Item
 * @author 海迪
 * @date 2015年7月8日 下午5:31:55
 * 
 */
public class MainHPageItem11 extends MainHPageItemBase implements View.OnClickListener {

	private String layout;
	private String jsonUrl;
	private String csLayout;
	private String csJsonUrl;

	private int carouselTime;

	private ImageView contFirstImgView;
	private ImageView contSecondImgView;
	private ImageView cornerImgView;

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

	private void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.main_hpage_item_11, this);

		int w = getResources().getDimensionPixelSize(R.dimen.d_206dp);
		int h = getResources().getDimensionPixelSize(R.dimen.d_135dp);
		setLayoutParams(new LayoutParams(w, h));
		contFirstImgView = (ImageView) findViewById(R.id.cont_first_img);
		contSecondImgView = (ImageView) findViewById(R.id.cont_second_img);
		cornerImgView = (ImageView) findViewById(R.id.corner_img);
	}

	@Override
	public String getLayout() {
		if(!TextUtils.isEmpty("CNLTV_Subject") && csLayout.equalsIgnoreCase("CNLTV_Subject")){

			return csLayout;

		}else {
			return layout;
		}
	}

	@Override
	public String getJsonUrl() {
		if(!TextUtils.isEmpty("CNLTV_Subject") && csLayout.equalsIgnoreCase("CNLTV_Subject")){

			return csJsonUrl;

		}else {
			return jsonUrl;
		}
	}

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

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

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

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

	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();
		csLayout = cm.getCsLayout();
		csJsonUrl = cm.getCsJsonUrl();
		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 secondImgUrl = null;
		if (cm.getPicUrlList().size() > 1) {
			secondImgUrl = cm.getPicUrlList().get(1).getBottomPic();
			if (StringUtils.isBlank(secondImgUrl))
				secondImgUrl = null;
		}

		if (contSecondImgView != null && !StringUtils.isBlank(secondImgUrl)) {
//			ImageFetcherModule.getInstance().attachImage(secondImgUrl, contSecondImgView, getResources().getDimensionPixelSize(R.dimen.d_8dp));
			FrescoImageFetcherModule.getInstance().attachImage(secondImgUrl, contSecondImgView);
			contSecondImgView.setVisibility(ImageView.VISIBLE);
		} else {
			contSecondImgView = null;
		}

		String cornerImgUrl = cm.getCornerIcon();
		if (cornerImgView != null && !StringUtils.isBlank(cornerImgUrl)) {
			FrescoImageFetcherModule.getInstance().attachImage(cornerImgUrl, cornerImgView);
			cornerImgView.setVisibility(ImageView.VISIBLE);
		}

		carouselTime = cm.getCarouselTime() * 1000;

		// 图片轮播
		if (contSecondImgView != null && contFirstImgView != null && carouselTime > 0) {
			mHandler.sendEmptyMessageDelayed(MSG_CAROUSEL_IMG, carouselTime);
		}

	}

	private boolean carouselTag = true;
	private final static int MSG_CAROUSEL_IMG = 0;

	/**
	 * Item图片轮播
	 */
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_CAROUSEL_IMG:
				if (carouselTag) {
					startAnimation();
				} else {
					reverseAnimation();

				}

				carouselTag = !carouselTag;
				mHandler.sendEmptyMessageDelayed(MSG_CAROUSEL_IMG, carouselTime);
				break;

			default:
				break;
			}
		};
	};

	/**
	 * 绑定轮播动画
	 */
	public void bindAnimations() {
		animSet = new AnimatorSet();
		if (contSecondImgView != null && contFirstImgView != null) {
			// float cfivY = contFirstImgView.getY();
			// float cfivWidth = contFirstImgView.getHeight();
			// Log.d("echo", "cfivY - cfivWidth"+(cfivY - cfivWidth));
			// ObjectAnimator translateY = ObjectAnimator.ofFloat(
			// contFirstImgView, "y", cfivY - cfivWidth);
			ObjectAnimator alpha2 = ObjectAnimator.ofFloat(contFirstImgView, "alpha", 1f, 0f);
			alpha2.setStartDelay(300);
			// translateY.setStartDelay(300);
			animSet.play(alpha2);
			// animSet.play(translateY);
		}
	}

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

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

	}

}
