package com.example.iot_controlhost.ui.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogPasswordBinding;
import com.example.iot_controlhost.ui.view.OnPasswordEnteredListener;
import com.google.android.material.textfield.TextInputEditText;

import es.dmoral.toasty.Toasty;

/**
 * @Description 密码输入弹窗
 * @Author DuanKaiji
 * @CreateTime 2024年03月12日 17:46
 */
public class PasswordDialog extends BaseDialog<DialogPasswordBinding> {

    public PasswordDialog(@NonNull Context context, String title, OnPasswordEnteredListener listener) {
        this(context, title, null, listener);
    }

    public PasswordDialog(@NonNull Context context, String title, String target, OnPasswordEnteredListener listener) {
        this(context, title, target, false, listener,false);
    }

    /**
     * @param context  上下文
     * @param title    题目
     * @param target   密码
     * @param showFace 是否可以通过人脸识别代替密码
     * @param listener 输入后的事件
     */
    public PasswordDialog(@NonNull Context context, String title, String target, boolean showFace, OnPasswordEnteredListener listener,boolean showPwd) {
        super(context);
        binding.tvTitle.setText(title);
        TextInputEditText inputEditText = binding.tiInput;
        if (showFace) {
            binding.ivFaceRecognize.setVisibility(View.VISIBLE);
            binding.ivFaceRecognize.setOnClickListener(view -> {
                new FaceDetectionDialog(context, listener).show();
                dismiss();
            });
        }
        if(showPwd){
            inputEditText.setInputType(InputType.TYPE_CLASS_TEXT);
        }else{
            inputEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        }
        inputEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                //大于六位才开始验证
                if (editable.toString().length() < 6) {
                    return;
                }
                if (!StringUtils.isEmpty(target) && target.equals(editable.toString())) {
                    Toasty.success(context, "密码验证通过 ").show();
                    listener.listener(inputEditText);
                    dismiss();
                }
            }
        });
        binding.btnYes.setOnClickListener(view -> {
            if (target != null) {
                if (StringUtils.isEmpty(inputEditText.getText().toString())) {
                    Toasty.info(context, "密码不可为空，请输入").show();
                    return;
                } else if (!inputEditText.getText().toString().equals(target)) {
                    Toasty.error(context, "密码错误").show();
                    inputEditText.setText("");
                    return;
                } else if (inputEditText.getText().toString().equals(target)) {
                    Toasty.success(context, "密码正确").show();
                }
            }
            //密码正确 || 并非验证密码 || 有其他的验证方式（不用target）
            listener.listener(inputEditText);
            dismiss();
        });
        binding.btn0.setOnClickListener(getNumberOnClickListener(inputEditText, 0));
        binding.btn1.setOnClickListener(getNumberOnClickListener(inputEditText, 1));
        binding.btn2.setOnClickListener(getNumberOnClickListener(inputEditText, 2));
        binding.btn3.setOnClickListener(getNumberOnClickListener(inputEditText, 3));
        binding.btn4.setOnClickListener(getNumberOnClickListener(inputEditText, 4));
        binding.btn5.setOnClickListener(getNumberOnClickListener(inputEditText, 5));
        binding.btn6.setOnClickListener(getNumberOnClickListener(inputEditText, 6));
        binding.btn7.setOnClickListener(getNumberOnClickListener(inputEditText, 7));
        binding.btn8.setOnClickListener(getNumberOnClickListener(inputEditText, 8));
        binding.btn9.setOnClickListener(getNumberOnClickListener(inputEditText, 9));
        binding.btnDelete.setOnClickListener(view -> {
            String password = inputEditText.getText().toString();
            if (!password.isEmpty()) {
                password = password.substring(0, password.length() - 1);
                inputEditText.setText(password);
            }
        });
    }

    /**
     * 获取密码输入弹窗的
     * 数字事件
     *
     * @param ti     输入框
     * @param number 目标数字 0-10
     * @return 点击事件
     */
    private View.OnClickListener getNumberOnClickListener(TextInputEditText ti, int number) {
        return view -> ti.setText(ti.getText().toString() + number);
    }
}
