package com.bbitcn.silk.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;

import com.bbitcn.silk.base.BaseDialog;
import com.bbitcn.silk.databinding.DialogCustomBinding;
import com.bbitcn.silk.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) {
        this(context, title, hint, list, listener, false);
    }

    public SpinnerDialog(@NonNull Context context, String title, String hint, String[] list, CustomString listener, boolean canEmpty) {
        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.btnYes.setOnClickListener(view -> {
            if (binding.acInput.getText().toString().isEmpty() && !canEmpty) {
                Toasty.info(context, hint).show();
                return;
            }
            listener.listener(binding.acInput.getText().toString());
            dismiss();
        });
    }

}
