package com.wasu.cs.widget;

import org.json.JSONException;
import org.json.JSONObject;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.Toast;
import cn.com.wasu.main.IntentMap;

import com.wasu.cs.jsobject.WR;
import com.wasu.cs.model.HomeColumnModel;
import com.wasu.cs.module.InstallOtherAppModule;
import com.wasu.cs.widget.MainHPageView.OnHomeItemClickListener;

/**
 * 
 * @ClassName: MainHPageItemBase
 * @Description: 首页内容区所有Item的基类
 * @author 海迪
 * @date 2015年7月25日 下午5:29:09
 * 
 */
public abstract class MainHPageItemBase extends RelativeLayout implements OnClickListener {

	private Context context;

	protected int index = -1;
	protected PageRelativeLayout pageRelativeLayout;

	public abstract String getLayout();

	public abstract String getJsonUrl();

	public abstract int getRowspan();

	public abstract int getColspan();

	public abstract boolean isFrontItem();

	public abstract boolean isVideoItem();

	public abstract boolean isAliRecItem();

	public abstract void initData(HomeColumnModel cm);

	public abstract void notifyPlay(boolean isToPlay);

	public abstract HomeColumnModel getData();

	private OnHomeItemClickListener onHomeItemClickL;

	public MainHPageItemBase(Context context) {
		super(context);
		_init(context);
	}

	public void setOnHomeItemClickListener(OnHomeItemClickListener onHomeItemClickL) {
		this.onHomeItemClickL = onHomeItemClickL;
	}

	private void _init(Context context) {
		this.context = context;

		setFocusable(true);
		setClickable(true);

		/**
		 * 添加Item的单击事件
		 */
		this.setOnClickListener(this);

		/**
		 * 添加Item焦点发送变化事件
		 */
		this.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (pageRelativeLayout == null)
					return;

				sendStatistical();

				resetTitleStyle(hasFocus);

				if (isVideoItem()) {// 如果是视频小窗口或者是异性图
					pageRelativeLayout.setVideoFocusedItemIndex(v, index, hasFocus);
				} else if (isFrontItem()) {
					pageRelativeLayout.setSpecialFocusedItemIndex(v, index, hasFocus);
				} else {
					pageRelativeLayout.setFocusedItemIndex(v, index, hasFocus);
				}

				focusChange(v, hasFocus);
			}
		});

	}

	/**
	 * Item被单击时调用
	 */
	@Override
	public void onClick(View v) {

		if (onHomeItemClickL != null) {
			onHomeItemClickL.onHomeItemClick(index);
		}

		if ("Column_Detail_List1".equalsIgnoreCase(getLayout())) {
			/** 跳转携带数据，防止从首页跳转到三级列表后显示栏目名称 **/
			Intent intent = new Intent();
			intent.putExtra("Column_Detail_List1", "Column_Detail_List1");
			IntentMap.startIntent(context, intent, getLayout(), getJsonUrl(), null);
		} else {
			if ("OpenApp".equalsIgnoreCase(getLayout())) {// 如果打开第三方app,首次安装无需跳转界面

				JSONObject jsonObj = null;
				try {
					jsonObj = new JSONObject(getJsonUrl());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String actionName = jsonObj.optString("actionName");
				String packageName = jsonObj.optString("packageName");
				String download = jsonObj.optString("download");
				Log.d("echo", "actionName"+actionName);
				Log.d("echo", "packageName"+packageName);
				Log.d("echo", "download"+download);
//				download = "http://nj.poms.baidupcs.com/file/ea9f253072954ee28228d8979573b932?bkt=p3-1400ea9f253072954ee28228d8979573b9322890a8920000006f6683&fid=488829491-250528-147971089084480&time=1461220858&sign=FDTAXGERLBH-DCb740ccc5511e5e8fedcff06b081203-3XZnsQDc%2FQzrrEoPvvIp%2BZ5BYlA%3D&to=nb&fm=Yan,B,G,ny&sta_dx=7&sta_cs=0&sta_ft=apk&sta_ct=0&fm2=Yangquan,B,G,ny&newver=1&newfm=1&secfm=1&flow_ver=3&pkey=1400ea9f253072954ee28228d8979573b9322890a8920000006f6683&sl=67633231&expires=8h&rt=pr&r=332788899&mlogid=2588592389464256963&vuk=488829491&vbdid=3954809873&fin=ceshi.apk&fn=ceshi.apk&slt=pm&uta=0&rtype=1&iv=0&isw=0&dp-logid=2588592389464256963&dp-callid=0.1.1";
//				
				if (!checkApkExist(context, packageName)) {// 如果没有安装
					if (!TextUtils.isEmpty(download)) {
						Toast.makeText(context, "正在下载，请稍候", Toast.LENGTH_SHORT).show();
						InstallOtherAppModule.getInstance().init(context);
						InstallOtherAppModule.getInstance().dowanloadApk(download);
					} else {
						// 无下载信息
						Toast.makeText(context, "无下载信息", Toast.LENGTH_SHORT).show();
					}

				} else {// 如果已经安装直接打开
					IntentMap.startIntent(context, null, getLayout(), getJsonUrl(), null);
				}

			} else {
				IntentMap.startIntent(context, null, getLayout(), getJsonUrl(), null);
			}
		}
	}

	public boolean checkApkExist(Context context, String packageName) {
		if (packageName == null || "".equals(packageName))
			return false;
		try {
			ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
			return true;
		} catch (NameNotFoundException e) {
			return false;
		}
	}

	// /**
	// *
	// * @author 海迪
	// * @Title: focusChange
	// * @Description: 添加Item焦点发送变化事件
	// * @param @param v
	// * @param @param hasFocus
	// * @return void
	// * @throws
	// *
	// * @param v
	// * @param hasFocus
	// */
	public void focusChange(View v, boolean hasFocus) {

	}

	protected void resetTitleStyle(boolean hasFocus) {

	}

	public void detect() {

	}

	public void setPageRelativeLayout(PageRelativeLayout pageRelativeLayout) {
		this.pageRelativeLayout = pageRelativeLayout;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: isStatistical
	 * @Description: 向服务端发送单击统计
	 * @param @return
	 * @return boolean
	 * @throws
	 * 
	 * @return
	 */
	protected void sendStatistical() {

	}

	protected boolean isCover() {
		boolean cover = false;
		Rect rect = new Rect();
		cover = getGlobalVisibleRect(rect);
		if (cover) {
			if (rect.width() >= getMeasuredWidth() && rect.height() >= getMeasuredHeight()) {
				return !cover;
			}
		}
		return true;
	}

	protected boolean isShow(int scrollX) {
		boolean show = false;
		float x = getX() - scrollX;
		int width = getMeasuredWidth();
		if ((x + width) > 0 && x < 1280) {
			show = true;
		}

		return show;
	}

	protected AnimatorSet animSet;

	public void bindAnimations() {
		animSet = new AnimatorSet();
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: startAnimation
	 * @Description: 执行AnimatorSet中的动画
	 * @param @param animSet
	 * @return void
	 * @throws
	 * 
	 * @param animSet
	 */
	public void startAnimation() {
		if (animSet == null) {
			bindAnimations();
		} else {
			animSet.cancel();
		}

		animSet.start();
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: reverseAnimation
	 * @Description: cancel正在执行的动画，并reverse动画
	 * @param
	 * @return void
	 * @throws
	 * 
	 */
	public void reverseAnimation() {
		if (animSet == null) {
			return;
		}

		animSet.cancel();
		for (Animator anim : animSet.getChildAnimations()) {
			ValueAnimator vAnim = (ValueAnimator) anim;
			vAnim.reverse();
		}
	}

}
