package com.wasu.cs.ui;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wasu.cs.model.ChildrenBrandAreaModel;
import com.wasu.cs.mvp.IView.IBrandAreaView;
import com.wasu.cs.mvp.presenter.ChildrenBrandAreaPresenter;
import com.wasu.cs.utils.FocusAnimUtils;
import com.wasu.cs.widget.FocusGridLayoutManager;
import com.wasu.module.log.WLog;
import com.wasu.widgets.adapter.BaseFocusRecyclerViewAdapter;
import com.wasu.widgets.adapter.BaseFocusRecyclerViewHolder;
import com.wasu.widgets.focuswidget.FocusRecyclerView;

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/14.
 *
 * @Description: 少儿专区
 * @email chenming@wasu.com
 */

public class ActivityBrandArea extends ActivityBase implements IBrandAreaView {
	private String TAG = "ActivityBrandArea";

	//ui 控件
	//private ImageView ivfavourate, ivsearch;
	private FocusRecyclerView mFocusRecyclerView;
	//adapter
	private BrandAreaAdapter mAdapter;
	//presenter
	private ChildrenBrandAreaPresenter presenter;
	//data
	private List<ChildrenBrandAreaModel.DataBean.ListBean> datalist;

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

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


	private void initUI() {
		setContentView(R.layout.activity_brand_area);
//		ivfavourate = (ImageView) findViewById(R.id.cartoon_favourate);
//		ivsearch = (ImageView) findViewById(R.id.cartoon_search);
		mFocusRecyclerView = (FocusRecyclerView) findViewById(R.id.child_brand_area_grid);
	}

	private void initAdapter() {
		mFocusRecyclerView.setHasFixedSize(true);
		FocusGridLayoutManager gridLayoutManager = new FocusGridLayoutManager(this, 5);
		gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
		mFocusRecyclerView.setLayoutManager(gridLayoutManager);
		mFocusRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
			@Override
			public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
				super.getItemOffsets(outRect, view, parent, state);
				outRect.left = getResources().getDimensionPixelSize(cn.com.wasu.main.R.dimen.d_10dp);
				outRect.right = getResources().getDimensionPixelSize(cn.com.wasu.main.R.dimen.d_10dp);
				outRect.bottom = getResources().getDimensionPixelSize(cn.com.wasu.main.R.dimen.d_10dp);
				outRect.top = getResources().getDimensionPixelSize(cn.com.wasu.main.R.dimen.d_10dp);
			}
		});
	}

	private void initListeners() {
//		View.OnClickListener listener = new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				switch (v.getId()) {
//					case R.id.cartoon_favourate:
//						IntentMap.startIntent(ActivityBrandArea.this,
//								null,
//								"Children_Fav",
//								null,
//								null);
//						break;
//					case R.id.cartoon_search:
//						IntentMap.startIntent(ActivityBrandArea.this,
//								null,
//								"Wasu_Search",
//								null,
//								null);
//						break;
//					default:
//						break;
//				}
//			}
//		};
//		ivsearch.setOnClickListener(listener);
//		ivfavourate.setOnClickListener(listener);
	}

	@Override
	public void onGetDataSuccess(ChildrenBrandAreaModel model) {
		datalist = model.getData().getList();
		mAdapter = new BrandAreaAdapter(mFocusRecyclerView, this);
		mAdapter.setData(datalist);
		mFocusRecyclerView.setAdapter(mAdapter);
		mFocusRecyclerView.scheduleLayoutAnimation();
		mFocusRecyclerView.postDelayed(new Runnable() {
			@Override
			public void run() {
				View view =mFocusRecyclerView.getLayoutManager().findViewByPosition(0);
				if (view != null) {
					view.requestFocus();
				}
			}
		}, 300);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		recycleMemory();
	}

	private void recycleMemory() {
		if (mAdapter != null) {
			mAdapter = null;
		}
		if (presenter != null) {
			presenter = null;
		}
		if (datalist != null) {
			datalist = null;
		}
		System.gc();
	}

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

	private class BrandAreaAdapter extends BaseFocusRecyclerViewAdapter<ChildrenBrandAreaModel.DataBean.ListBean> {
		Context mContext;

		public BrandAreaAdapter(FocusRecyclerView recyclerView, Context context) {
			super(recyclerView);
			mContext = context;
		}

		@Override
		protected BaseFocusRecyclerViewHolder createItem(ViewGroup parent, int viewType) {
			View view = LayoutInflater.from(mContext).inflate(R.layout.item_child_brand_area_grid, null);
			return new BrandAreaViewHolder(view, getItemParent(), getItemListener());
		}

		@Override
		protected void bindData(final BaseFocusRecyclerViewHolder holder, final int position) {
			holder.itemView.setFocusable(true);

			((BrandAreaViewHolder) holder).mTextView.setText(getItemData(position).getTitle());
//			ImageFetcherModule.getInstance().attachImage(getItemData(position).getPicUrl(), ((BrandAreaViewHolder) holder).mImageView, corner);
			//fresco方案
			((BrandAreaViewHolder) holder).mImageView.setImageURI(getItemData(position).getPicUrl());
			((BrandAreaViewHolder) holder).itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					FocusAnimUtils.animItem(v, hasFocus, 1.15f);
				}
			});
			((BrandAreaViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					IntentMap.startIntent(ActivityBrandArea.this,
							null,
							datalist.get(position).getLayout(),
							datalist.get(position).getJsonUrl(),
							null);
				}
			});
		}

		class BrandAreaViewHolder extends BaseFocusRecyclerViewHolder {
			/**
			 * 创建FocusRecyclerView的itemView
			 *
			 * @param itemView          item View
			 * @param focusRecyclerView item parent ViewGroup(Parent must instanceof FocusRecyclerView)
			 * @param mOnItemListener   item click、longClick、focusChange listener
			 */
			View view;
			SimpleDraweeView mImageView;
			TextView mTextView;

			public BrandAreaViewHolder(View itemView, FocusRecyclerView focusRecyclerView, OnItemListener mOnItemListener) {
				super(itemView, focusRecyclerView, mOnItemListener);
				this.view = itemView;
				mImageView = (SimpleDraweeView) itemView.findViewById(R.id.iv_brandarea);
				mTextView = (TextView) itemView.findViewById(R.id.tv_brandarea_name);
			}

			public void showText() {
				mTextView.setVisibility(View.VISIBLE);
			}

			public void hideText() {
				mTextView.setVisibility(View.INVISIBLE);
			}

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