package com.bpa.scalage.control;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.widget.Button;

import com.bpa.scalage.mode.Foods.FoodModel;
import com.bpa.scalage.MainActivity;
import com.bpa.scalage.R;
import com.bpa.scalage.data.DataBus;

//用于将上下文、listview 子项布局的 id 和数据都传递过来
public class FruitAdapter extends ArrayAdapter<FoodModel> {
    private OnSelectListener onSelectListener; //加入购物车按钮的监听事件

    public FruitAdapter(@NonNull Context context, int resource, @NonNull List<FoodModel> objects) {
        super(context, resource, objects);
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        FoodModel fruit=(FoodModel)getItem(position);//得到当前项的 Fruit 实例
        //为每一个子项加载设定的布局
        View view=LayoutInflater.from(getContext()).inflate(R.layout.menu_item,parent,false);
        //分别获取 image view 和 textview 的实例
        MyImageView fruitimage =view.findViewById(R.id.iv_food_pic);
        TextView fruitname =view.findViewById(R.id.tv_food_name);
        TextView fruitprice =view.findViewById(R.id.tv_price);
        TextView fruitvipprice =view.findViewById(R.id.tv_vipprice);
        Button add_car= view.findViewById(R.id.btn_add_car);
        add_car.setTag(position);
        if(add_car!=null)//点击事件打开，称重界面
        {
            add_car.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                        try
                        {
                            Button add_car1= view.findViewById(R.id.btn_add_car);
                            int point= (int) add_car1.getTag();
                            FoodModel fruit=getItem(point);
                            if(fruit !=null )
                            {
                                Intent intent = new Intent(getContext(), MainActivity.class);
                                DataBus.getInstance().SelectFood=fruit;
                                getContext().startActivity(intent);
                            }
                        } catch (Exception e)
                        {
                            throw new RuntimeException(e);
                        }
                }
            });
        }
        // 设置要显示的图片和文字
        fruitimage.glideAppLoadUrlImage(fruit.cover);
        fruitname.setText(fruit.name);
        fruitprice.setText("非会员 ¥"+new java.text.DecimalFormat("#.00").format(fruit.price)+"/50g");
        fruitvipprice.setText("会员 ¥"+new java.text.DecimalFormat("#.00").format(fruit.vipPrice)+"/50g");
        view.setTag(fruit);
        return view;
    }

    public interface OnSelectListener {
        void onSelectAddCar (int position); //处理加入购物车按钮的方法
    }

}
