package com.zjgsu.jianshu

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.os.Process
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import cn.bmob.v3.Bmob
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.zjgsu.jianshu.Bmob.User_bmob
import kotlinx.android.synthetic.main.activity_findpwd.*
import kotlinx.android.synthetic.main.activity_findpwd.image_findpwd_backtoLogin
import kotlinx.android.synthetic.main.activity_register.*

class FindpwdActivity:AppCompatActivity() {
    private var msg:String=""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_findpwd)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        image_findpwd_backtoLogin.setOnClickListener{
            val intent=Intent(this,LoginActivity::class.java)
            startActivity(intent)
            finish()
        }
        inits()
    }
    private val click2 = DialogInterface.OnClickListener { arg0, arg1 -> arg0.cancel()
    val intent=Intent(this,LoginActivity::class.java)
        startActivity(intent)
        finish()
    }
    public fun showdialog(view:View){
        val alertdialog:AlertDialog.Builder=AlertDialog.Builder(this)
        alertdialog.setMessage(msg)
        alertdialog.setNegativeButton("确认", click2);
        val alertdialog1:AlertDialog=alertdialog.create();
        alertdialog1.show();
    }
    public fun inits(){
        bt_find_nextstep.setOnClickListener{
            var flag:Boolean=false
            val user_account: String = et_find_account.text.toString()
            val user_email:String=et_find_mail.text.toString()
            if(user_account.isEmpty()||user_email.isEmpty()) {
                Toast.makeText(this, "账号或邮箱不能为空！", Toast.LENGTH_SHORT).show()
                Log.d("zy",user_account)
            }
            else{
                val bmobQuery = BmobQuery<User_bmob>()
                var tempPwd:String=""
                bmobQuery.findObjects(object :FindListener<User_bmob>(){
                    override fun done(list: List<User_bmob>, e: BmobException?) {
                        if (e == null) {
                            for (i in list) {
                                if (user_account.equals(i.account)&&user_email.equals(i.email)) {
                                    flag = true
                                    tempPwd=i.password
                                    msg="您的密码为: "+tempPwd
                                    showdialog(bt_find_nextstep)
                                }
                            }
                            if(flag!=true)
                                Toast.makeText(this@FindpwdActivity,"账号不存在，请重新输入！",Toast.LENGTH_SHORT).show()
                        }
                    }
                })
            }
        }
    }
}