package com.example.iot_controlhost.ui.dialog;

import android.content.Context;
import android.text.InputType;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;

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.utils.global.RoomController;
import com.example.iot_controlhost.utils.log.UserLog;
import com.example.iot_controlhost.utils.log.MyLog;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

import es.dmoral.toasty.Toasty;

/**
 * @Description 获得手动配置地暖PWM弹窗
 * @Author DuanKaiji
 * @CreateTime 2024年04月11日 10:50:40
 */
public class SetPWMDialog extends BaseDialog<DialogDeviceCustomSetBinding> {
    public SetPWMDialog(@NonNull Context context,int gear,Runnable runnable) {
        super(context, true);
        binding.tvTitle.setText(gear + "档" + getString(R.string.set_pwm));
        binding.tilInput.setVisibility(View.VISIBLE);
        binding.tilInput.setHint(getString(R.string.pwm_param));
        binding.tiInput.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.btnYes.setText(getString(R.string.save));
        binding.btnYes.setOnClickListener(view -> {
            try {
                if (binding.tiInput.getText().toString().isEmpty()) {
                    Toasty.info(mContext, "请先输入PWM值").show();
                    return;
                }
                int pwm = Integer.parseInt(binding.tiInput.getText().toString());
                if (pwm <= 0) {
                    Toasty.error(mContext, "PWM不能小于等于0").show();
                    return;
                }
                if (pwm > RoomController.floor.getMaxPower()) {
                    Toasty.error(mContext, "PWM大于当前地暖型号支持的最大PWM").show();
                    return;
                }
                int oldPwm = RoomController.floor.getPwm(gear).getPwm();
                RoomController.floor.getPwm(gear).setPwm(pwm);
                UserLog.operate("本机屏幕", "地暖PWM变化：" + gear + "档=" + oldPwm + " → " + pwm);
                MyLog.app("地暖PWM修改：档位=" + gear + "，PWM=" + oldPwm + "→" + pwm);
            } catch (Exception e) {
                Toasty.error(mContext, "输入格式有误，请检查").show();
                return;
            }
            dismiss();
            runnable.run();
        });
    }
}
