package com.zjgsu.jianshu

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import cn.bmob.v3.Bmob
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
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_login.*
import kotlinx.android.synthetic.main.activity_register.*

class LoginActivity : AppCompatActivity() {
    //    var mBtnLogin:TextView= TextView(this)
    override fun onCreate(savedInstanceState: Bundle?) {
        var flag: Boolean = false
        var done: Boolean = false
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        forget_password.setOnClickListener {
            val intent = Intent(this, FindpwdActivity::class.java)
            startActivity(intent)
            finish()
        }
        bt_register.setOnClickListener {
            val intent = Intent(this, RegisterActivity::class.java)
            startActivity(intent)
            finish()
        }
        bt_login.setOnClickListener {
            val use_account: String = et_blackOutName.text.toString()
            val use_pwd: String = et_UserPassword.text.toString()
            val bmobQuery = BmobQuery<User_bmob>()
            bmobQuery.findObjects(object : FindListener<User_bmob>() {
                override fun done(list: List<User_bmob>, e: BmobException?) {
                    if (e == null) {
                        for (i in list) {
                            if (use_account.equals(i.account) && use_pwd.equals(i.password)) {
                                val editor=getSharedPreferences("userinf", Context.MODE_PRIVATE).edit()
                                editor.putString("user_id",i.objectId)
                                editor.apply()
                                flag = true
                                val intent = Intent(this@LoginActivity, MainActivity::class.java)
                                intent.putExtra("userid",i.objectId)
                                startActivity(intent)
                                Toast.makeText(this@LoginActivity, "登陆成功！", Toast.LENGTH_SHORT)
                                    .show()
                                break
                            }
                        }
                        done = true
                    }
                }
            })
            if (done) {
                var a: String = ""
                if (flag)
                    a = "1"
                else
                    a = "0"
                Log.d("zynnnb", a)
                if (use_account.isEmpty() || use_pwd.isEmpty())
                    Toast.makeText(this, "账号或密码不能为空白", Toast.LENGTH_SHORT).show()
                else if (flag != true)
                    Toast.makeText(this@LoginActivity, "账号或密码不正确", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
