package com.unibuy.smartdevice.ui.recycler;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

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

import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.databinding.RecyclerProductSearchBinding;
import com.unibuy.smartdevice.ui.tools.ImageGlide;
import com.unibuy.smartdevice.ui.ProductSearchActivity;

import java.util.List;

public class RecyclerProductSearchAdpter extends RecyclerView.Adapter<RecyclerProductSearchAdpter.ViewHolder> {
    private Logs logs;
    private Context context;
    private final int intentPosition;
    private final List<ProductStructure> ProductList;

    public RecyclerProductSearchAdpter(Context context, int intentPosition, List<ProductStructure> productList) {
        this.logs = new Logs(this.getClass());
        this.context = context;
        this.intentPosition = intentPosition;
        this.ProductList = productList;
    }

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

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

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

        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ProductStructure product = ProductList.get(position);
//        String filePath = "";
//        File f = new File(filePath, productStructure.getProductImg());
//        holder.binding.imageProductPicture.setImageBitmap(BitmapFactory.decodeFile(f.toString()));

        String urlString = product.getProductImg();
        try {
            new ImageGlide(context).fileload(urlString, holder.binding.imageProductPicture);
        } catch (LogsEmptyException e) {
            this.logs.warning(e);
        }

        holder.binding.textProductId.setText(product.getProductID());
        holder.binding.textProductName.setText(product.getProductName());
        holder.binding.buttonSlotProductSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("select product", "點選商品:"+product.getProductName());
                Toast.makeText(v.getContext(), "點選商品:"+product.getProductName(), Toast.LENGTH_SHORT).show();//點選商品
                Intent resultIntent = new Intent();
                resultIntent.putExtra("position", intentPosition);
                resultIntent.putExtra("product_id", product.getProductID());
                ProductSearchActivity productSearchActivity = (ProductSearchActivity) context;
                productSearchActivity.setResult(Activity.RESULT_OK, resultIntent);
                productSearchActivity.finish();
            }
        });
    }

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

    public void addProductData(ProductStructure productStructure) {
        ProductList.add(productStructure);
        notifyItemInserted(ProductList.size() -1);
    }
}
