package com.unibuy.smartdevice.ui.recycler;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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


import com.unibuy.smartdevice.databinding.RecyclerDialogTakeMedicineListBinding;
import com.unibuy.smartdevice.devices.SlotField;
import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.structure.BuyStructure;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.ui.tools.ImageGlide;

import java.util.List;

public class RecyclerDialogTakeMedicineListAdpter extends RecyclerView.Adapter<RecyclerDialogTakeMedicineListAdpter.ViewHolder> {
    private Logs logs;
    private Context context;
    private final List<BuyStructure> buyList;
    private HandlerMain handlerMain;

    public RecyclerDialogTakeMedicineListAdpter(Context context, List<BuyStructure> buyList, HandlerMain handlerMain) {
        this.logs = new Logs(this.getClass());
        this.context = context;
        this.buyList = buyList;
        this.handlerMain = handlerMain;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        RecyclerDialogTakeMedicineListBinding binding;

        public ViewHolder(@NonNull RecyclerDialogTakeMedicineListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 使用 ViewBinding 來綁定佈局
        RecyclerDialogTakeMedicineListBinding binding = RecyclerDialogTakeMedicineListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);

        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        logs.info("position:" + position);

        BuyStructure buyProduct = buyList.get(position);
        holder.binding.textPosition.setText(String.valueOf(buyProduct.getSlot()));
        String productID = buyProduct.getProduct().getProductID();
        if (!productID.isEmpty() && !productID.equals("?")) {
            holder.binding.textProductName.setText(buyProduct.getProduct().getProductName());
        } else {
            holder.binding.textProductName.setText("點我選擇商品");
        }

        String urlString = buyProduct.getProduct().getProductImg();
        try {
            new ImageGlide(context).fileload(urlString, holder.binding.imageProductPicture);
        } catch (LogsEmptyException e) {
            this.logs.warning(e);
        }
        int machinePrice = buyProduct.getProduct().getMachinePrice();
        int sellingPrice = buyProduct.getProduct().getSellingPrice();
        int count = buyProduct.getCount();

        holder.binding.textCount.setText("x"+String.valueOf(count));

        logs.info("field:" + buyProduct.getField());

        try {
            SlotField slotField = SlotField.getSlotField(buyProduct.getField());
            logs.info("slotField:" + slotField.isCanAddPurchased());
        } catch (LogsEmptyException e) {
        }
    }

    @Override
    public int getItemCount() {
        return buyList.size();
    }

}
