package com.example.iot_controlhost.ui.dialog;

import android.content.Context;
import android.view.View;
import androidx.annotation.NonNull;
import com.blankj.utilcode.util.StringUtils;
import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogDeviceCustomSetBinding;
import com.example.iot_controlhost.ui.activity.SetActivity;
import com.example.iot_controlhost.ui.view.CustomSpinnerAdapter;
import com.example.iot_controlhost.utils.log.UserLog;
import com.example.iot_controlhost.utils.log.MyLog;

import es.dmoral.toasty.Toasty;

/**
 * @Description 设备基础设置弹窗
 * @Author DuanKaiji
 * @CreateTime 2024年04月10日 11:34:10
 */
public class SetDeviceBaseDialog extends BaseDialog<DialogDeviceCustomSetBinding> {

    /**
     * 显示基础配置弹窗
     *
     * @param context  上下文
     * @param title    标题
     * @param ac1List  下拉框1的内容
     * @param ac1Hint  下拉框1的提示
     * @param ac2List  下拉框2的内容
     * @param ac2Hint  下拉框2的提示
     * @param listener 点击确定事件
     */
    public SetDeviceBaseDialog(@NonNull Context context, String title,
                               String[] ac1List, String ac1Hint, String oldValue1,
                               String[] ac2List, String ac2Hint, String oldValue2,
                               OnCommonSetListener listener) {
        super(context,true);
        binding.tilSpinner1.setVisibility(View.VISIBLE);
        binding.tilSpinner2.setVisibility(View.VISIBLE);
        binding.tilSpinner1.setHint(ac1Hint);
        binding.tilSpinner2.setHint(ac2Hint);
        binding.acInput1.setAdapter(new CustomSpinnerAdapter(context, ac1List));
        binding.acInput2.setAdapter(new CustomSpinnerAdapter(context, ac2List));
        binding.tvTitle.setText(title);
        binding.btnYes.setText(context.getText(R.string.yes));
        binding.btnYes.setOnClickListener(view -> {
            if (StringUtils.isEmpty(binding.acInput1.getText().toString())) {
                Toasty.info(context, ac1Hint).show();
                return;
            }
            if (StringUtils.isEmpty(binding.acInput2.getText().toString())) {
                Toasty.info(context, ac2Hint).show();
                return;
            }
            String newValue1 = binding.acInput1.getText().toString();
            String newValue2 = binding.acInput2.getText().toString();
            listener.btnSet(newValue1, newValue2);
            String change = title + "配置变化：" + ac1Hint + "=" + oldValue1 + " → " + newValue1
                    + "，" + ac2Hint + "=" + oldValue2 + " → " + newValue2;
            UserLog.operate("本机屏幕", change);
            MyLog.app(change);
            dismiss();
        });
    }

    public interface OnCommonSetListener {
        /**
         * 设置按钮事件
         */
        void btnSet(String ac1, String ac2);
    }


}
