package com.unibuy.smartdevice.ui.recycler;

import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

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

import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.databinding.RecyclerDialogBuyListBinding;
import com.unibuy.smartdevice.databinding.RecyclerDialogPickupCodeBuyListBinding;
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.dialog.BuyDialog;
import com.unibuy.smartdevice.ui.tools.ImageGlide;

import java.util.List;

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

    public RecyclerDialogPickupCodeBuyListAdpter(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 {
        RecyclerDialogPickupCodeBuyListBinding binding;

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

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 使用 ViewBinding 來綁定佈局
        RecyclerDialogPickupCodeBuyListBinding binding = RecyclerDialogPickupCodeBuyListBinding.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(position));
        String productID = buyProduct.getProduct().getProductID();
        if (!productID.isEmpty() && !productID.equals("?")) {
            holder.binding.textProductName.setText(buyProduct.getProduct().getProductName());
        } else {
            holder.binding.textProductName.setText(context.getString(R.string.click_select_product));//
        }
//        holder.binding.textDelete.setOnClickListener(new textDeleteOnClickListener(position));
        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 memberPrice = buyProduct.getProduct().getMemberPrice();

        int slotUpperLimit = buyProduct.getProduct().getSlotUpperLimit();
        int count = buyProduct.getCount();

        int price = machinePrice > 0? machinePrice: sellingPrice;

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

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

    public class addition1OnClickListener implements View.OnClickListener {
        private Context context;
        private RecyclerDialogBuyListBinding binding;
        private int position;

        public addition1OnClickListener(Context context, RecyclerDialogBuyListBinding binding, int position) {
            this.context = context;
            this.binding = binding;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            BuyStructure buyProduct = buyList.get(position);

            int count = buyProduct.getCount();
            count++;
            buyList.get(position).setCount(count);
            binding.textCount.setText("x"+String.valueOf(count));

            handlerMain.start(getClass().getSimpleName(), BuyDialog.Option.SET_PRICE.getOption(), "total price");
        }
    }

    public class subtraction1OnClickListener implements View.OnClickListener {
        private Context context;
        private RecyclerDialogBuyListBinding binding;
        private int position;
        public subtraction1OnClickListener(Context context, RecyclerDialogBuyListBinding binding, int position) {
            this.context = context;
            this.binding = binding;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            BuyStructure buyProduct = buyList.get(position);

            int count = buyProduct.getCount();
            count--;
            if (count <= 0) {
                buyList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, buyList.size() - position);

                handlerMain.start(getClass().getSimpleName(),context.getString(R.string.delete_a_record));//刪除一筆資料
            } else {
                buyList.get(position).setCount(count);
                binding.textCount.setText("x"+String.valueOf(count));

                handlerMain.start(getClass().getSimpleName(), BuyDialog.Option.SET_PRICE.getOption(),"total price");
            }
        }
    }

    public class editCountOnEditorActionListener implements TextView.OnEditorActionListener{
        private ViewHolder holder;
        private int position;

        public editCountOnEditorActionListener(ViewHolder holder, int position) {
            this.holder = holder;
            this.position = position;
        }

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                int count = Integer.valueOf(v.getText().toString());
                buyList.get(position).setCount(count);
                notifyItemChanged(position);
                return true;
            }
            return false;
        }
    }

    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 buyList.size();
    }

    public void addProductData(BuyStructure buyProduct) {
        buyList.add(buyProduct);
        notifyItemInserted(buyList.size() -1);
    }

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