package com.zjgsu.jianshu

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
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 cn.bmob.v3.listener.QueryListListener
import cn.bmob.v3.listener.SaveListener
import cn.bmob.v3.listener.UpdateListener
import com.zjgsu.jianshu.Bmob.User_bmob
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_register.*

class RegisterActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        inits()
    }

    private fun inits() {
        image_backtoLogin.setOnClickListener{
            val intent=Intent(this,LoginActivity::class.java)
            startActivity(intent)
            finish()
        }
        button_check.setOnClickListener {
            var flag: Boolean = false
            val use_account: String = et_reg_account.text.toString()
            Log.d("zhangyu", use_account)
            val bmobQuery = BmobQuery<User_bmob>()
//            bmobQuery.addWhereEqualTo("account", use_account)
            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)) {
                                Toast.makeText(this@RegisterActivity, "账号已注册！", Toast.LENGTH_SHORT)
                                    .show()
                                flag=true
                            }
                        }
                        if(flag!=true)
                            Toast.makeText(this@RegisterActivity,"可以注册",Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
        bt_reg_register.setOnClickListener {
            var sex: String = ""
            if (rg_reg_man.isChecked)
                sex = "男"
            else if (rg_reg_woman.isChecked)
                sex = "女"
            else
                sex = "未知"
            val user = User_bmob()
            val use_account: String = et_reg_account.text.toString()
            val use_pwd: String = et_reg_pwd.text.toString()
            val use_agapwd: String = et_reg_agapwd.text.toString()
            val use_name: String = et_reg_name.text.toString()
            val use_mail: String = et_reg_email.text.toString()
            val use_phone: String = et_reg_phone.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)) {
                                Toast.makeText(this@RegisterActivity, "账号已注册！", Toast.LENGTH_SHORT)
                                    .show()
                            }
                        }
                    }
                }
            })
            if (use_account.isEmpty() || use_pwd.isEmpty() || use_agapwd.isEmpty())
                Toast.makeText(this, "账号或密码不能为空白！", Toast.LENGTH_SHORT).show()
            if (use_mail.isEmpty())
                Toast.makeText(this, "邮箱不能为空白！", Toast.LENGTH_SHORT).show()
            else if (use_agapwd != use_pwd) {
                Toast.makeText(this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show()
            }
            else {
                user.setaccount(use_account)
                user.setpassword(use_pwd)
                user.setemail(use_mail)
                user.setnickName(use_name)
                user.setphone(use_phone)
                user.setsex(sex)
                user.save(object : SaveListener<String>() {
                    override fun done(objectId: String?, e: BmobException?) {
                        if (e == null) {
                            Toast.makeText(
                                this@RegisterActivity,
                                objectId + "注册成功！",
                                Toast.LENGTH_SHORT
                            ).show()
                        } else {
                            Toast.makeText(
                                this@RegisterActivity,
                                "注册失败!" + e.message,
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    }
                })
                val intent = Intent(this, LoginActivity::class.java)
                startActivity(intent)
                finish()
            }
        }
    }
}