package com.example.iot_controlhost.ui.dialog;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;

import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogCustomBinding;
import com.example.iot_controlhost.ui.view.CustomSpinnerAdapter;

import es.dmoral.toasty.Toasty;

/**
 * @Description 下拉框弹窗
 * @Author DuanKaiji
 * @CreateTime 2024年03月13日 14:08
 */
public class SpinnerDialog extends BaseDialog<DialogCustomBinding> {
    /**
     * 获取下拉框弹窗
     *
     * @param context  上下文
     * @param title    标题
     * @param hint     提示信息 会在默认时、未选择点击确定时显示
     * @param list     下拉框列表内容
     * @param listener 点击确定后的事件
     */
    public SpinnerDialog(@NonNull Context context, String title, String hint, String[] list, CustomString listener) {
        super(context);
        binding.tilSpinner.setVisibility(View.VISIBLE);
        binding.tvMessage.setVisibility(View.GONE);
        binding.btnNo.setVisibility(View.GONE);
        binding.tvTitle.setText(title);
        ArrayAdapter<String> adapter = new CustomSpinnerAdapter(context, list);
        binding.acInput.setAdapter(adapter);
        binding.acInput.setHint(hint);
        binding.acInput.setDropDownHeight(
                (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP,
                        350,
                        context.getResources().getDisplayMetrics()
                )
        );
        //需要默认显示已设置的项，还能同时显示所有项
        binding.btnYes.setOnClickListener(view -> {
            if (binding.acInput.getText().toString().isEmpty()) {
                Toasty.info(context, hint).show();
                return;
            }
            listener.listener(binding.acInput.getText().toString());
            dismiss();
        });
    }

}
