package com.example.tyre.maintenance.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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


import com.bumptech.glide.Glide;
import com.example.tyre.BR;
import com.example.tyre.R;
import com.example.tyre.databinding.ItemDisposalImgBinding;
import com.example.tyre.maintenance.been.CheckInstanceFiles;

import java.util.List;

/**
 * @author wanghao
 * @date 2024/3/11
 */
public class DisposalFileAdapter extends RecyclerView.Adapter<DisposalFileAdapter.MyViewHolder> {
    private List<CheckInstanceFiles> list;
    private static Context context;
    private LayoutInflater inflater;
    private ItemClickCall clickCall;
    private boolean delectState=false;

    public void setDelectState(boolean delectState) {
        this.delectState = delectState;
    }

    public DisposalFileAdapter(Context context, ItemClickCall clickCall) {
        this.context = context;
        this.clickCall = clickCall;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<CheckInstanceFiles> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDisposalImgBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_disposal_img, parent, false);
        return new MyViewHolder(binding);
    }

    // @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        var outstock = list.get(position);
        var binding = holder.binding;
        binding.setVariable(BR.vm,outstock);
        Log.e("TAG", "onBindViewHolder:" + outstock.isState());
        Glide.with(context)
                .load(outstock.getFaultFile())
                .into(binding.disFile);
        binding.disFile.setOnClickListener(v -> clickCall.onClick(position,true));
        if (delectState){
            binding.disposalDelect.setVisibility(View.GONE);
        }else{
            binding.disposalDelect.setVisibility(View.VISIBLE);
            binding.disposalDelect.setOnClickListener(v -> clickCall.onClick(position,false));
        }

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ItemDisposalImgBinding binding;

        public MyViewHolder(ItemDisposalImgBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }


    }
    public interface ItemClickCall {
        void onClick(int position,boolean type);
    }
}
