package com.gh.gamecenter.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

import androidx.core.content.ContextCompat;

import android.view.View;
import android.view.ViewGroup;

import com.gh.gamecenter.databinding.GameNewsTypeListItemBinding;
import com.lightgame.adapter.BaseRecyclerAdapter;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.GameNewsTypeViewHolder;
import com.gh.gamecenter.eventbus.EBTypeChange;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


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

    private List<String> mTypeList;
    private String mNewsType;

    public GameNewsTypeListAdapter(Context context, List<String> tList, String type) {
        super(context);
        mTypeList = tList;
        mNewsType = type;
    }

    @Override
    public GameNewsTypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        GameNewsTypeListItemBinding binding = GameNewsTypeListItemBinding.inflate(mLayoutInflater, parent, false);
        binding.gameNewsTypeListTvType.setBackgroundDrawable(selectorBg());
        return new GameNewsTypeViewHolder(binding);
    }

    public StateListDrawable selectorBg() {

        GradientDrawable roundCornerShape1 = createRoundCornerShape(2, 50, ContextCompat.getColor(mContext, R.color.hint), Color.WHITE);
        GradientDrawable roundCornerShape2 = createRoundCornerShape(0, 50, Color.TRANSPARENT, ContextCompat.getColor(mContext, R.color.type_gonglue));
        GradientDrawable roundCornerShape3 = createRoundCornerShape(0, 50, Color.TRANSPARENT, ContextCompat.getColor(mContext, R.color.news_type_pressed));
        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;
    }

    // 绘图圆角带填充色
    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;
    }

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

        String type = mTypeList.get(position);

        holder.binding.gameNewsTypeListTvType.setText(type);

        if (type.equals(mNewsType)) {
            holder.binding.gameNewsTypeListTvType.setSelected(true);
            holder.binding.gameNewsTypeListTvType.setTextColor(Color.WHITE);
        } else {
            holder.binding.gameNewsTypeListTvType.setSelected(false);
            holder.binding.gameNewsTypeListTvType.setTextColor(ContextCompat.getColor(mContext, R.color.hint));
        }

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

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

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