package com.example.beijing_daxing.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beijing_daxing.BR;
import com.example.beijing_daxing.R;
import com.example.beijing_daxing.base.AdapterClickCall;
import com.example.beijing_daxing.been.CheckTaskInfo;
import com.example.beijing_daxing.been.Stock;
import com.example.beijing_daxing.databinding.ItemCheckBinding;
import com.example.beijing_daxing.databinding.ItemCheckListBinding;

import java.util.List;

/**
 * @author wanghao
 * @date 2024/1/19  14:32
 */
public class CheckListAdapter extends RecyclerView.Adapter<CheckListAdapter.MyViewHoder> {
    private Context context;
    private List<CheckTaskInfo> list;
    private LayoutInflater inflater;
    private AdapterClickCall call;
    public CheckListAdapter(Context context,AdapterClickCall call) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.call=call;
    }

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

    @Override
    public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemCheckListBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_check_list, parent, false);
        return new MyViewHoder(binding);
    }

    @Override
    public void onBindViewHolder(MyViewHoder holder, int position) {
        var binding = holder.getBinding();
        binding.setVariable(BR.vm, list.get(position));
        binding.checkListStart.setOnClickListener(v ->  call.clickItem(position));
        binding.executePendingBindings();

    }

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

    public class MyViewHoder extends RecyclerView.ViewHolder {
        private ItemCheckListBinding binding;

        public MyViewHoder( ItemCheckListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public ItemCheckListBinding getBinding() {
            return binding;
        }
    }



    @BindingAdapter("customBackground")
    public static void setCustomBackground(TextView textView, String taskState) {
        if ("待完成".equals(taskState)) {
            textView.setBackgroundResource(R.drawable.state_n_bg);
        } else {
            textView.setBackgroundResource(R.drawable.state_y_bg);
        }
    }
}
