package com.unibuy.smartdevice.ui.recycler;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.databinding.RecyclerDialogDispatchListBinding;
import com.unibuy.smartdevice.devices.SlotField;
import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.structure.DispatchStructure;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.ui.tools.ImageGlide;

import java.util.List;

public class RecyclerDialogDispatchListAdpter extends RecyclerView.Adapter<RecyclerDialogDispatchListAdpter.ViewHolder> {
    private Logs logs;
    private Context context;
    private final List<DispatchStructure> dispatchList;
    private HandlerMain handlerMain;

    public RecyclerDialogDispatchListAdpter(Context context, List<DispatchStructure> dispatchList, HandlerMain handlerMain) {
        this.logs = new Logs(this.getClass());
        this.context = context;
        this.dispatchList = dispatchList;
        this.handlerMain = handlerMain;
    }

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

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

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

        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DispatchStructure dispatchProduct = dispatchList.get(position);

        holder.binding.textSlot.setText(String.valueOf(dispatchProduct.getSlot()));

        String productID = dispatchProduct.getProduct().getProductID();
        if (!productID.isEmpty() && !productID.equals("?")) {
            holder.binding.textProductName.setText(dispatchProduct.getProduct().getProductName());
        } else {
            holder.binding.textProductName.setText(context.getString(R.string.click_select_product));//點我選擇商品
        }

        holder.binding.textDelete.setOnClickListener(new textDeleteOnClickListener(position));

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

        int machinePrice = dispatchProduct.getProduct().getMachinePrice();
        int sellingPrice = dispatchProduct.getProduct().getSellingPrice();
        int memberPrice = dispatchProduct.getProduct().getMemberPrice();

        int slotUpperLimit = dispatchProduct.getProduct().getSlotUpperLimit();
        int count = dispatchProduct.getCount();
        int quantity = dispatchProduct.getQuantity();

        int price = machinePrice > 0? machinePrice: sellingPrice;

        holder.binding.textCount.setText(String.valueOf(count));
        holder.binding.textQuantity.setText(String.valueOf(quantity));

        logs.info("isShipped:" + dispatchProduct.isShipped());

        if (dispatchProduct.getCount() == dispatchProduct.getQuantity()) {
            holder.binding.textDispatchStatus.setText(context.getString(R.string.shipped));//已出貨
            holder.binding.textDispatchStatus.setTextColor(Color.RED);
        } else if (dispatchProduct.isShipped()) {
            try {
                if (SlotField.getSlotField(dispatchProduct.getField()).isCanOneShipped()) {
                    holder.binding.textDispatchStatus.setText(context.getString(R.string.shipping)+"("+(count+1)+")");//出貨中
                } else {
                    holder.binding.textDispatchStatus.setText(context.getString(R.string.shipping)+"(All)");//出貨中
                }
            } catch (LogsEmptyException e) {
                holder.binding.textDispatchStatus.setText(context.getString(R.string.shipping)+"("+(count+1)+")");//出貨中
            }

            holder.binding.textDispatchStatus.setTextColor(Color.RED);
        } else {
            holder.binding.textDispatchStatus.setText(context.getString(R.string.waiting));//等待中
            holder.binding.textDispatchStatus.setTextColor(Color.BLACK);
        }
    }

    public class textDeleteOnClickListener implements View.OnClickListener{
        private int position;

        public textDeleteOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            delProductData(position);
        }
    }
    @Override
    public int getItemCount() {
        return dispatchList.size();
    }

    public void addProductData(DispatchStructure dispatchProduct) {
        dispatchList.add(dispatchProduct);
        notifyItemInserted(dispatchList.size() -1);
    }

    public void delProductData(int position) {
        dispatchList.remove(position);
        notifyItemRemoved(position);
    }
}
