package com.example.register.adapter;

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

import com.example.register.R;
import com.example.register.entity.OrangeProductPack;

import java.util.List;

public class ProductAdapter extends BaseAdapter {
    //private List<Product> productList;
    private List<OrangeProductPack> orangeProductList;
    private LayoutInflater layoutInflater;

    public ProductAdapter(Context context, List<OrangeProductPack> orangeProductList) {
        this.orangeProductList = orangeProductList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return orangeProductList.size();
    }

    @Override
    public Object getItem(int position) {
        return orangeProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.categoty_detail_content, null);
            viewHolder = new ViewHolder();
            viewHolder.productImage = convertView.findViewById(R.id.category_product_image);
            viewHolder.productName = convertView.findViewById(R.id.category_product_name);
            viewHolder.productPrice = convertView.findViewById(R.id.category_product_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        OrangeProductPack product = orangeProductList.get(position);
        if (product != null) {
            viewHolder.productImage.setImageBitmap(product.getImgBitmap());
            viewHolder.productName.setText(product.getName());
            viewHolder.productPrice.setText(String.format("%s元", String.valueOf(product.getPrice())));
        }
        return convertView;
    }

    class ViewHolder {
        ImageView productImage;
        TextView productName, productPrice;
    }

}
