package com.example.myapp_2.List_1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp_2.R;

import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
    private OnAddToCartClickListener addToCartClickListener;
    private List<Product> products;
    private Context context;

    public ProductAdapter(Context context, List<Product> products) {
        this.context = context;
        this.products = products;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_product, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ProductViewHolder holder, int position) {
        Product product = products.get(position);
        holder.productImage.setImageResource(product.getImageResource());
        holder.productName.setText(product.getName());
        holder.productDescription.setText(product.getDescription());
        holder.productRating.setText(String.format("%.1f", product.getRating()));






        holder.addToCartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addToCartClickListener != null) {
                    addToCartClickListener.onAddToCartClick(product);
                }
            }
        });
        holder.priceTextView.setText(context.getString(R.string.price, product.getPrice()));
    }

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

    public class ProductViewHolder extends RecyclerView.ViewHolder {
        public Button addToCartButton;
        ImageView productImage;
        TextView productName;
        TextView productDescription;
        TextView productRating;

        public ProductViewHolder(View view) {
            super(view);
            productImage = view.findViewById(R.id.product_image);
            productName = view.findViewById(R.id.product_name);
            productDescription = view.findViewById(R.id.product_description);
            productRating = view.findViewById(R.id.product_rating);
            addToCartButton = itemView.findViewById(R.id.add_button);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showRatingDialog(getAdapterPosition());
                }
            });

        }
    }

    private void showRatingDialog(final int position) {
        final RatingDialog ratingDialog = new RatingDialog(context);
        ratingDialog.setRating(products.get(position).getRating());
        ratingDialog.setOnRatingChangedListener(new RatingDialog.OnRatingChangedListener() {
            @Override
            public void onRatingChanged(float newRating) {
                products.get(position).setRating(newRating);
                notifyDataSetChanged();
            }
        });
        ratingDialog.show();
    }
    public interface OnAddToCartClickListener {
        void onAddToCartClick(Product product);
    }
    public void setOnAddToCartClickListener(OnAddToCartClickListener listener) {
        this.addToCartClickListener = listener;
    }

}
(Выдается ошибка : Cannot resolve symbol 'priceTextView')