package com.example.iot_controlhost.ui.dialog;

import android.content.Context;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogBiasThBinding;
import com.example.iot_controlhost.databinding.DialogCustomBinding;
import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.global.AutoModelSet;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.log.UserLog;
import com.example.iot_controlhost.utils.log.MyLog;

/**
 * @Description 文本输入弹窗
 * @Author DuanKaiji
 * @CreateTime 2024年03月12日 15:54
 */
public class THBiasDialog extends BaseDialog<DialogBiasThBinding> {
    public THBiasDialog(@NonNull Context context, boolean isTemp) {
        super(context, true);
        binding.tvTitle.setText(isTemp ? "校准温度" : "校准湿度");
        String value = String.valueOf(isTemp ? RoomSetting.getBiasTemp() : RoomSetting.getBiasHumidity());
        binding.tvSet1.setText(isTemp?"温度偏差":"湿度偏差");
        binding.tvTitle.setOnClickListener(v -> new NumberInputDialog(mContext, binding.tvTitle).show());
        binding.tvSet1Symbol.setText(isTemp ? R.string.T_symbol : R.string.H_symbol);
        binding.btnAdd1.setOnClickListener(getTargetListener(binding.tiSet1, isTemp, true));
        binding.btnMinus1.setOnClickListener(getTargetListener(binding.tiSet1, isTemp, false));
        binding.tiSet1.setText(value);
        binding.tiSet1.setOnClickListener(v -> new NumberInputDialog(mContext, binding.tiSet1).show());
        binding.btnYes.setOnClickListener(view -> {
            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            double oldValue = isTemp ? RoomSetting.getBiasTemp() : RoomSetting.getBiasHumidity();
            double newValue = Double.parseDouble(binding.tiSet1.getText().toString());
            MMKVUtil.put(isTemp ? RoomSetting.BIAS_TEMP : RoomSetting.BIAS_HUMIDITY, newValue);
            String typeName = isTemp ? "温度" : "湿度";
            UserLog.operate("本机屏幕", typeName + "校准偏差变化：" + oldValue + " → " + newValue);
            MyLog.app(typeName + "校准偏差修改：" + oldValue + "→" + newValue);
            dismiss();
        });
    }

    private View.OnClickListener getTargetListener(TextView t1, boolean isTemp, boolean isAdd) {
        double value = isTemp ? 0.1 : 1;
        return view -> {
            double d1 = Double.parseDouble(format(t1.getText().toString()));
            double result = isAdd ? d1 + value : d1 - value;
            t1.setText(String.format("%.1f", result));
        };
    }

    private String format(String str) {
        if (str == null) {
            return "0.0";
        }
        if (str.contains("|")) {
            // 删除|
            str = str.replace("|", "");
        }
        return str;
    }

}
