package com.wasu.cs.ui;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.wasu.cs.model.FourKData;
import com.wasu.cs.model.FourKData.DataEntity.AssetsEntity;
import com.wasu.cs.ui.ActivityFourKSpecial.hRecyclerViewAdapter.ViewHolder;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;
import com.wasu.module.datafetch.DataFetchListener.JsonListener;
import com.wasu.module.datafetch.DataFetchModule;
import com.wasu.module.log.WLog;

import org.json.JSONObject;

import java.util.List;

import cn.com.wasu.main.ErrorMap;
import cn.com.wasu.main.IntentConstant;
import cn.com.wasu.main.IntentMap;
import cn.com.wasu.main.R;
/**
 * 4k专区
 * @author Created by Danxingxi on 2015/11/20 
 *
 */
public class ActivityFourKSpecial extends ActivityBase{
	private String TAG = "ActivityFourKSpecial";

	private RecyclerView hRecyclerView;
	private SimpleDraweeView ivBg;
	private String JSON_URL = "http://121.40.195.74/?s=2002&p=sntCat&k=1&v=1&catId=221555";
	private FourKData fourKData = new FourKData();
	private hRecyclerViewAdapter recyclerViewAdapter;
	
	@Override
	protected void doCreate(Bundle savedInstanceState) {
		WLog.i(TAG,"doCreate()");

		setContentView(R.layout.activity_four_k);
		initView();
		initData();
	}
	
	private void initView(){
		hRecyclerView = (RecyclerView) findViewById(R.id.hRecyclerView);
		ivBg = (SimpleDraweeView) findViewById(R.id.four_k_bg);
		//设置布局管理器  
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);  
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);  
        hRecyclerView.setLayoutManager(linearLayoutManager);  
	}
	
	private void initData(){
		JSON_URL = getIntent().getStringExtra(IntentConstant.DATAURI.value());
		if (TextUtils.isEmpty(JSON_URL)) {
			showErrorExitDlg("没有数据源!");
			return;
		}
		showLoading();
		DataFetchModule.getInstance().fetchJsonGet(JSON_URL, new JsonListener() {
			
			@Override
			public void onJsonGet(int retcode, String extraMsg,JSONObject jsondata) {
				if (retcode != 0) {
					showErrorExitDlg(ErrorMap.mapError(	ActivityFourKSpecial.this, retcode, extraMsg));
					return;
				}
				
				Gson gson = new Gson();
				fourKData = gson.fromJson(jsondata.toString(), FourKData.class);
				if(fourKData.getData().getAssets().size()<1){
					showErrorExitDlg("抱歉，暂无资产！");
					return;
				}else{
					FrescoImageFetcherModule.getInstance().attachImage(fourKData.getData().getBgImage(), ivBg);
					recyclerViewAdapter = new hRecyclerViewAdapter(ActivityFourKSpecial.this, fourKData.getData().getAssets());
					hRecyclerView.setAdapter(recyclerViewAdapter);
					hideLoading();
				}
			}
		});
	}
	
	class hRecyclerViewAdapter extends RecyclerView.Adapter<hRecyclerViewAdapter.ViewHolder>{

		private LayoutInflater mInflater;  
		private List<AssetsEntity> mData;
		
		public hRecyclerViewAdapter(Context context,
				List<AssetsEntity> mData) {
			super();
			this.mInflater = LayoutInflater.from(context);
			this.mData = mData;
		}

		@Override
		public int getItemCount() {
			return mData.size();
		}
		/**
		 * 绑定数据到ViewHolder上
		 */
		@Override
		public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
			View view = mInflater.inflate(R.layout.item_fourk_special, null);
			ViewHolder viewHolder = new ViewHolder(view);
			
			viewHolder.mImg = (SimpleDraweeView) view.findViewById(R.id.ivItem);
			viewHolder.mTxt = (TextView) view.findViewById(R.id.tvItem);
			return viewHolder;
		}
		/**
		 * 设置值
		 */
		public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
			FrescoImageFetcherModule.getInstance().attachImageAlpha(mData.get(i).getPicUrl(), viewHolder.mImg,getResources().getDimensionPixelSize(R.dimen.d_5dp),300);
			/**描述文字等待数据下发**/
			if(!TextUtils.isEmpty(mData.get(i).getTitle())){
				viewHolder.mTxt.setText(mData.get(i).getTitle());
			}else{
				viewHolder.hideDesc();
			}
			View view = viewHolder.getView();
			/**
			 * 获得焦点的时候放大1.05倍
			 */
			view.setOnFocusChangeListener(new OnFocusChangeListener() {
				
				@Override
				public void onFocusChange(View view, boolean hasFocus) {
//					if(hasFocus){
//						view.setScaleX(1.05F );
//						view.setScaleY(1.05F);
//						viewHolder.showDesc();
//					}else{
//						view.setScaleX(1.0F);
//						view.setScaleY(1.0F);
//						viewHolder.hideDesc();
//					}
					animItem(view, hasFocus ,viewHolder);
				}
			});
			view.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View view) {
					String josnUrl = mData.get(i).getJsonUrl();
					String layoutCode = mData.get(i).getLayout();
					IntentMap.startIntent(ActivityFourKSpecial.this,null,layoutCode,josnUrl,null);
				}
			});
			if(0==i){
				view.requestFocus();
			}
		}
		
		class ViewHolder extends RecyclerView.ViewHolder  {
			View view;
			SimpleDraweeView mImg;
	        TextView mTxt; 
			public ViewHolder(View view) {
				super(view);
				this.view = view;
				this.view.setFocusable(true);
				this.view.setClickable(true);
			}
			
			public View getView() {
				return view;
			}
			
			public void showDesc() {
				mTxt.setVisibility(View.VISIBLE);
			}
			public void hideDesc(){
				mTxt.setVisibility(View.GONE);
			}
		}
	}
	
	/**
	 * 4K专区Item添加动画
	 * @param view
	 * @param scale
	 */
    private void animItem(View view,boolean scale ,ViewHolder viewHolder) {
        float toValue = 1.0f;
        if(scale) {  //得到焦点
            toValue = 1.05f;
            ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", toValue);
            ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", toValue);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.setDuration(300);
            animatorSet.playTogether(animatorX, animatorY);
            animatorSet.start();
            viewHolder.showDesc();
        }else{   //失去焦点
        	ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", toValue);
            ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", toValue);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.setDuration(300);
            animatorSet.playTogether(animatorX, animatorY);
            animatorSet.start();
            viewHolder.hideDesc();
        }
    }
}
