package com.wasu.cs.adapter;

import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.wasu.cs.model.DemandList;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;

import java.util.List;

import cn.com.wasu.main.R;

/**
 * Created by Danxx on 2016/6/8.
 */
public class RowRecyclerViewAdapter extends BaseRecyclerViewAdapter {
	private LayoutInflater inflater = null;
	private TranslateAnimation shakeAnim;

	private RowRecyclerViewAdapter() {
	}

	public RowRecyclerViewAdapter(Context context, List<DemandList.Asset> mData) {
		inflater = LayoutInflater.from(context);
		setData(mData);
		initAnimation();
	}

	private void initAnimation() {
		shakeAnim = new TranslateAnimation(4, -4, 0, 0);
		shakeAnim.setInterpolator(new OvershootInterpolator());
		shakeAnim.setDuration(100);
		shakeAnim.setRepeatCount(3);
		shakeAnim.setRepeatMode(Animation.REVERSE);
	}

	/**
	 * 创建item view
	 *
	 * @param parent
	 * @param viewType
	 * @return
	 */
	@Override
	protected BaseRecyclerViewHolder createItem(ViewGroup parent, int viewType) {
		View view = inflater.inflate(R.layout.item_row_item, null);
		MyViewHolder viewHolder = new MyViewHolder(view);
		return viewHolder;
	}

	@Override
	public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
		super.onBindViewHolder(holder, position);
		holder.itemView.setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (position == 0 && keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
					holder.itemView.startAnimation(shakeAnim);
					return true;
				} else if (position == getItemCount() - 1 && keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
					holder.itemView.startAnimation(shakeAnim);
					return true;
				}
				return false;
			}
		});
	}

	/**
	 * 绑定数据
	 *
	 * @param holder
	 * @param position
	 */
	@Override
	protected void bindData(BaseRecyclerViewHolder holder, int position) {
		DemandList.Asset itemData = (DemandList.Asset) getItemData(position);
		((MyViewHolder) holder).tvName.setText(itemData.getTitle());
		FrescoImageFetcherModule.getInstance().attachImage(itemData.getPicUrl(), ((MyViewHolder) holder).ivPic);
	}

	class MyViewHolder extends BaseRecyclerViewHolder {
		TextView tvName;
		ImageView ivPic;

		public MyViewHolder(View itemView) {
			super(itemView);
			tvName = (TextView) itemView.findViewById(R.id.tvName);
			ivPic = (ImageView) itemView.findViewById(R.id.ivPic);
		}

		@Override
		protected View getView() {
			return null;
		}
	}
}
