package com.brisco.meatwholesaler;

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

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

import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {

    private Context context;
    private List<Order> orderList;

    public OrderAdapter(Context context, List<Order> orderList) {
        this.context = context;
        this.orderList = orderList;
    }

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

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        Order order = orderList.get(position);
        
        holder.textViewOrderNumber.setText(order.getOrderNumber());
        holder.textViewProductName.setText(order.getProductName());
        holder.textViewTotalAmount.setText(order.getFormattedTotal());
        holder.textViewOrderDate.setText(order.getOrderDate());
        holder.textViewStatus.setText(order.getStatus());

        // Set status color
        switch (order.getStatus().toLowerCase()) {
            case "delivered":
                holder.textViewStatus.setTextColor(context.getResources().getColor(R.color.success));
                break;
            case "in transit":
                holder.textViewStatus.setTextColor(context.getResources().getColor(R.color.warning));
                break;
            case "processing":
                holder.textViewStatus.setTextColor(context.getResources().getColor(R.color.info));
                break;
            default:
                holder.textViewStatus.setTextColor(context.getResources().getColor(R.color.text_secondary));
                break;
        }
    }

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

    static class OrderViewHolder extends RecyclerView.ViewHolder {
        TextView textViewOrderNumber;
        TextView textViewProductName;
        TextView textViewTotalAmount;
        TextView textViewOrderDate;
        TextView textViewStatus;

        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewOrderNumber = itemView.findViewById(R.id.text_order_number);
            textViewProductName = itemView.findViewById(R.id.text_product_name);
            textViewTotalAmount = itemView.findViewById(R.id.text_total_amount);
            textViewOrderDate = itemView.findViewById(R.id.text_order_date);
            textViewStatus = itemView.findViewById(R.id.text_status);
        }
    }
}
