package com.wasu.cs.ui;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wasu.cs.model.CartoonStarModel;
import com.wasu.cs.mvp.IView.ICartoonStarView;
import com.wasu.cs.mvp.presenter.CartoonStarPresenter;
import com.wasu.module.log.WLog;

import java.util.List;

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

/**
 * Created by chenming on 2016/11/11.
 *
 * @Description: 动画明星页
 * @email chenming@wasu.com
 */

public class ActivityCartoonStar extends ActivityBase implements ICartoonStarView {
	private String TAG = "ActivityCartoonStar";

	//UI控件
	private RecyclerView rvcartoonlist;
	private LinearLayoutManager mLinearLayoutManager;
	//动效
	//presenter
	CartoonStarPresenter presenter;
	//数据
	private CartoonStarAdapter adapter;
	private CartoonStarModel model;

	@Override
	protected void doCreate(Bundle savedInstanceState) {
		WLog.i(TAG,"onCreate()");

		presenter = new CartoonStarPresenter();
		presenter.attachView(this);
		presenter.getCartoonStarData(getIntent().getStringExtra(IntentConstant.DATAURI.value()));
		initUI();
		initAdapter();
		initListeners();
	}

	private void initUI() {
		setContentView(R.layout.activity_cartoon_stars);
		rvcartoonlist = (RecyclerView) findViewById(R.id.rv_cartoon_star);
	}

	private void initAdapter() {
		rvcartoonlist.setHasFixedSize(true);
		rvcartoonlist.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
		final int spaceInPixels = getResources().getDimensionPixelOffset(R.dimen.d_9dp);
		rvcartoonlist.addItemDecoration(new RecyclerView.ItemDecoration() {
			@Override
			public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
				super.getItemOffsets(outRect, view, parent, state);
				int count = parent.getChildCount();
				int pos = parent.getChildPosition(view);
				if (pos == 0) {
					outRect.top = 0;
				} else if (pos == count - 1) {
					outRect.top = spaceInPixels;
					outRect.bottom = 0;
				} else {
					outRect.top = spaceInPixels;
				}
			}
		});
		mLinearLayoutManager = new LinearLayoutManager(this);
		mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		rvcartoonlist.setLayoutManager(mLinearLayoutManager);

	}

	private void initListeners() {

	}


	@Override
	public void onGetDataSucceed(CartoonStarModel cartoonData) {
		model = cartoonData;
		setBg(model.getData().getBgPicUrl());
		adapter = new CartoonStarAdapter(this, model.getData().getList());
		rvcartoonlist.setAdapter(adapter);
		rvcartoonlist.postDelayed(new Runnable() {
			@Override
			public void run() {
				View view = rvcartoonlist.getLayoutManager().findViewByPosition(0);
				if (view != null) {
					view.requestFocus();
				}
			}
		}, 300);
	}

	@Override
	public void setBg(String url) {
		super.setBg(url);
	}

	@Override
	public void onGetDataFailed(Throwable throwable) {
		Toast.makeText(ActivityCartoonStar.this, "服务器开小差了，请稍后重试哦", Toast.LENGTH_SHORT).show();
	}

	class CartoonStarAdapter extends RecyclerView.Adapter<CartoonStarAdapter.CartoonStarViewHolder> {
		private Context mContext;
		private TranslateAnimation shakeAnim;
		private int focusindex;
		private List<CartoonStarModel.DataBean.Asset> datalist;
		//final int roundconner = getResources().getDimensionPixelOffset(R.dimen.d_10dp);


		public CartoonStarAdapter(Context context, List<CartoonStarModel.DataBean.Asset> datasp) {
			mContext = context;
			datalist = datasp;
			shakeAnim = new TranslateAnimation(0, 0, 4, -4);
			shakeAnim.setInterpolator(new OvershootInterpolator());
			shakeAnim.setDuration(100);
			shakeAnim.setRepeatCount(3);
			shakeAnim.setRepeatMode(Animation.REVERSE);
		}

		public int getFocusindex() {
			return focusindex;
		}

		public void setFocusindex(int focusindex) {
			this.focusindex = focusindex;
		}

		@Override
		public CartoonStarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View view = LayoutInflater.from(mContext).inflate(R.layout.item_cartoon_star_list, null);
			return new CartoonStarViewHolder(view);
		}

		@Override
		public void onBindViewHolder(final CartoonStarViewHolder holder, final int position) {
			holder.itemView.setFocusable(true);
			holder.tvname.setText(datalist.get(position).getTitle());
			holder.ivcartoonset.setImageURI(datalist.get(position).getPicUrl());
			//ImageFetcherModule.getInstance().attachImage(datalist.get(position).getPicUrl(), holder.ivcartoonset, roundconner);
			holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if (hasFocus) {
						setFocusindex(position);
					}
				}
			});
			holder.itemView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String layout = datalist.get(position).getLayout();
					if (layout.contains("Detail_Series") || layout.contains("Detail_Movie")) {
						IntentMap.startIntent(ActivityCartoonStar.this,
								null,
								"Children_Detail",
								datalist.get(position).getJsonUrl(),
								null);
					}else {
						IntentMap.startIntent(ActivityCartoonStar.this,
								null,
								datalist.get(position).getLayout(),
								datalist.get(position).getJsonUrl(),
								null);
					}


				}
			});
			holder.itemView.setOnKeyListener(new View.OnKeyListener() {
				@Override
				public boolean onKey(View v, int keyCode, KeyEvent event) {
					if (position == 0 && keyCode == KeyEvent.KEYCODE_DPAD_UP && event.getAction() == KeyEvent.ACTION_DOWN) {
						holder.itemView.startAnimation(shakeAnim);
						return true;
					} else if (position == getItemCount() - 1 && keyCode == KeyEvent.KEYCODE_DPAD_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) {
						holder.itemView.startAnimation(shakeAnim);
						return true;
					}
					return false;
				}
			});
		}

		@Override
		public int getItemCount() {
			return datalist == null ? 0 : datalist.size();
		}

		class CartoonStarViewHolder extends RecyclerView.ViewHolder {
			TextView tvname;
			SimpleDraweeView ivcartoonset;

			public CartoonStarViewHolder(View itemView) {
				super(itemView);
				tvname = (TextView) itemView.findViewById(R.id.tv_cartoon_star_set_name);
				ivcartoonset = (SimpleDraweeView) itemView.findViewById(R.id.iv_cartoon_set);
			}
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		recycleMemoroy();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		recycleMemoroy();
		System.gc();
	}

	private void recycleMemoroy() {
		if (mLinearLayoutManager != null) {
			mLinearLayoutManager = null;
		}
		if (presenter != null) {
			presenter = null;
		}
		if (adapter != null) {
			adapter = null;
		}
		if (model != null) {
			model = null;
		}
	}
}
