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.bean.UpLoadFileBean;
import com.zztrapp.zztrapp.databinding.ItemRvUpLoadFileBinding;

import java.util.List;

/**
 * Describe 上传文件
 * Created by Bby on 2022/6/22
 * Email bby0856@163.com
 */

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

    private Context mContext;
    private OnItemClickListener onItemClickListener;
    private List<UpLoadFileBean> list;
    private int jumpType;//1编辑 2查看详情

    public interface OnItemClickListener {
        void onDelClick(int position);

        void onDownloadClick(String filePath, String fileName);
    }

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

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

    public UpLoadFileAdapter(Context context, int jumpType) {
        this.mContext = context;
        this.jumpType = jumpType;
    }

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

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemRvUpLoadFileBinding binding = ItemRvUpLoadFileBinding.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).getFileName());
        switch (jumpType) {
            case 1:
                holder.bindings.download.setVisibility(View.GONE);
                holder.bindings.del.setVisibility(View.VISIBLE);
                break;
            case 2:
                holder.bindings.download.setVisibility(View.VISIBLE);
                holder.bindings.del.setVisibility(View.GONE);
                break;
        }
        holder.bindings.download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onDownloadClick(list.get(position).getFilePath(), list.get(position).getFileName());
                }
            }
        });
        holder.bindings.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onDelClick(position);
                }
            }
        });
    }

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

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

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