package com.zjgsu.jianshu

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.UpdateListener
import com.zjgsu.jianshu.Bmob.User_bmob

import kotlinx.android.synthetic.main.activity_re_phone.*

class RePhoneActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_re_phone)

        back2my.setOnClickListener() {
            val intent = Intent(this, ReMyinfoActivity::class.java)
            startActivity(intent)
            finish()
        }
        val telPatten="^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}\$".toRegex()
//        Log.d("myLog",telPatten.matches(input = "19883120190").toString())

        val extra_account = intent.getStringExtra("extra_account")
        val extra_tel = intent.getStringExtra("extra_tel")
        Log.d("myLog",extra_tel.toString())
        cur_tel.setHint(extra_tel.toString())

        btnOk.setOnClickListener {
            var flag: Boolean = false
            val user_account: String = et_account.text.toString()
            var curTel = cur_tel.text.toString()
            //
//            val intent=Intent(this,ReMyinfoActivity::class.java)
            val intent = Intent(this, ReMyinfoActivity::class.java)
            if (user_account.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)) {
                                    flag = true //找到
                                    Log.d("myLog", "find sucessflly")
                                    //-------更新数据库中的sex
                                    if (telPatten.matches(input = curTel) == true) {
                                        i.setphone(curTel)
                                        i.update(i.objectId, object : UpdateListener() {
                                            override fun done(p0: BmobException?) {
                                                if (p0 == null) {
                                                    Toast.makeText(
                                                        this@RePhoneActivity,
                                                        "电话号码修改成功！",
                                                        Toast.LENGTH_SHORT
                                                    ).show()
                                                    //跳转个人信息页面，但是跳转之后原来的信息消失，需要重新进才能刷新
                                                    startActivity(intent)
//                                                startActivityForResult(intent,1)
                                                    finish()

                                                } else {
                                                    Log.d("myLog", "can't modify name")
                                                }
                                            }
                                        })
                                    }
                                    else{
                                        Toast.makeText(this@RePhoneActivity,"手机号输入不合法！"
                                            , Toast.LENGTH_SHORT).show()
                                    }
                                }
                            }

                            if (flag != true)
                                Toast.makeText(
                                   this@RePhoneActivity,
                                    "账号不存在，请重新输入！",
                                    Toast.LENGTH_SHORT
                                ).show()
                        }
                    }
                })
            }

        }

    }
}