package com.bokecc.livemodule.view.gift;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bokecc.gift.pojo.GiftInfo;
import com.bokecc.dwlivedemo.R;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class GiftListAdapter extends RecyclerView.Adapter<GiftListAdapter.GiftListViewHolder> {
    private OnClickOnItemClickListener listener;
    private ArrayList<GiftInfo> giftList;
    private Context context;
    private int oldIndex = -1;//上次点击位置

    public GiftListAdapter(Context context, ArrayList<GiftInfo> giftList, OnClickOnItemClickListener listener){
        this.context = context;
        this.listener = listener;
        this.giftList = giftList;
    }

    @Override
    public GiftListAdapter.GiftListViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        return new GiftListViewHolder(View.inflate(context, R.layout.item_gift_list,null));
    }

    @Override
    public void onBindViewHolder(final GiftListViewHolder viewHolder, @SuppressLint("RecyclerView") final int i) {
        final GiftInfo giftInfo = giftList.get(i);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (oldIndex != -1){
                    giftList.get(oldIndex).setSelected(false);
                }

                giftList.get(i).setSelected(true);
                notifyDataSetChanged();

                oldIndex = i;
                if (listener != null){
                    listener.onClickItem(giftInfo);
                }
            }
        });

        if (giftInfo.isSelected()) {
            viewHolder.contentView.setBackgroundResource(R.drawable.item_gift_selector_bg);
        } else {
            viewHolder.contentView.setBackgroundResource(R.drawable.item_gift_unselector_bg);
        }

        Glide.with(context).load(giftInfo.giftThumbnail).into(viewHolder.ivGift);
        viewHolder.giftPrice.setText(giftInfo.giftPrice+"");
        viewHolder.giftName.setText(giftInfo.giftName);

    }

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

    public interface OnClickOnItemClickListener{
        void onClickItem(GiftInfo giftInfo);
    }

    static class GiftListViewHolder extends RecyclerView.ViewHolder{
        ImageView ivGift;
        TextView giftName;
        TextView giftPrice;
        View contentView;

        public GiftListViewHolder(View itemView) {
            super(itemView);
            contentView = itemView.findViewById(R.id.content);
            ivGift = itemView.findViewById(R.id.iv_gift);
            giftName = itemView.findViewById(R.id.gif_name);
            giftPrice = itemView.findViewById(R.id.gift_price);
        }
    }

    public void updateData(List<GiftInfo> list){
        this.giftList.clear();
        giftList.addAll(list);
    }
}
