package com.gh.gamecenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Response;
import com.gh.base.AppController;
import com.gh.common.constant.Config;
import com.gh.common.constant.Constants;
import com.gh.common.util.DataUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.TimestampUtils;
import com.gh.gamecenter.ConcernActivity;
import com.gh.gamecenter.GameDetailActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.ConcernViewHolder;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.listener.OnCallBackListener;
import com.gh.gamecenter.manager.ConcernManager;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LGT on 2016/9/20.
 */
public class ConcernRecommendAdapter extends RecyclerView.Adapter<ConcernViewHolder> {

	private Context context;
	private OnCallBackListener listener;

	private List<GameEntity> gameList;
	private List<GameEntity> recommendGameList;

	private ConcernManager concernManager;

	public ConcernRecommendAdapter(ConcernActivity activity) {
		this.context = activity;
		this.listener = activity;

		concernManager = new ConcernManager(context);

		initRecommendGame();
	}

	private void initRecommendGame() {
		JsonArrayExtendedRequest recommendRequest = new JsonArrayExtendedRequest(
				TimestampUtils.addTimestamp(Config.HOST + "v1d45/game/remenkapai",
						Constants.GAME_CD),
				new Response.Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray response) {
						Type listType = new TypeToken<ArrayList<GameEntity>>() {}.getType();
						Gson gson = new Gson();
						gameList = gson.fromJson(response.toString(), listType);
						if (gameList != null && gameList.size() != 0) {
							recommendGameList = new ArrayList<>();
							for (int i = 0, size = gameList.size(); i < size; i++) {
								if (!concernManager.isConcern(gameList.get(i).getId())) {
									recommendGameList.add(gameList.get(i));
								}
							}
						}
						if (recommendGameList.size() != 0) {
							listener.loadDone();
						}
					}
				}, null);
		AppController.addToRequestQueue(recommendRequest, ConcernActivity.TAG);
	}

	@Override
	public ConcernViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.concern_item, parent, false);
		return new ConcernViewHolder(view);
	}

	@Override
	public void onBindViewHolder(final ConcernViewHolder holder, int position) {
		GameEntity gameEntity = recommendGameList.get(position);
		ImageUtils.getInstance(context).display(
				gameEntity.getIcon(), holder.concern_item_icon);
		holder.concern_item_name.setText(gameEntity.getName());
		holder.concern_item_concern.setText("关注");
		holder.concern_item_concern.setBackgroundResource(R.drawable.textview_blue_style);
		holder.concern_item_concern.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 添加关注到表，并更新当前界面，插入一个关注
				GameEntity gameEntity = recommendGameList.remove(holder.getPosition());

				Map<String, Object> kv = new HashMap<>();
				kv.put("状态", "关注");
				DataUtils.onEvent(context, "游戏关注", gameEntity.getName(), kv);

				Map<String, Object> map = new HashMap<>();
				map.put("game", gameEntity.getName());
				map.put("game_id", gameEntity.getId());
				map.put("type", "关注");
				DataCollectionManager.onEvent(context, "concern", map);

				concernManager.addByEntity(gameEntity);

				holder.concern_item_concern.setClickable(false);
				holder.itemView.setClickable(false);
//				notifyItemRemoved(holder.getPosition());
				notifyDataSetChanged();

				Toast.makeText(context, "关注成功", Toast.LENGTH_SHORT).show();

				listener.loadEmpty();
			}
		});
		holder.itemView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				GameEntity gameEntity = recommendGameList.get(holder.getPosition());

				Map<String, Object> kv = new HashMap<>();
				kv.put("名字", gameEntity.getName());
				kv.put("位置", String.valueOf(holder.getPosition() + 1));
				DataUtils.onEvent(context, "点击", "我的关注-推荐", kv);

				Map<String, Object> map = new HashMap<>();
				map.put("location", "推荐");
				map.put("page", "我的关注");
				map.put("game", gameEntity.getName());
				map.put("game_id", gameEntity.getId());
				DataCollectionManager.onEvent(context, "click-item", map);

				AppController.put("GameEntity", gameEntity);
				Intent intent = new Intent(context, GameDetailActivity.class);
				intent.putExtra("entrance", "(我的关注-推荐)");
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});
	}

	@Override
	public int getItemCount() {
		if (recommendGameList == null) {
			return 0;
		}
		return recommendGameList.size();
	}

	public List<GameEntity> getRecommendGameList() {
		if (recommendGameList == null) {
			recommendGameList = new ArrayList<>();
		}
		return recommendGameList;
	}

	public List<GameEntity> getGameList() {
		return gameList;
	}
}
