package com.gh.gamecenter.search;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.view.CardLinearLayout;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.ReuseViewHolder;
import com.gh.gamecenter.db.SearchHistoryDao;
import com.gh.gamecenter.eventbus.EBSearch;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class SearchHistoryFragmentAdapter extends RecyclerView.Adapter<ViewHolder> {

	private Context context;

	private SearchHistoryDao dao;

	private List<String> historyList;

	public SearchHistoryFragmentAdapter(Context context) {

		this.context = context;

		dao = new SearchHistoryDao(context);
		historyList = dao.getAll();
		if (historyList == null) {
			historyList = new ArrayList<>();
		}
	}

	public void clearSearchResult() {
		if (historyList != null) {
			historyList.clear();
		}
		notifyDataSetChanged();
	}

	@Override
	public int getItemCount() {
		if (historyList.isEmpty()) {
			return 0;
		}
		return historyList.size() + 1;
	}

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

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
		if (viewType == historyList.size()) {
			View itemView = LayoutInflater.from(context).inflate(
					R.layout.fm_search_history_delete_item, viewGroup, false);
			return new ReuseViewHolder(itemView);
		} else {
			View view = LayoutInflater.from(context).inflate(
					R.layout.fm_search_history_item, viewGroup, false);
			return new SearchHistoryViewHolder(view);
		}
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		if (holder instanceof SearchHistoryViewHolder) {
			final SearchHistoryViewHolder viewHolder = (SearchHistoryViewHolder) holder;

			// 第一个
			if (position == 0) {
				((CardLinearLayout) viewHolder.itemView).setmTop(DisplayUtils.dip2px(context, 8));
				viewHolder.search_history_line.setVisibility(View.GONE);
			} else {
				((CardLinearLayout) viewHolder.itemView).setmTop(0);
				viewHolder.search_history_line.setVisibility(View.VISIBLE);
			}

			// 最后一个
			if (position + 1 == historyList.size()) {
				((CardLinearLayout) viewHolder.itemView).setmBottom(DisplayUtils.dip2px(context, 8));
				((CardLinearLayout) viewHolder.itemView).setBottom(false);
			} else {
				((CardLinearLayout) viewHolder.itemView).setmBottom(0);
				((CardLinearLayout) viewHolder.itemView).setBottom(false);
			}

			viewHolder.search_history_name.setText(historyList.get(position));
			viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String key = viewHolder.search_history_name.getText().toString();
					EventBus.getDefault().post(new EBSearch(key, true));
					dao.add(viewHolder.search_history_name.getText().toString());
				}
			});
		} else if (holder instanceof ReuseViewHolder) {
			holder.itemView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					DialogUtils.showWarningDialog(context, "清空记录", "确定清空历史搜索记录？",
							new DialogUtils.ConfiremListener() {
								@Override
								public void onConfirem() {
									dao.deleteAll();
									clearSearchResult();
								}
							});
				}
			});
		}
	}

}
