package com.tencent.liteav.trtcvoiceroom.ui.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.tencent.liteav.trtcvoiceroom.R;
import com.tencent.qcloud.tuicore.util.ScreenUtil;
import com.zxhl.cms.AppContext;
import com.zxhl.cms.common.base.BaseRecyclerAdapter;
import com.zxhl.cms.net.model.chat.ChatGiftListBackEntity;
import com.zxhl.cms.utils.NoDoubleClickListener;
import com.zxhl.cms.utils.OnRecycleItemClickListener;


public class GiftChildAdapter extends BaseRecyclerAdapter<ChatGiftListBackEntity.ChatGiftListEntity, GiftChildAdapter.BaseViewHolder> {

    private OnRecycleItemClickListener<ChatGiftListBackEntity.ChatGiftListEntity> listener;

    public GiftChildAdapter(OnRecycleItemClickListener<ChatGiftListBackEntity.ChatGiftListEntity> listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(AppContext.get()).inflate(R.layout.item_layout_child_gift, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(AppContext.get()).load(mList.get(position).img).apply(options).into(holder.giftImg);
        holder.giftName.setText(mList.get(position).title);
        holder.giftPrice.setText(mList.get(position).priceStr);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.items.getLayoutParams();
        int spaceWidth = ScreenUtil.getScreenWidth(AppContext.get()) - ScreenUtil.dip2px(5f) * 2;
        layoutParams.width = spaceWidth / 4;
        holder.items.setLayoutParams(layoutParams);
        holder.items.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                listener.onItemClick(v,position,mList.get(position));
            }
        });
    }


    class BaseViewHolder extends RecyclerView.ViewHolder {
        TextView giftName;
        TextView giftPrice;
        AppCompatImageView giftImg;
        LinearLayout items;

        public BaseViewHolder(View itemView) {
            super(itemView);
            items = itemView.findViewById(R.id.id_ll_item);
            giftName = itemView.findViewById(R.id.id_tv_gift_name);
            giftPrice = itemView.findViewById(R.id.id_tv_gift_price);
            giftImg = itemView.findViewById(R.id.id_img_gift);
        }
    }
}
