package com.wasu.cs.widget;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils.TruncateAt;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wasu.cs.model.FilmSpecialModel;
import com.wasu.cs.ui.ActivityPlayer;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;

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

import cn.com.wasu.main.IntentConstant;
import cn.com.wasu.main.IntentMap;
import cn.com.wasu.main.R;

/**
 * 
 * @ClassName: ShortVideoSpecialItem
 * @Description: 短视频专题模板Item
 * @author 海迪
 * @date 2015年7月25日 下午5:12:17
 * 
 */
public class ShortVideoSpecialItem extends LinearLayout {

	private ImageView firstImgView;
	private AlwaysMarqueeTextView titleView;
	/* private TextView summaryView; */
	private int position;

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	private Context context;

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

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 0) {
				modifyTitleStyle(false);
			} else {
				modifyTitleStyle(true);
			}
		};
	};

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

		firstImgView = (ImageView) findViewById(R.id.cont_first_img);
		titleView = (AlwaysMarqueeTextView) findViewById(R.id.title);
		/* summaryView = (TextView) findViewById(R.id.summary); */
		setFocusable(true);
		setClickable(true);
		setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					// startAnimation();

					specialListView.setFocusedItemIndex((ShortVideoSpecialItem) v, index);
					handler.sendEmptyMessageDelayed(1, 200);
				} else {
					// reverseAnimation();

					specialListView.setFocusedItemIndex(null, index);
					handler.sendEmptyMessageDelayed(0, 200);
				}

				// modifyTitleStyle(hasFocus);
			}

		});

		/**
		 * 添加单击处理
		 */
		setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// IntentMap.startIntent(context, null, model.getLayout(),
				// model.getJsonUrl(), null);

				try {
					JSONObject json = new JSONObject((String) v.getTag());

					Intent intent = new Intent();
					intent.putExtra(IntentConstant.PLAY_TYPE.value(), ActivityPlayer.PLAY_TYPE_LOOP);
					intent.putExtra(IntentConstant.PLAY_INDEX.value(), json.getInt("playIndex"));
					intent.putExtra(IntentConstant.DATAURI.value(), json.getString("jsonUrl"));

					IntentMap.startIntent(getContext(), intent, null, null, ActivityPlayer.class);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});

	}

	/**
	 * 
	 * @author 海迪
	 * @Title: modifyTitleStyle
	 * @Description: 选中时，标题滚动
	 * @param @param hasFocus
	 * @return void
	 * @throws
	 * 
	 * @param hasFocus
	 */
	public void modifyTitleStyle(boolean hasFocus) {
		if (hasFocus) {
			titleView.setEllipsize(TruncateAt.MARQUEE);
		} else {
			titleView.setEllipsize(TruncateAt.END);
		}
		this.postInvalidate();
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: initData
	 * @Description: 初始化Item数据
	 * @param @param model
	 * @return void
	 * @throws
	 * 
	 * @param model
	 */
	public void initData(FilmSpecialModel model) {
		if (model == null)
			return;

		if (firstImgView != null) {
			FrescoImageFetcherModule.getInstance().attachImage(model.getPicUrl(), firstImgView, getResources().getDimensionPixelSize(R.dimen.d_8dp));
		}

		if (titleView != null) {
			titleView.setText(model.getTitle());
		}

		/*
		 * if (summaryView != null) { summaryView.setText(model.getSummary()); }
		 */

	}

	private SpecialListView specialListView;

	public void setSpecialListView(SpecialListView specialListView) {
		this.specialListView = specialListView;
	}

	public SpecialListView getSpecialListView() {
		return specialListView;
	}

	private int index = -1;

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

	public int getIndex() {
		return index;
	}

}
