package com.gh.gamecenter.gamedetail;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.gh.base.AppController;
import com.gh.common.constant.Config;
import com.gh.common.util.ConcernUtils;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.NewsUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.util.Utils;
import com.gh.common.view.HorizontalItemDecoration;
import com.gh.gamecenter.GameDetailActivity;
import com.gh.gamecenter.GameNewsActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameDetailEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.NewsEntity;
import com.gh.gamecenter.entity.ServerEntity;
import com.gh.gamecenter.entity.TagEntity;
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.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Created by LGT on 2016/9/8.
 */
public class GameDetailAdapter extends RecyclerView.Adapter {

	private Context context;
	private OnCallBackListener listener;

	private ConcernManager concernManager;

	private GameEntity gameEntity;
	private GameDetailEntity gameDetailEntity;

	private String gameId;
	private String entrance;

	private int position_top = -1;
	private int position_newsserver = -1;
	private int position_plugin = -1;
	private int position_news = -1;
	private int position_intro = -1;

	public GameDetailAdapter(GameDetailActivity activity,
	                         String gameId,
	                         String entrance) {
		this.context = activity;
		this.listener = activity;

		this.gameId = gameId;
		this.entrance = entrance;

		concernManager = new ConcernManager(context);
	}

	public void setGameEntity(GameEntity gameEntity) {
		this.gameEntity = gameEntity;
	}

	public GameDetailEntity getGameDetailEntity() {
		return gameDetailEntity;
	}

	// 获取游戏详情
	public void getGameDetail() {
		JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
				Config.HOST + "v2d0/game/" + gameId + "/detail",
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						Gson gson = new Gson();
						gameDetailEntity = gson.fromJson(response.toString(), GameDetailEntity.class);

						getGameNews();

						getNewsServer();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						if (listener != null) {
							listener.loadError();
						}
					}
				});
		AppController.addToRequestQueue(request, GameDetailActivity.TAG);
	}

	// 获取游戏新闻
	private void getGameNews() {
		String url = Config.HOST + "v1d45/game/" + gameId + "/news?limit=3";
		JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(url,
				new Response.Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray response) {
						Gson gson = new Gson();
						ArrayList<NewsEntity> news = gson.fromJson(response.toString(),
								new TypeToken<ArrayList<NewsEntity>>() {}.getType());
						gameDetailEntity.setNews(news);

						if (listener != null) {
							listener.loadDone();
						}
						initPosition();
						notifyDataSetChanged();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						if (listener != null) {
							listener.loadDone();
						}
						initPosition();
						notifyDataSetChanged();
					}
				});
		AppController.addToRequestQueue(request, GameDetailActivity.TAG);
	}

	// 获取开发信息
	private void getNewsServer() {
		JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(
				Config.HOST + "v2/game/" + gameId + "/serverInfo",
				new Response.Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray response) {
						if (response.length() != 0) {
							try {
								ArrayList<ServerEntity> serverInfo = new ArrayList<>();
								SimpleDateFormat format = new SimpleDateFormat(
										"Mdd", Locale.getDefault());
								int today = Integer.valueOf(format.format(new Date()));
								for (int j = 0, sizej = response.length(); j < sizej; j++) {
									ServerEntity entity = new ServerEntity();
									JSONObject jsonObject2;
									jsonObject2 = response.getJSONObject(j);
									String server = jsonObject2.getString("server");
									if (server.length() > 4) {
										server = server.substring(0, 4);
									}
									entity.setServer(server);
									entity.setTime(Long.valueOf(jsonObject2.getString("time") + "000"));
									int day = Integer.valueOf(format.format(new Date(entity.getTime())));
									if (day == today + 1) {
										entity.setTag("明天");
										serverInfo.add(entity);
									} else if (day == today - 1) {
										entity.setTag("昨天");
										serverInfo.add(entity);
									} else if (day == today) {
										entity.setTag("今天");
										serverInfo.add(entity);
									}
								}

								Comparator<ServerEntity> comparator = new Comparator<ServerEntity>() {
									@Override
									public int compare(ServerEntity lhs, ServerEntity rhs) {
										return (int) (lhs.getTime() - rhs.getTime());
									}
								};
								Collections.sort(serverInfo, comparator);

								gameDetailEntity.setServerInfo(serverInfo);
								initPosition();
								if (position_newsserver != -1) {
									notifyItemInserted(position_newsserver);
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}
				}, null);
		AppController.addToRequestQueue(request, GameDetailActivity.TAG);
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		if (viewType == 1) {
			View view = LayoutInflater.from(parent.getContext())
					.inflate(R.layout.gamedetail_item_top, parent, false);
			return new GameDetailTopViewHolder(view);
		} else if (viewType == 2) {
			View view = LayoutInflater.from(parent.getContext())
					.inflate(R.layout.gamedetail_item_newsserver,parent,false);
			return new GameDetailNewsServerViewHolder(view);
		} else if (viewType == 3) {
			View view = LayoutInflater.from(parent.getContext())
					.inflate(R.layout.gamedetail_item_plugin, parent, false);
			return new GameDetailPluginViewHolder(view);
		} else if (viewType == 4) {
			View view = LayoutInflater.from(parent.getContext())
					.inflate(R.layout.gamedetail_item_news, parent, false);
			return new GameDetailNewsViewHolder(view);
		} else if (viewType == 5) {
			View view = LayoutInflater.from(parent.getContext())
					.inflate(R.layout.gamedetail_item_intro,parent,false);
			return new GameDetailIntroViewHolder(view);
		}
		return null;
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
		if (holder instanceof GameDetailTopViewHolder){
			initTopViewHolder((GameDetailTopViewHolder) holder);
		} else if (holder instanceof GameDetailPluginViewHolder){
			initPluginViewHolder((GameDetailPluginViewHolder) holder);
		} else if (holder instanceof GameDetailNewsViewHolder){
			initNewsViewHolder((GameDetailNewsViewHolder) holder);
		} else if (holder instanceof GameDetailIntroViewHolder){
			initIntroViewHolder((GameDetailIntroViewHolder) holder);
		} else if (holder instanceof GameDetailNewsServerViewHolder) {
			initNewsServerViewHolder((GameDetailNewsServerViewHolder) holder);
		}
	}

	private void initNewsServerViewHolder(final GameDetailNewsServerViewHolder viewHolder) {
		ArrayList<ServerEntity> serverInfo = gameDetailEntity.getServerInfo();
		viewHolder.gamedetail_newsserver_show.setLayoutManager(new LinearLayoutManager(
				context, LinearLayoutManager.HORIZONTAL, false));
		viewHolder.gamedetail_newsserver_show.setAdapter(
				new GameDetailNewsServerAdapter(context, serverInfo));
		viewHolder.gamedetail_newsserver_show.setOnScrollListener(new RecyclerView.OnScrollListener() {
			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				super.onScrolled(recyclerView, dx, dy);
				LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
				if (layoutManager.findFirstVisibleItemPosition() == 0
						&& layoutManager.findViewByPosition(0).getLeft() == 0) {
					viewHolder.gamedetail_newsserver_left.setVisibility(View.INVISIBLE);
					viewHolder.gamedetail_newsserver_right.setVisibility(View.VISIBLE);
				} else if (layoutManager.findLastVisibleItemPosition() == recyclerView.getAdapter().getItemCount() - 1
						&& layoutManager.findViewByPosition(recyclerView.getAdapter().getItemCount() - 1).getRight() == recyclerView.getWidth()) {
					viewHolder.gamedetail_newsserver_left.setVisibility(View.VISIBLE);
					viewHolder.gamedetail_newsserver_right.setVisibility(View.INVISIBLE);
				} else {
					viewHolder.gamedetail_newsserver_left.setVisibility(View.VISIBLE);
					viewHolder.gamedetail_newsserver_right.setVisibility(View.VISIBLE);
				}
			}
		});
		if (serverInfo.size() <= 3) {
			viewHolder.gamedetail_newsserver_left.setVisibility(View.GONE);
			viewHolder.gamedetail_newsserver_right.setVisibility(View.GONE);
		} else {
			int todayPosition = -1;
			int tomorrowPosition = -1;
			for (int j = 0, sizej = serverInfo.size(); j < sizej; j++) {
				if ("今天".equals(serverInfo.get(j).getTag())
						&& todayPosition == -1) {
					todayPosition = j;
				} else if ("明天".equals(serverInfo.get(j).getTag())
						&& tomorrowPosition == -1) {
					tomorrowPosition = j;
				}
			}
			if (!(todayPosition == 0 || todayPosition == 1 || todayPosition == -1)) {
				if (serverInfo.size() >= todayPosition + 2) {
					viewHolder.gamedetail_newsserver_show.scrollToPosition(todayPosition - 1);
				} else {
					viewHolder.gamedetail_newsserver_show.scrollToPosition(todayPosition);
				}
			} else if (todayPosition == -1
					&& !(tomorrowPosition == 0 || tomorrowPosition == 1 || tomorrowPosition == -1)) {
				if (serverInfo.size() >= todayPosition + 2) {
					viewHolder.gamedetail_newsserver_show.scrollToPosition(tomorrowPosition - 1);
				} else {
					viewHolder.gamedetail_newsserver_show.scrollToPosition(tomorrowPosition);
				}
			} else if (todayPosition == -1 && tomorrowPosition == -1) {
				viewHolder.gamedetail_newsserver_show.scrollToPosition(serverInfo.size() - 1);
				viewHolder.gamedetail_newsserver_show.setVisibility(View.GONE);
			} else {
				viewHolder.gamedetail_newsserver_left.setVisibility(View.GONE);
			}
			if (todayPosition == serverInfo.size() - 1
					|| todayPosition == serverInfo.size() - 2) {
				viewHolder.gamedetail_newsserver_right.setVisibility(View.GONE);
			}
			if (todayPosition == -1
					&& (tomorrowPosition == serverInfo.size() - 1
					|| tomorrowPosition == serverInfo.size() - 2)) {
				viewHolder.gamedetail_newsserver_right.setVisibility(View.GONE);
			}
		}
	}

	private void initIntroViewHolder(GameDetailIntroViewHolder viewHolder) {
		if (viewHolder.gamedetail_item_intro_gallery.getAdapter() == null ) {
			viewHolder.gamedetail_item_intro_gallery.setHasFixedSize(true);
			viewHolder.gamedetail_item_intro_gallery.setLayoutManager(
					new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
			viewHolder.gamedetail_item_intro_gallery.setAdapter(new GameGalleryAdapter(context, gameDetailEntity.getGallery()));
			viewHolder.gamedetail_item_intro_gallery.addItemDecoration(
					new HorizontalItemDecoration(context, 1, gameDetailEntity.getGallery().size()));
		}
		if (TextUtils.isEmpty(gameDetailEntity.getDes())) {
			viewHolder.gamedetail_item_intro_content.setText("");
		} else {
			viewHolder.gamedetail_item_intro_content.setText(gameDetailEntity.getDes());
		}
	}

	private void initNewsViewHolder(GameDetailNewsViewHolder viewHolder) {
		if (gameDetailEntity.getNews().size() < 3) {
			viewHolder.gamedetail_item_news_more.setVisibility(View.GONE);
		} else {
			viewHolder.gamedetail_item_news_more.setVisibility(View.VISIBLE);
			viewHolder.gamedetail_item_news_more.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (gameDetailEntity.getArticleTypes() != null
							&& gameEntity.getId() != null
							&& gameEntity.getName() != null) {
						Map<String, Object> kv = new HashMap<>();
						kv.put("名字", gameEntity.getName());
						kv.put("位置", "资讯攻略-全部");
						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);

						Intent intent = new Intent(context, GameNewsActivity.class);
						intent.putExtra("articleTypes", gameDetailEntity.getArticleTypes());
						intent.putExtra("gameName", gameEntity.getName());
						intent.putExtra("gameId", gameId);
						intent.putExtra("entrance", entrance + "+(游戏详情:资讯攻略)");
						context.startActivity(intent);
					}
				}
			});
		}
		final ArrayList<NewsEntity> newsList = gameDetailEntity.getNews();
		int childCount = viewHolder.gamedetail_item_news_list.getChildCount();
		if (childCount == 0) {
			for (int i = 0, size = newsList.size(); i < size; i++) {
				View view = View.inflate(context, R.layout.gamedetail_news_item, null);

				TextView tv_tag = (TextView) view.findViewById(R.id.gamedetail_news_item_tag);
				if ("活动".equals(newsList.get(i).getType())){
					tv_tag.setBackgroundResource(R.drawable.textview_orange_style);
				} else if ("公告".equals(newsList.get(i).getType())){
					tv_tag.setBackgroundResource(R.drawable.textview_red_style);
				} else {
					tv_tag.setBackgroundResource(R.drawable.textview_blue_style);
				}
				tv_tag.setText(newsList.get(i).getType());

				TextView tv_title = (TextView) view.findViewById(R.id.gamedetail_news_item_title);
				tv_title.setText(newsList.get(i).getTitle());

				final NewsEntity newsEntity = newsList.get(i);
				final int ii = i;
				view.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Map<String, Object> kv = new HashMap<>();
						kv.put("名字", newsEntity.getTitle());
						kv.put("位置", String.valueOf(ii + 1));
						DataUtils.onEvent(context, "点击", "游戏详情-资讯攻略", kv);

						Map<String, Object> map = new HashMap<>();
						map.put("location", "资讯攻略");
						map.put("page", "游戏详情");
						map.put("news", newsEntity.getTitle());
						map.put("news_id", newsEntity.getId());
						DataCollectionManager.onEvent(context, "click-item", map);

						// 统计阅读量
						NewsUtils.statNewsViews(newsEntity.getId());
						NewsUtils.startNewsActivity(context, newsEntity, entrance + "+(游戏详情:资讯攻略)");
					}
				});
				viewHolder.gamedetail_item_news_list.addView(view);
			}
		}
	}

	private void initPluginViewHolder(final GameDetailPluginViewHolder viewHolder) {
		ArrayList<TagEntity> tags = gameDetailEntity.getTag();

		viewHolder.gamedetail_ll_plugin.setVisibility(View.GONE);
		viewHolder.open_plugin_detail_ll.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (viewHolder.gamedetail_ll_plugin.getVisibility() == View.GONE){
					viewHolder.gamedetail_ll_plugin.setVisibility(View.VISIBLE);
					viewHolder.gamedetail_ll_plugin_colse.setVisibility(View.GONE);
					viewHolder.open_plugin_detail_img.setImageResource(R.drawable.up_img);
					viewHolder.open_plugin_detail_tv.setText("收起");
				}else {
					viewHolder.gamedetail_ll_plugin.setVisibility(View.GONE);
					viewHolder.gamedetail_ll_plugin_colse.setVisibility(View.VISIBLE);
					viewHolder.open_plugin_detail_img.setImageResource(R.drawable.down_img);
					viewHolder.open_plugin_detail_tv.setText("详情");
				}
			}
		});
		int childCount = viewHolder.gamedetail_ll_plugin.getChildCount();
		if (childCount == 0) {
			View view;
			ImageView iv_hint;
			TextView tv_hint;
			TextView tv_content;
			TagEntity tagEntity;
			for (int i = 0, size = tags.size() + 1; i < size; i++) {
				view = View.inflate(context, R.layout.gamedetail_plugin_item, null);
				iv_hint = (ImageView) view.findViewById(R.id.gamedetail_iv_hint);
				tv_hint = (TextView) view.findViewById(R.id.gamedetail_tv_hint);
				tv_content = (TextView) view.findViewById(R.id.gamedetail_tv_content);
				if (i == 0) {
					tv_hint.setTextColor(Color.parseColor("#00B7FA"));
					if (TextUtils.isEmpty(gameDetailEntity.getRemind())) {
						tv_content.setText("无需root尽享插件功能");
					} else {
						tv_content.setText(gameDetailEntity.getRemind());
					}
				} else {
					tagEntity = tags.get(i - 1);
					tv_content.setText(tagEntity.getDes());
					tv_hint.setText(tagEntity.getName());
					ImageUtils.getInstance(context).display(tagEntity.getIcon(), iv_hint);
				}
				viewHolder.gamedetail_ll_plugin.addView(view);
			}

			view = View.inflate(context, R.layout.gamedetail_plugin_item, null);
			tv_hint = (TextView) view.findViewById(R.id.gamedetail_tv_hint);
			tv_content = (TextView) view.findViewById(R.id.gamedetail_tv_content);
			if (TextUtils.isEmpty(gameDetailEntity.getRemind())) {
				tv_content.setText("无需root尽享插件功能");
			} else {
				tv_content.setText(gameDetailEntity.getRemind());
			}
			tv_hint.setText("温馨提示");
			tv_hint.setTextColor(Color.parseColor("#00B7FA"));

			viewHolder.gamedetail_ll_plugin_colse.addView(view);

			RecyclerView recyclerView = new RecyclerView(context);
			recyclerView.setLayoutManager(new GridLayoutManager(context, 3));

			int index = (int) Math.ceil(tags.size() / 3.0);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(context, index * 28));
			params.setMargins(0, DisplayUtils.dip2px(context, index * 3), 0, 0);

			recyclerView.setLayoutParams(params);
			recyclerView.setAdapter(new PluginAdapter(tags));
			viewHolder.gamedetail_ll_plugin_colse.addView(recyclerView);
		}
	}

	private class PluginAdapter extends RecyclerView.Adapter<PluginAdapter.ViewHolder>{
		private ArrayList<TagEntity> tags;

		public PluginAdapter(ArrayList<TagEntity> tags) {
			this.tags = tags;
		}

		@Override
		public int getItemViewType(int position) {
			return position;
		}

		@Override
		public PluginAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			LinearLayout linearLayout = new LinearLayout(context);
			linearLayout.setOrientation(LinearLayout.HORIZONTAL);
			LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(context, 28));
			linearLayout.setLayoutParams(llParams);

			switch (viewType % 3) {
				case 0:
					linearLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
					break;
				case 1:
					linearLayout.setGravity(Gravity.CENTER);
					break;
				default:
					linearLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
			}

			ImageView pluginIcon = new ImageView(context);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DisplayUtils.dip2px(context, 15), DisplayUtils.dip2px(context, 15));
			params.setMargins(0, 0, DisplayUtils.dip2px(context, 5), 0);
			pluginIcon.setLayoutParams(params);

			TextView pluginTitle = new TextView(context);
			pluginTitle.setTextSize(15);
			pluginTitle.setTextColor(Color.parseColor("#06d0a8"));

			linearLayout.addView(pluginIcon);
			linearLayout.addView(pluginTitle);
			return new ViewHolder(linearLayout);
		}

		@Override
		public void onBindViewHolder(PluginAdapter.ViewHolder holder, int position) {
			holder.pluginTitle.setText(tags.get(position).getName());
			ImageUtils.getInstance(context).display(tags.get(position).getIcon(), holder.pluginIcon);
		}

		@Override
		public int getItemCount() {
			return tags.size();
		}

		public class ViewHolder extends RecyclerView.ViewHolder{

			LinearLayout linearLayout;
			ImageView pluginIcon;
			TextView pluginTitle;

			public ViewHolder(View itemView) {
				super(itemView);
				linearLayout = (LinearLayout) itemView;
				pluginIcon = (ImageView) linearLayout.getChildAt(0);
				pluginTitle = (TextView) linearLayout.getChildAt(1);
			}
		}
	}

	private void initTopViewHolder(final GameDetailTopViewHolder viewHolder) {
		if (concernManager.isConcern(gameId)) {
			viewHolder.gamedetail_tv_concern.setText("取消关注");
			viewHolder.gamedetail_tv_concern.setBackgroundResource(R.drawable.border_red_bg);
			viewHolder.gamedetail_tv_concern.setTextColor(Color.parseColor("#ff4147"));
		} else {
			viewHolder.gamedetail_tv_concern.setText("关注");
			viewHolder.gamedetail_tv_concern.setBackgroundResource(R.drawable.textview_concern_red_style);
			viewHolder.gamedetail_tv_concern.setTextColor(0xffffffff);
		}

		viewHolder.gamedetail_tv_name.setText(gameEntity.getName());
		ImageUtils.getInstance(context).display(gameEntity.getIcon(), viewHolder.gamedetail_iv_thumb);
		if (gameEntity.getApk() == null || gameEntity.getApk().isEmpty()) {
			viewHolder.gamedetail_tv_info.setText("");
		} else {
			ApkEntity apkEntity = gameEntity.getApk().get(0);
			viewHolder.gamedetail_tv_info.setText(String.format("V%s | %s", apkEntity.getVersion(), apkEntity.getSize()));
		}
		viewHolder.gamedetail_tv_concern.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final TextView concern = (TextView) v;
				if ("关注".equals(concern.getText().toString())) {

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

					Map<String, Object> kv2 = new HashMap<>();
					kv2.put("点击", "关注");
					DataUtils.onEvent(context, "插件数据", gameEntity.getName(), kv2);

					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);
					concern.setText("取消关注");
					concern.setBackgroundResource(R.drawable.border_red_bg);
					concern.setTextColor(Color.parseColor("#ff4147"));

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

					//添加关注
					String uuid = TokenUtils.getDeviceId(context);
					ConcernUtils.postConcernGameId(gameId, Config.HOST + "v2d0/device/" + uuid + "/concern",
							new ConcernUtils.DownJsonListener() {
						@Override
						public void downSucced(String str) {
							Utils.log("关注提交成功==游戏详情");
						}

						@Override
						public void downFailed() {
							Utils.log("关注提交失败==游戏详情");
						}
					});
				} else {
					Map<String, Object> kv2 = new HashMap<>();
					kv2.put("点击", "取消关注");
					DataUtils.onEvent(context, "插件数据", gameEntity.getName(), kv2);

					DialogUtils.showCancelDialog(context, new DialogUtils.ConfiremListener() {
						@Override
						public void onConfirem() {
							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.deleteConcern(gameEntity.getId());
							concern.setText("关注");
							concern.setBackgroundResource(R.drawable.textview_red_style);
							concern.setTextColor(0xffffffff);

							//取消关注
							String uuid = TokenUtils.getDeviceId(context);
							ConcernUtils.deleteConcernData(Config.HOST + "v2d0/device/" + uuid + "/concern/" + gameId,
									new ConcernUtils.DownJsonListener() {
								@Override
								public void downSucced(String str) {
									Utils.log("删除提交成功==游戏详情");
								}

								@Override
								public void downFailed() {
									Utils.log("删除提交失败==游戏详情");
								}
							});
						}
					});
				}
			}
		});

	}

	private void initPosition() {
		if (gameDetailEntity != null) {
			position_top = 0;
		}
		if (gameDetailEntity.getServerInfo() != null
				&& gameDetailEntity.getServerInfo().size() != 0) {
			position_newsserver = 1;
		}
		if (gameDetailEntity.getTag() != null
				&& gameDetailEntity.getTag().size() != 0) {
			if (position_newsserver == -1) {
				position_plugin = 1;
			} else {
				position_plugin = 2;
			}
		}
		if (gameDetailEntity.getNews() != null
				&& gameDetailEntity.getNews().size() != 0) {
			if (position_newsserver == -1 && position_plugin == -1) {
				position_news = 1;
			} else if (position_plugin != -1) {
				position_news = position_plugin + 1;
			} else if (position_newsserver != -1) {
				position_news = position_newsserver + 1;
			} else {
				position_news = 1;
			}
		}
		if (gameDetailEntity.getGallery() != null
				&& gameDetailEntity.getGallery().size() != 0) {
			if (position_newsserver == -1 && position_plugin == -1 && position_news == -1) {
				position_intro = 1;
			} else if (position_news != -1) {
				position_intro = position_news + 1;
			} else if (position_plugin != -1) {
				position_intro = position_plugin + 1;
			} else if (position_newsserver != -1) {
				position_intro = position_newsserver + 1;
			} else {
				position_intro = 1;
			}
		}
	}

	@Override
	public int getItemCount() {
		if (gameDetailEntity == null) {
			return 0;
		} else {
			int index = 0;
			if (position_top != -1) {
				index++;
			}
			if (position_newsserver != -1) {
				index++;
			}
			if (position_plugin != -1) {
				index++;
			}
			if (position_news != -1) {
				index++;
			}
			if (position_intro != -1) {
				index++;
			}
			return index;
		}
	}

	@Override
	public int getItemViewType(int position) {
		if (position_top != -1 && position == position_top) {
			return 1;
		}
		if (position_newsserver != -1 && position == position_newsserver) {
			return 2;
		}
		if (position_plugin != -1 && position == position_plugin) {
			return 3;
		}
		if (position_news != -1 && position == position_news) {
			return 4;
		}
		if (position_intro != -1 && position == position_intro) {
			return 5;
		}
		return position;
	}

}
