package com.gh.gamecenter.adapter;

import android.content.Context;
import android.graphics.Color;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lightgame.adapter.BaseRecyclerAdapter;
import com.gh.gamecenter.core.utils.DisplayUtils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.SubjectTypeAdapter.GameTypeViewHolder;

import java.util.List;

/**
 * Created by khy on 2017/3/16.
 */
public class SubjectTypeAdapter extends BaseRecyclerAdapter<GameTypeViewHolder> {

    private OnSelectTypeListener mListener;
    private List<String> mGameType;
    private String mCurType;

    public SubjectTypeAdapter(Context context, OnSelectTypeListener listener, List<String> gameType) {
        super(context);
        this.mListener = listener;

        mGameType = gameType;
        mCurType = "全部";
    }

    public SubjectTypeAdapter(Context context, OnSelectTypeListener listener, List<String> gameType, String initTitle) {
        super(context);
        this.mListener = listener;

        mGameType = gameType;
        mCurType = initTitle;
    }

    @Override
    public GameTypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RelativeLayout relativeLayout = new RelativeLayout(mContext);
        relativeLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , DisplayUtils.dip2px(mContext, 35)));
        relativeLayout.setGravity(Gravity.CENTER);
        TextView textView = new TextView(mContext);
        textView.setTextSize(14);
        textView.setBackgroundResource(R.drawable.subject_tab_style);
        textView.setTextColor(ContextCompat.getColor(mContext, R.color.title));
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                , DisplayUtils.dip2px(mContext, 25)));
        textView.setGravity(Gravity.CENTER);
        textView.setPadding(DisplayUtils.dip2px(mContext, 10), 0, DisplayUtils.dip2px(mContext, 10), 0);
        relativeLayout.addView(textView);
        return new GameTypeViewHolder(relativeLayout);
    }


    @Override
    public void onBindViewHolder(final GameTypeViewHolder holder, int position) {
        holder.type.setText(mGameType.get(position));

        if (!TextUtils.isEmpty(mCurType) && mCurType.equals(mGameType.get(position))) {
            holder.type.setSelected(true);
            holder.type.setTextColor(Color.WHITE);

        } else {
            holder.type.setSelected(false);
            holder.type.setTextColor(ContextCompat.getColor(mContext, R.color.title));
        }

        holder.type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.type.setSelected(true);
                mListener.onSelectType(mGameType.get(holder.getAdapterPosition()));
                mCurType = mGameType.get(holder.getAdapterPosition());
                notifyItemRangeChanged(0, getItemCount());
            }
        });
    }

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

    public interface OnSelectTypeListener {
        void onSelectType(String type);
    }

    public class GameTypeViewHolder extends RecyclerView.ViewHolder {
        private TextView type;

        public GameTypeViewHolder(View itemView) {
            super(itemView);
            type = (TextView) ((RelativeLayout) itemView).getChildAt(0);
        }
    }
}
