package com.gh.gamecenter.adapter;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.GameNewsTypeViewHolder;
import com.gh.gamecenter.eventbus.EBTypeChange;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by LGT on 2016/8/29.
 */
public class GameNewsTypeListAdapter extends RecyclerView.Adapter<GameNewsTypeViewHolder> {

	private ArrayList<String> typeList;

	private String newsType;

	private int currentPosition;

	public GameNewsTypeListAdapter(ArrayList<String> tList, String type) {

		typeList = tList;

		newsType = type;
		currentPosition = 0;
	}

	// 绘图圆角带填充色
	public GradientDrawable createRoundCornerShape(int strokeWidth,int roundRadius, int strokeColor, int fillColor) {
		GradientDrawable gd = new GradientDrawable();
		gd.setColor(fillColor);
		gd.setCornerRadius(roundRadius);
		gd.setStroke(strokeWidth, strokeColor);
		return gd;
	}

	public StateListDrawable selectorBg(){

		GradientDrawable roundCornerShape1 = createRoundCornerShape(2, 50, Color.parseColor("#b3b3b3"), Color.parseColor("#ffffff"));
		GradientDrawable roundCornerShape2 = createRoundCornerShape(0, 50, Color.TRANSPARENT, Color.parseColor("#1BA4FC"));
		GradientDrawable roundCornerShape3 = createRoundCornerShape(0, 50, Color.TRANSPARENT, Color.parseColor("#59d3ff"));
		StateListDrawable bg_install = new StateListDrawable();

		bg_install.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled },
				roundCornerShape3);
		bg_install.addState(new int[] { android.R.attr.state_selected, android.R.attr.state_enabled },
				roundCornerShape2);
		bg_install.addState(new int[] {}, roundCornerShape1);

		return bg_install;
	}

	@Override
	public GameNewsTypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.game_news_type_list_item, parent, false);
		view.findViewById(R.id.game_news_type_list_tv_type).setBackgroundDrawable(selectorBg());
		return new GameNewsTypeViewHolder(view);
	}

	@Override
	public void onBindViewHolder(final GameNewsTypeViewHolder holder, int position) {

		String type = typeList.get(position);

		holder.game_news_type_list_tv_type.setText(type);

		if (type.equals(newsType)) {
			holder.game_news_type_list_tv_type.setSelected(true);
			holder.game_news_type_list_tv_type.setTextColor(Color.WHITE);
			currentPosition = position;
		} else {
			holder.game_news_type_list_tv_type.setSelected(false);
			holder.game_news_type_list_tv_type.setTextColor(Color.parseColor("#b3b3b3"));
		}

		holder.game_news_type_list_tv_type.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EventBus.getDefault().post(new EBTypeChange(typeList.get(holder.getPosition()), holder.getPosition()));
			}
		});
	}

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

	public void setNewsType(String newsType, int position) {
		this.newsType = newsType;
		notifyDataSetChanged();//感觉 position 和 currentPosition 不准确，只能用这个刷新了
//		notifyItemChanged(currentPosition);
//		notifyItemChanged(position);
	}
}
