package com.gh.gamecenter.info;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.gh.gamecenter.common.constant.Constants;
import com.gh.gamecenter.core.utils.DisplayUtils;

import androidx.core.content.ContextCompat;

import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.StrategyDialogViewHolder;
import com.gh.gamecenter.databinding.DialogStrategyItemBinding;
import com.gh.gamecenter.feature.entity.GameEntity;
import com.lightgame.adapter.BaseRecyclerAdapter;

import java.util.List;

/**
 * Created by khy on 2016/12/5.
 * 资讯-攻略关注的游戏弹窗适配器
 */
class StrategyDialogAdapter extends BaseRecyclerAdapter<StrategyDialogViewHolder> {

    private OnStrategyDialogCallBackListener callBackListener;

    private List<GameEntity> mConcernGame;

    StrategyDialogAdapter(StrategyFragment fragment, List<GameEntity> concernGame) {
        super(fragment.getContext());
        this.callBackListener = fragment;
        this.mConcernGame = concernGame;
        for (int i = 0; i < mConcernGame.size(); i++) {
            if (Constants.GHZS_GAME_ID.equals(mConcernGame.get(i).getId())) {
                mConcernGame.remove(i);
            }
        }
    }

    @Override
    public StrategyDialogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new StrategyDialogViewHolder(DialogStrategyItemBinding.inflate(mLayoutInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(final StrategyDialogViewHolder holder, int position) {
        holder.binding.dialogStrategyItemName.setTextColor(ContextCompat.getColor(holder.binding.getRoot().getContext(), com.gh.gamecenter.common.R.color.text_theme));
        holder.binding.dialogStrategyItemName.setText(mConcernGame.get(position).getName());
        holder.binding.dialogStrategyItemIcon.setVisibility(View.INVISIBLE);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (getItemCount() == 1) {
            params.setMargins(0, DisplayUtils.dip2px(mContext, 12), 0, DisplayUtils.dip2px(mContext, 12));
        } else if (position == 0) {
            params.setMargins(0, DisplayUtils.dip2px(mContext, 12), 0, 0);
        } else if (position == getItemCount() - 1) {
            params.setMargins(0, 0, 0, DisplayUtils.dip2px(mContext, 12));
        } else {
            params.setMargins(0, 0, 0, 0);
        }
        holder.binding.getRoot().setLayoutParams(params);

        holder.binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBackListener.selectPosition(holder.getAdapterPosition(), mConcernGame.get(holder.getAdapterPosition()));
            }
        });
    }

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


}
