package com.example.tyre.util;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.tyre.R;
import com.example.tyre.entity.CarTeamEntity;

import java.util.List;

public class CarTeamSelectionDialog extends Dialog {
    private ListView carListView;
    private CarListAdapter adapter;
    private OnCarSelectedListener listener;
    // 1. 把 String 列表改为 CarTeamEntity 列表
    private List<CarTeamEntity> carEntityList;

    public CarTeamSelectionDialog(Context context, List<CarTeamEntity> carEntityList) {
        super(context);
        this.carEntityList = carEntityList;
        initDialog();
    }

    private void initDialog() {
        setContentView(R.layout.dialog_car_selection);
        setCanceledOnTouchOutside(true);

        carListView = findViewById(R.id.car_list_view);
        adapter = new CarListAdapter(getContext(), carEntityList);
        carListView.setAdapter(adapter);

        carListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    // 获取当前选中的实体
                    CarTeamEntity selectedCar = carEntityList.get(position);
                    // 回调返回：如果需要车牌号就传 selectedCar.getCarTeamName()，需要ID就传 selectedCar.getCarTeamId()
                    // 这里先返回显示的名称，如需ID可修改回调参数类型
                    listener.onCarSelected(selectedCar.getCarTeamName(),selectedCar.getCarTeamId());
                    // 如需拿到ID，可参考下方「扩展说明」修改回调
                }
                dismiss();
            }
        });

        findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setOnCarSelectedListener(OnCarSelectedListener listener) {
        this.listener = listener;
    }

    public interface OnCarSelectedListener {
        void onCarSelected(String carName, String carId);
    }

    private static class CarListAdapter extends BaseAdapter {
        private Context context;
        // 3. 适配器数据源改为 CarTeamEntity 列表
        private List<CarTeamEntity> carEntityList;

        // 4. 适配器构造方法同步修改
        public CarListAdapter(Context context, List<CarTeamEntity> carEntityList) {
            this.context = context;
            this.carEntityList = carEntityList;
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
            }
            TextView textView = convertView.findViewById(android.R.id.text1);
            // 5. 只显示 carTeamName 字段（carTeamId 隐藏）
            textView.setText(carEntityList.get(position).getCarTeamName());
            return convertView;
        }
    }
}
