package com.gh.gamecenter.gamedetail;

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

import com.gh.common.util.DisplayUtils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.entity.ServerEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

/**
 * Created by LGT on 2016/9/18.
 */
public class GameDetailNewsServerAdapter extends RecyclerView.Adapter<GameDetailNewsServerItemViewHolder> {

	private Context context;

	private ArrayList<ServerEntity> serverInfo;

	public GameDetailNewsServerAdapter(Context context, ArrayList<ServerEntity> list) {
		this.context = context;
		this.serverInfo = list;
	}

	@Override
	public GameDetailNewsServerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.gamedetail_newservice_item, parent, false);
		int width = context.getResources().getDisplayMetrics().widthPixels
				- DisplayUtils.dip2px(context, 90);
		RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(
				width / 3, DisplayUtils.dip2px(context, 1));
		rparams.addRule(RelativeLayout.CENTER_IN_PARENT);
		view.findViewById(R.id.newsserver_item_line).setLayoutParams(rparams);
		return new GameDetailNewsServerItemViewHolder(view);
	}

	@Override
	public void onBindViewHolder(GameDetailNewsServerItemViewHolder viewHolder, int position) {
		ServerEntity serverEntity = serverInfo.get(position);
		viewHolder.newsserver_item_name.setText(serverEntity.getServer());
		String tag = serverEntity.getTag();
		if ("昨天".equals(tag)) {
			viewHolder.newsserver_item_name
					.setBackgroundResource(R.drawable.newserver_gray);
			viewHolder.newsserver_item_point
					.setBackgroundResource(R.drawable.oval_gray_bg);
		} else if ("今天".equals(tag)) {
			viewHolder.newsserver_item_name
					.setBackgroundResource(R.drawable.newserver_red);
			viewHolder.newsserver_item_point
					.setBackgroundResource(R.drawable.oval_scarlet_bg);
		} else {
			viewHolder.newsserver_item_name
					.setBackgroundResource(R.drawable.newserver_orange);
			viewHolder.newsserver_item_point
					.setBackgroundResource(R.drawable.oval_orange_bg);
		}
		SimpleDateFormat format = new SimpleDateFormat(tag + "kk:mm", Locale.getDefault());
		viewHolder.newsserver_item_time.setText(format.format(serverEntity.getTime()));
	}

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

}
