package com.tofly.ghrq_route.common.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.buildManage.domain.BuildFileBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BuildFileAdapte extends BaseAdapter {

    private Context context;
    private List<BuildFileBean> data;

    public BuildFileAdapte(Context context, List<BuildFileBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_build_file, null);
        BuildFileViewHolder viewHolder = new BuildFileViewHolder(convertView);
        convertView.setTag(viewHolder);
        final BuildFileBean news = data.get(position);
        if (!TextUtils.isEmpty(news.getFileName())) {
            viewHolder.tvFileName.setText(news.getFileName());
        }
        viewHolder.ivClear.setOnClickListener(v -> {
            data.remove(position);
            notifyDataSetChanged();
        });
        return convertView;
    }

    public static class BuildFileViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_file_name)
        TextView tvFileName;
        @BindView(R.id.iv_clear)
        ImageView ivClear;

        public BuildFileViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
