package com.unibuy.smartdevice.ui.recycler;

import android.content.Context;
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.MyApp;
import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.databinding.RecyclerDialogMarketingPlanListBinding;
import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.structure.BuyStructure;
import com.unibuy.smartdevice.structure.MarketingPlanStructure;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.ui.dialog.MarketingPlanDialog;
import com.unibuy.smartdevice.ui.tools.ImageGlide;

import java.util.List;

public class RecyclerDialogMarketingPlanListAdpter extends RecyclerView.Adapter<RecyclerDialogMarketingPlanListAdpter.ViewHolder> {
    private Logs logs;
    private Context context;
    private final List<MarketingPlanStructure> marketingPlanList;
    private HandlerMain handlerMain;

    public RecyclerDialogMarketingPlanListAdpter(Context context, List<MarketingPlanStructure> marketingPlanList, HandlerMain handlerMain) {
        this.logs = new Logs(this.getClass());
        this.context = context;
        this.marketingPlanList = marketingPlanList;
        this.handlerMain = handlerMain;
    }

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

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

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

        return new ViewHolder(binding);
    }

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

        MarketingPlanStructure marketingPlanProduct = marketingPlanList.get(position);
        holder.binding.textPosition.setText(String.valueOf(position));
        String productID = marketingPlanProduct.getProduct().getProductID();
        if (!productID.isEmpty() && !productID.equals("?")) {
            holder.binding.textProductName.setText(marketingPlanProduct.getProduct().getProductName());
        } else {
            holder.binding.textProductName.setText(context.getString(R.string.click_select_product));//點我選擇商品
        }
        String urlString = marketingPlanProduct.getProduct().getProductImg();
        try {
            new ImageGlide(context).fileload(urlString, holder.binding.imageProductPicture);
        } catch (LogsEmptyException e) {
            this.logs.warning(e);
        }
        int machinePrice = marketingPlanProduct.getProduct().getMachinePrice();
        int sellingPrice = marketingPlanProduct.getProduct().getSellingPrice();
        int memberPrice = marketingPlanProduct.getProduct().getMemberPrice();

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

        int price = machinePrice > 0? machinePrice: sellingPrice;

        holder.binding.textPrice.setText("$"+price);
        holder.binding.buttonSelect.setOnClickListener(new SelectOnClickListener(context, holder.binding, position));
    }

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

    public void addProductData(MarketingPlanStructure marketingPlanProduct) {
        marketingPlanList.add(marketingPlanProduct);
        notifyItemInserted(marketingPlanList.size() -1);
    }

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

    public class SelectOnClickListener implements View.OnClickListener {
        private Context context;
        private RecyclerDialogMarketingPlanListBinding binding;
        private int position;

        public SelectOnClickListener(Context context, RecyclerDialogMarketingPlanListBinding binding, int position) {
            this.context = context;
            this.binding = binding;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            MarketingPlanStructure marktingPlanProduct = marketingPlanList.get(position);

            boolean pass = true;
            for (int index = 0; index < MyApp.getInstance().getBuyList().size(); index++) {
                BuyStructure buyData = MyApp.getInstance().getBuyList().get(index);
                if (buyData.getField() == marktingPlanProduct.getField() && buyData.getSlot() == marktingPlanProduct.getSlot()) {
                    pass = false;
                    int count = buyData.getCount();
                    buyData.setCount(count + 1);
                }
            }

            if (pass) {
                BuyStructure buyProduct = new BuyStructure(marktingPlanProduct.getField(), marktingPlanProduct.getSlot(), marktingPlanProduct.getProduct(), 1);
                buyProduct.setMarketingPlan(marktingPlanProduct.isMarketingPlan());
                MyApp.getInstance().getBuyList().add(buyProduct);
            }

            handlerMain.start(getClass().getSimpleName(), MarketingPlanDialog.Option.CHANGE_BUY.getOption(), "change buy");
        }
    }
}
