package com.gh.gamecenter;

import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gh.base.AppController;
import com.gh.base.BaseActivity;
import com.gh.common.util.DisplayUtils;
import com.gh.gamecenter.adapter.GameNewsAdapter;
import com.gh.gamecenter.adapter.GameNewsTypeListAdapter;
import com.gh.gamecenter.eventbus.EBTypeChange;

import java.util.ArrayList;

/**
 * Created by LGT on 2016/8/29.
 */
public class GameNewsActivity extends BaseActivity implements View.OnClickListener{

	public static final String TAG = GameNewsActivity.class.getSimpleName();

	private GameNewsAdapter adapter;
	private GameNewsTypeListAdapter typeListAdapter;

	private RecyclerView game_news_list;
	private RecyclerView game_news_top_type_list;
	private LinearLayoutManager layoutManager;
	private ImageView ivSearch;

	private ArrayMap<String, GameNewsAdapter> adapterMap;

	private ArrayList<String> typeList;

	private String gameName;
	private String gameId;
	private String entrance;

	private float startY = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		entrance = getIntent().getStringExtra("entrance");

		View contentView = View.inflate(this, R.layout.activity_game_news, null);

		ivSearch = new ImageView(this);
		ivSearch.setImageResource(R.drawable.search_icon);

		RelativeLayout.LayoutParams ivSearchParams = new RelativeLayout.LayoutParams(
				DisplayUtils.dip2px(this, 20), DisplayUtils.dip2px(this, 20));
		ivSearchParams.addRule( RelativeLayout.CENTER_VERTICAL);
		ivSearchParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		ivSearchParams.setMargins(0, 0, DisplayUtils.dip2px(this, 10), 0);

		RelativeLayout reuse_actionbar = (RelativeLayout) contentView.findViewById(R.id.reuse_actionbar);
		reuse_actionbar.addView(ivSearch, ivSearchParams);
		ivSearch.setOnClickListener(this);
		ivSearch.setVisibility(View.GONE);

		gameName = getIntent().getStringExtra("gameName");
		init(contentView, gameName);

		adapterMap = new ArrayMap<>();

		typeList = getIntent().getStringArrayListExtra("articleTypes");
		typeList.add(0, "全部");

		gameId = getIntent().getStringExtra("gameId");

		game_news_list.setHasFixedSize(true);
		layoutManager = new LinearLayoutManager(this);
		game_news_list.setLayoutManager(layoutManager);
		adapter = new GameNewsAdapter(this, typeList, game_news_list, gameId, "全部", entrance);
		adapterMap.put("全部", adapter);
		game_news_list.setAdapter(adapter);
		game_news_list.setOnScrollListener(new RecyclerView.OnScrollListener() {
			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				super.onScrolled(recyclerView, dx, dy);

				if (layoutManager.findFirstVisibleItemPosition() >= 1) {
					game_news_top_type_list.setVisibility(View.VISIBLE);
					ivSearch.setVisibility(View.VISIBLE);
				} else {
					game_news_top_type_list.setVisibility(View.GONE);
					ivSearch.setVisibility(View.GONE);
				}

			}

			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				super.onScrollStateChanged(recyclerView, newState);
				if (!isDestroy && newState == RecyclerView.SCROLL_STATE_IDLE
						&& layoutManager.findLastVisibleItemPosition() == adapter.getItemCount() - 1) {
					if (!adapter.isRemove() && !adapter.isLoading() && !adapter.isNetworkError()) {
						adapter.addList(adapter.getNewsList().size());
					}
				}
			}
		});

		game_news_top_type_list.setHasFixedSize(true);
		game_news_top_type_list.setLayoutManager(new GridLayoutManager(this, 5));
		typeListAdapter = new GameNewsTypeListAdapter(typeList, "全部");
		game_news_top_type_list.setAdapter(typeListAdapter);

		ViewGroup.LayoutParams params = game_news_top_type_list.getLayoutParams();
		params.height = (int) Math.ceil(typeList.size() / 5f) * DisplayUtils.dip2px(this, 35)+ DisplayUtils.dip2px(this, 12);
		game_news_top_type_list.setLayoutParams(params);

		//禁止由于滑动出现的阴影
		game_news_top_type_list.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					startY = event.getY();
				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					if (Math.abs(startY - event.getY()) > 0) {
						return true;
					}
				}
				return false;
			}
		});
	}

	public void onEventMainThread(EBTypeChange change) {
		adapter = adapterMap.get(change.getType());
		if (adapter == null) {
			adapter = new GameNewsAdapter(this, typeList, game_news_list, gameId, change.getType(), entrance);
			adapterMap.put(change.getType(), adapter);
		}
		game_news_list.setAdapter(adapter);

		game_news_top_type_list.setVisibility(View.GONE);
		ivSearch.setVisibility(View.GONE);
		typeListAdapter.setNewsType(change.getType(), change.getPosition());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		AppController.canclePendingRequests(TAG);
		adapter = null;
		typeListAdapter = null;
	}

	@Override
	public void onClick(View v) {
		if (v == ivSearch){
			game_news_list.scrollToPosition(0);
			game_news_top_type_list.setVisibility(View.GONE);
			ivSearch.setVisibility(View.GONE);
			adapter.openKeyBoard();
		}
	}
}
