package com.zztrapp.zztrapp.ui.currency.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.bean.CurrencyBean;
import com.zztrapp.zztrapp.databinding.ItemRvCurrenccyBinding;

import java.util.List;

/**
 * Describe 通用
 * Created by Bby on 2022/6/22
 * Email bby0856@163.com
 */

public class CurrencyAdapter extends RecyclerView.Adapter<CurrencyAdapter.ItemHolder> {

    private Context mContext;
    private OnItemClickListener onItemClickListener;
    private List<CurrencyBean> list;

    public interface OnItemClickListener {
        void onEnterClick(int type);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public CurrencyAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<CurrencyBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemRvCurrenccyBinding binding = ItemRvCurrenccyBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new ItemHolder(binding);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindings.name.setText(list.get(position).getName());
        holder.bindings.hint.setText(list.get(position).getHint());
        switch (list.get(position).getType()) {
            case 1:
                holder.bindings.icon.setBackgroundResource(R.mipmap.zhaunxiang_ic);
                holder.bindings.hint.setVisibility(View.GONE);
                break;
            case 2:
                holder.bindings.icon.setBackgroundResource(R.mipmap.shipin_ic);
                break;
            case 3:
                holder.bindings.icon.setBackgroundResource(R.mipmap.falv_ic);
                holder.bindings.hint.setVisibility(View.GONE);
                break;
        }
        holder.bindings.enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onEnterClick(list.get(position).getType());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 8;
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        private ItemRvCurrenccyBinding bindings;

        public ItemHolder(ItemRvCurrenccyBinding binding) {
            super(binding.getRoot());
            bindings = binding;
        }
    }
}