package com.dlc.eboard.ui.settings

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import com.dlc.eboard.R
import com.dlc.eboard.common.Constants
import com.dlc.eboard.databinding.DialogPasswordBinding
import com.dlc.eboard.sp.EnvSp

/**
 * info
 *
 * @author ReiChin_
 */
class PasswordDialog(context: Context, private val dialogListener: (() -> Unit)) :
    Dialog(context, R.style.AlertDialogStyle) {

    private val viewBinding: DialogPasswordBinding by lazy {
        DialogPasswordBinding.inflate(LayoutInflater.from(context))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)

        // 取消
        viewBinding.btnNeg.setOnClickListener { dismiss() }
        // 确定
        viewBinding.btnPos.setOnClickListener {
            val password = viewBinding.etInput.text.toString().trim()
            val settingPwd = EnvSp.loadEnv(context)?.settingPwd ?: Constants.Mock.SETTING_PWD
            if (password == settingPwd) {
                dialogListener.invoke()
                dismiss()
            } else {
                Toast.makeText(context, "密码不正确！", Toast.LENGTH_SHORT).show()
            }
        }
    }

}