package com.gh.gamecenter.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gh.base.AppController;
import com.gh.gamecenter.R;

public class SearchGameDetailFragment extends Fragment {

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

	private View view;
	private RecyclerView search_detail;
	private LinearLayout search_loading;
	private LinearLayout reuse_none_date;
	private LinearLayout reuse_no_connection;

	private String key;

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

		view = View.inflate(getActivity(), R.layout.fm_search, null);

		TextView reuse_tv_none_data = (TextView) view.findViewById(R.id.reuse_tv_none_data);
		reuse_tv_none_data.setText("搜索结果为空");

		search_loading = (LinearLayout) view.findViewById(R.id.search_gamedetail_ll_loading);
		reuse_none_date = (LinearLayout) view.findViewById(R.id.reuse_none_data);
		reuse_no_connection = (LinearLayout) view.findViewById(R.id.reuse_no_connection);

		search_detail = (RecyclerView) view.findViewById(R.id.search_detail);
		search_detail.setHasFixedSize(true);
		search_detail.setLayoutManager(new LinearLayoutManager(getActivity()));
		search_detail.setAdapter(new SearchGameDetailFragmentAdapter(getActivity(), search_detail,
				search_loading, reuse_none_date, reuse_no_connection, key));
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
							 @Nullable Bundle savedInstanceState) {
		if (container != null) {
			container.removeView(view);
		}
		return view;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public void search(String key) {
		search_detail.setAdapter(new SearchGameDetailFragmentAdapter(getActivity(), search_detail,
				search_loading, reuse_none_date, reuse_no_connection, key));
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		AppController.canclePendingRequests(TAG);
		view = null;
		search_detail = null;
		search_loading = null;
		reuse_none_date = null;
		reuse_no_connection = null;
		key = null;
	}

}
